VBA AutoFilter hiding all rows - including the ones matching criteria - vba

I'm applying VBA AutoFilter to some results in an excel sheet. It seems to compile properly, but when I check the results, the AutoFilter is hiding both the rows that match and that do not match the criteria I applied.
If I manually select the autofilter that was applied, i see that the criteria that I coded is correctly input and, by just clicking enter, the criteria matching rows show.
I'm using a Brazilian Portuguese version of Excel, not sure if that might be the issue.
Here's what I've tried:
Sub FilterOff(ByVal thisSheet)
thisSheet.Activate
With thisSheet
.AutoFilterMode = False
.Range("A1:F1").AutoFilter
.Range("A1:F1").AutoFilter Field:=4, Criteria1:=">0.01", _
Operator:=xlOr, Criteria2:="<-0.01"
.Range("A1:F1").AutoFilter Field:=5, Criteria1:=">100"
End With
End Sub

I was experiencing something similar in one of my macros. I had a table that I was trying to autofilter. I could do it manually, but not in VBA, even when I was exactly replicating what the recording function gave me. I also could not copy+paste as values in VBA, but I could manually.
What worked for me was to save and close the workbook, then reopen it and apply the autofilter. Specifically, I used this:
tempWb.SaveAs ("dir\temp.xlsx")
tempWb.Close (0)
Set rptWb = Workbooks.Open("dir\temp.xlsx")
Set rptWs = rptWb.Sheets(1)
rptWs.Range(rptWs.Cells(1, 1), rptWs.Cells(lstRow, lstCol)).AutoFilter Field:=20, Criteria1:="=NO RECORD"
and it worked.
Update: I think the underlying issue was that I had calculation set to manual. After I set calculation to automatic, the problems went away.

I did something like this and it worked
Range("A1:B6").AutoFilter
ActiveSheet.Range("$A$1:$B$6").AutoFilter Field:=1, Criteria1:="=10", _
Operator:=xlOr, Criteria2:="=30"
ActiveSheet.Range("$A$1:$B$6").AutoFilter Field:=2, Criteria1:="100"

Related

Autofill in Excel VBA returns error 1004

I am trying to get a raw Excel file into a customized format. I added a picture below, so its easier to explain. I will address the requirements as steps too.
1) I need to get rid off all columns which include "Importo" or "Prezzo"
2) I need to extract the date from the remaining columns (Quantitá). First, I insert an empty row on top and then i apply right(cell,7).
So far, so good.
Then I want to autofill the remaining columns, but i get a 1004 error. In the example code I tried from J:O, but really id need it from J to the last column. I post the code (which works until the last row).
I was actually wondering if Autofilling is best practise here, maybe indexing though would be better?
Sub delete_col()
Dim A As Range
Do
Set A = Rows(1).Find(What:="Importo", LookIn:=xlValues, lookat:=xlPart)
If A Is Nothing Then Exit Do
A.EntireColumn.Delete
Loop
Do
Set A = Rows(1).Find(What:="Prezzo", LookIn:=xlValues, lookat:=xlPart)
If A Is Nothing Then Exit Do
A.EntireColumn.Delete
Loop
Rows("1:1").Select
ActiveCell.EntireRow.Insert
ActiveCell.Range("J1").Select
ActiveCell.FormulaR1C1 = "=RIGHT(R\[1\]C,7)"
Selection.AutoFill Destination:=ActiveCell.Range("J1:O1"), Type:=xlFillDefault
End Sub
I suppose that in the line ActiveCell.FormulaR1C1 = "=RIGHT(R\[1\]C,7)", the \ is a kind of typo, which should be deleted.
Concerning the 1004 error, the easiest way to go around it, while doing AutoFill, is something like this:
Sub TestMe()
Range("A1:O1") = Range("J1")
End Sub
Thus, every value in Range("A1:O1") will be set with the value from Range("J1").
However, your code uses a lot of Select, Activate and ActiveCell. Try to avoid these, because they are not considered good practices in VBA and may lead to different errors. How to avoid using Select in Excel VBA

How to get autofill working in VBA?

I'm trying to get the autofill option to work. For one situation I only need to copy the values in a cell. In the other the formulas (but I guess this works the same as copying a value?).
This selects the cell I want to copy all the way down to the last filled cell in column B
Range("A1048576").End (xlUp)
Just adding the filldown option does not work. Any combination of .Select or .Value doesn't work either.
When I'm trying to simplify it by using this code, the filldown option doesn't work either.
Range("A11").Select
Selection.Filldown
Anybody got an idea?
It's always good if you can use the macro recorder for such tasks and then edit the result to remove the Select and ActiveCell:
Option Explicit
Sub Makro1()
Range("A1").Select
ActiveCell.FormulaR1C1 = "123"
Range("A1").Select
Selection.AutoFill Destination:=Range("A1:A11"), Type:=xlFillDefault
End Sub

Skip code when filters applied and returns no results VBA

I am using the below code to filter data as part of a much larger code however in some cases there may not be any results returned when the filters are applied. Alternatively If no results are returned I would like to skip part of the code.
Can anyone suggest the code I would need to use for this. (I'm guessing it would be an IF statement but unsure)
The code I am currently using is
Worksheets("DC Allocations Input").Activate
ActiveSheet.Unprotect Password:="Projects123"
ActiveSheet.Range("$B$9:$CT$382").AutoFilter Field:=1, Criteria1:="<>"
ActiveSheet.Range("$B$9:$CT$382").AutoFilter Field:=5, Criteria1:="=OPP", Operator:=xlOr, Criteria2:="=RSK"
ActiveSheet.Range("$B$9:$CT$382").AutoFilter Field:=6, Criteria1:="<>"
Any help would be much appreciated.
Thanks
You can use an if statement with special cells visible like this:
Dim lngCnt As Long
On Error GoTo someplace 'you get an error if there are no visible cells, use it to skip to where you want
lngCnt = Range("$b$10:$CT$382").SpecialCells(xlCellTypeVisible).Count 'Note I'm not including the headings row, this will always be visible
On Error GoTo 0
'Put your code to run if there are results from the filter here
someplace:
'And the code you want to resume at here

How to edit a macro in excel 2010 so that the filter criteria is: not equal to blanks?

I'm new to creating macros so my apologies if my question is not very clear. I have recorded a macro do autofilter a spreadsheet. What I need it to do is to not include the blanks in the filter. Considering that each sheet has different values inputted the way the macro is setup right now is based on the first sheet I used.
This is what the code looks like:
Range ("B7:B8) .Select
Selection.AutoFilter
ActiveSheet.Range("$B$7:$N$38").AutoFilter Field:=12, Criteria1:=Array( _
"1.00", "13.00", "2.00", "3.00", "3.50", "42.50", "6.00", "7.00", "Total"), Operator _
:=xlFilterValues
So what I think it should be is changing the Criteria in order for it to not select blanks instead of selecting exact values (that will be different in every sheet).
Does anyone know how to do this? and please let me know if you need any other information.
Thank you.
How to edit a macro in excel 2010 so that the filter criteria is: not equal to blanks?
in continuation to my comment, what if the value is <0?
The best way is to use this
Criteria1:="<>"
FOLLOWUP FROM COMMENTS
Try this
Yourrng.AutoFilter Field:=12, Criteria1:="<>", Criteria2:="<>-"
I did this
Myrng.AutoFilter Field:=12, Criteria1:="<>", Criteria2:="<>0"
For number you can use:
Criteria1:=">0"
For Text you could use:
Criteria1:="=*"

Apply formatting to an Excel file programmatically

I want to format an existing Excel file (xls) cell in such a way that the cell values in a column only show two digits after the decimal.
So instead of 0.090919729581319146%, I want to show 0.09%.
I need to do this across multiple documents, so I need some repeatable way to apply the transformation. I was thinking of a macro - and tried it with the integrated macro recorder in Excel 2010, but unfortunately couldn't get it to work.
I have only to format a Range from C3 --> C5000.
I found something on web. Look at this code. It does what i am talking about:
Sub NurZumUeben()
With Range("C2:C5000")
.Replace What:="%", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows
Range ("K1") = 100
Range ("K1").Copy
.PasteSpecial Paste :=xlPasteAll, Operation:= xlDivide
.NumberFormat = "0.00%"
Range("K1").Clear
End With
End Sub