Set Excel Autofilter Values with VBA - vba

My spreadsheet has an autofilter set to cell A8 that has the options L, B, U and (Blanks) and range A9:A532.
I would like a macro that when it has been run, its selects the B, U and (Blanks) options (or conversely deselects the L option) and therefore the relevant rows are filtered. I've had a look around what everything I have found so far hasn't worked.
Thanks in advance

Thanks for the recorded macro suggestion, for anyone else trying to achieve the same you get the following by using the macro recorder:
ActiveSheet.Range("$A$9:$A$532").AutoFilter Field:=1, Criteria1:=Array("B", _
"U", "="), Operator:=xlFilterValues

Related

VBA Code to copy and paste active column with merged cells inside

I'm trying to copy the active column and paste it next to it but the code selects the entire worksheet because it has merged cells in.
Sub CopyPaste()
Columns(ActiveCell.Column).Selection
Selection.Copy
ActiveCell.Offset(0,1).PasteSpecial Paste:=xlPasteAll
End Sub
Could you please help me adding the missing code to ignore merged cells?
This is yet another reason to avoid using Select in VBA for Excel. Your selection will expand with the merged cells. You can try this:
ActiveCell.EntireColumn.Copy ActiveCell.Offset(0, 1).EntireColumn
And again, you should find some way to avoid counting on the ActiveCell in your code, and use some fully qualified range.

VBA AutoFilter hiding all rows - including the ones matching criteria

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"

Excel VBA - transpose formulas in vertical array to horizontal array

I have googled all sorts of phrases for an answer to my question but I'm having a hard time locating a solution that works. It likely involves combination of a few different solutions, or a method I have yet to think of; so any help would be appreciated.
Say I have formulas in cells A1, A2, A3, and A4. Let's say I want those EXACT formulas moved to the right one column.
In VBA I can say:
Range("B1:B4").Formula = Range("A1:A4").Formula
What I'm looking to do is something like this:
Range("B1:E1").Formula = Range("A1:A4").Formula
See how my B:E range is horizontal verses the vertical range of A1:A4.
I have tried all sorts of transpose options but I can't find any that work because I want the EXACT formula's to transfer.
Any thoughts?
You could try something like:
Sub PivotRangeFormulas()
Dim rngSrc As Range: Set rngSrc = ActiveSheet.Range("A1:A4")
Dim rngTgt As Range: Set rngTgt = ActiveSheet.Range("B1:E1")
Dim i As Long: For i = 1 To rngSrc.Rows.Count
Application.Index(rngTgt, i).Formula = Application.Index(rngSrc, i).Formula
Next i
End Sub
You could also use an Offset function from the first cell in each range
Range("B1:E1").Formula = WorksheetFunction.Transpose(Range("A1:A4").Formula)
Is locking the cell reference inside your formulas possible? I'm sure you are aware, but the F4 key (pc) will toggle referenced cell locks. The dollar sign locks the column letter or row number [A6, $A$6, A$6, $A6]. If you lock your cell references, you can then copy and transpose the formulas.
Range("B1:E1").Copy
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Here is another option: Try recording a macro while entering your formula in the cell. If your formula is:
=SUM(D3:D4)
Depending on where you entered the formula, the VBA output might look like:
"=SUM(R[3]C:R[4]C)"
Here is the absolute reference in VBA:
"=SUM(R3C4:R4C4)"
You could then do something like:
Range("A8:A23").FormulaR1C1 = "=SUM(R3C4:R4C4)"
This will enter the formula =SUM($D$3:$D$4) in all the cells from "A8:A23". If you play with the brackets in the VBA formula, you should be able to make it work. The formula below searches the column to the left of the selected cell(s) containing the formula for the text "nff":
Selection.FormulaR1C1 = _
"=SEARCH(""nff"",RC[-1])"

Copy all rows that match criteria from data set

I've looked all over the net for this and found quite a lot of similar issues, but none of the solutions seem to work for what I want. The dataset is below, and includes about 200 different rows. I am trying to create a macro button that when clicked will check to see if they are in Department A, and then copy all the row to a new sheet and also change page to that sheet.
The aim is to then setup 4 button, for Department A, B, C, D each on different sheets
thanks
Edit:
I don't want to have to select everything then click a button.
I would like just a button with a macro assigned that will check to see if they are in Department A, and if so copy the row over to sheet 2
thank
You can do something like this:
Range("A1").Select
ActiveCell.CurrentRegion.Select
Selection.AutoFilter Field:=1, Criteria1:="a"
Range("A1").Select
ActiveCell.CurrentRegion.Select
Selection.Copy
Sheets("Sheet2").Select
Range("A1").Select
ActiveCell.PasteSpecial
This does rely on your data all being as 1 block so that when you hit CTRL + A it selects it all, else you will have to manually specify your ranges.

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:="=*"