Range.Autofilter is NOT filtering correct column - vba

I'm having a problem with my autofilter vba code where it's skipping the first column for some reason and taking field "4" as column E (the 5th column). This is extra odd because on some other files with the same exact setup, it doesn't do this, but for others it does. I can't put my finger on what the issue is (whether it's code or the actual spreadsheet). See the code below. Appreciate any help!
Workbooks(Num14).Sheets(1).Range("A:D").AutoFilter Field:=4, Criteria1:="REP"

I figured out the issue. I had to turn off autofiltermode first and then have the macro run the autofilter code. For some reason, the filter on the spreadsheet wasn't allowing for proper autofiltering. Hope this helps others!

Related

Autofill but skipping a column

My current formula is:
=IF(COUNTIF('W/C 1/8/22'!B$47:C$53,""&$B12&""),"S1","-")
When dragging/autofilling I would like the next box to refer to columns 'D'&'E'(and then the following 'F'&'G') and so on, basically to skip along as I have merged two columns on the 'w/c' sheet.
D11 is the first box this formula will be in and hopefully will be able to drag
/autofill from there.
I have tried lots of different methods but can't seem to insert the correct formula to make it work. Any help would be most appreciated, thank you!

Advanced filter error

I am trying to create a unique list but the first value in the list is a blank and that seems to be causing me a problem. When I use a basic advanced filter it essentially just names the range 'Extract' and copies the formatting. So F2 equals `Extract' as below,
I have tried many approaches using the criteria range but nothing seems to works. Ideally I want this to be VBA code but the manual Advanced Filter is not working. To clarify I am searching like this,
I see a similar question here, it's just not working for me. Does anyone have any ideas what I am doing wrong? Is it the 'Copy into new location' option that is messing this up? Ideally I need it to be unique fields but I seem to be getting the same results, with or without the unique fields only box ticked.
Eventually I want this to be the code but, when I run the below in VBA I get a Run-Time error.
Range("E2:E5").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"h1:h2"), CopyToRange:=Range("F2"), Unique:=True
EDIT
Use <> for the criteria to exclude blanks.
btw, your List Range should be E1:E5 and H1 should be Offset values. Advanced Filters require a header.

Inserting vlookup function via vba adds brackets on column

I've got a problem with brackets in complex vlookup "insert function".
It's needed for a plan for each day of week. Because of slow performance of workbook I decided to turn on/off each day of plan, entire plan works but the column with expeditions. Code for part of "U" column is:
Range(Cells(firstm, "U"), Cells(lastm, "U")) = "=IF(RC2=0,IF(RC[-8]>0,
IF(ISERROR(VLOOKUP(G:G,Expedition!F:H,3,0)),R1C22,VLOOKUP(G:G,Expedition!F:H,3,0)),R1C22)
,IF(ISERROR(VLOOKUP(G:G,Expedition!F:H,3,0)),R1C22,VLOOKUP(G:G,Expedition!F:H,3,0)))"
It's quite messy, but it works :) at least in sheet.
The problem is that part of inserted formula in cell is
VLOOKUP(G:(G);Expedition!F:(H);3;0)
instead of
VLOOKUP(G:G;Expedition!F:H;3;0)
if only VLOOKUP is used, it works fine, I have no idea why in this example are those brackets.
Any ideas?
//edited for better understanding
Try to use either R1C1 or A1 reference style and specify the formula type, for example:
Range(Cells(firstm, "U"), Cells(lastm, "U")).FormulaR1C1 = "=IF(RC2=0,IF(RC[-8]>0, IF(ISERROR(VLOOKUP(C[7],Expedition!C[6]:C[8],3,0)),R1C22,VLOOKUP(C[7],Expedition!C[6]:C[8],3,0)),R1C22) ,IF(ISERROR(VLOOKUP(C[7],Expedition!C[6]:C[8],3,0)),R1C22,VLOOKUP(C[7],Expedition!C[6]:C[8],3,0)))"
edit: of course the relative references G:G etc have to be changed to relative references C[7]. C[7] becomes G:G while C7 becomes $G:$G.
Well, it perfectly works.. when I'm using that in half of the code, why haven't I used that for columns?.. :) Thank you!

Find/replace conditionally formatted cells

I've been using a conditional format formula =OR(B2=B1,B2=B3) to highlight consecutive duplicates. I then use format painter to copy the formula to all columns in my excel table.
I have set up a button that will, amongst other things, copy a workbook into a new workbook. I now want to include the above formula in this macro. My final objective is to replace all of the cells found with this formula with an asterisk (*).
I first tried to just pop the formula into the macro as a starting point -
For Each sh In Destwb.Worksheets
With sh.UsedRange.FormatConditions _
.Add(Type:=xlExpression, Formula1:="=OR(B2=B1,B2=B3)")
.Interior.Color = RGB(198, 239, 206)
End With
Next sh
But this just makes a mess of seemingly randomly highlighted cells. I'm not sure where I've gone wrong. Even column B highlights are all wrong. Could the header in B1 affect this? It doesn't when I use the CF normally. How can I expand the CF into all columns uniquely?
Finally, how do I go about working a replacement of formatted cells into this formula? Or is there a quicker/easier way to meet this end-goal?
I didn't realise I could add custom text in that way. That's moved me a good couple of steps forward.
Taking Balinti's suggestion into consideration I've tried a workaround. I was making a couple of assumptions that turned out to be wrong. I have been able to enter the CF into the Array of data that I have in my original workbook. It's not as simple as putting it into a table, but by manually selecting the range in each column and inputting the CF I have made it work.
I also wasn't sure if the formatting would carry through to the new worksheet as I have used the Paste Special command to convert the array formula to values in the new sheet. It does, however, carry the formatting across which is very handy.
It's not the perfect solution for me but it appears to be working so far. I still need to test what happens when I change the date and get updated data. It would still be interesting to know if I can move this formatting into my macro though. Any tips?

#Value! error with =(A1+A2)

I'm trying to add multiple cells with the formula =(A1+A2+A3...etc)
Which works, but if all the cells are empty then I get a #Value!
PLEASE NOTE:
Yes I am aware the proper way to add cell values is with =SUM(A1:A3)
However the cells I'm adding together each have their own functions to get their numbers, and the =SUM function won't add them together.
So! Is there a way I can make =(A1+A2+A3...etc) not give me a #Value! error in the cell that's supposed to total them if ALL the cells (A1,A2,etc) are empty? (as in, the cell with the total will just be blank)
Yes I know this is overly complicated. I'm working with that I've got.
EDIT
I might have figured out my problem. My 'false' statement in the function of the cells that were being added is "" in order to make the cell not have a 0 in it when empty. When it tries to add those cells together, if they all read "" and none are a number that's when I get the #Value! error. Not sure yet what I'm going to do about that...
EDIT 2
Yup. Problem was caused by having a non-numerical value as my false statement. Didn't want a bunch of zeros everywhere, but oh well I guess.
I tried both Excel 2007 and Calc 3.4.1, and neither one of these generated the #Value! that you mention. I am thinking that perhaps your source cells' equations are producing a value that is causing this to error out.
For example, if one of the cells has a String value, then this will be the result. This can be detected with the TYPE() function. for example:
=( IF(TYPE(A1)=1;A1;0) + IF(TYPE(A1)=1;B1;0) + ...)
this will make sure that you are actually adding numbers before the addition takes place.
edit
See: http://www.techonthenet.com/excel/formulas/type.php
for details on TYPE()