Rrave26
asked on
VB Autofilter Question
My apologies for my very rough VB code here but I need some help with turning the auto filter on. I have created this script to format a report and turn on the auto filter so the managers can quickly select their employees so they can perform an audit. I have pasted the code here, again my apologies for the newbie code here as it is a combination of macro recording and my coding.
Range("A:A,I:I,M:M").Selec t
Range("M1").Activate
Selection.NumberFormat = "mm/dd/yyyy h:mm:ss"
Rows("1:1").Select
ActiveCell.EntireRow.Inser t
Range("A1").Select
ActiveCell.FormulaR1C1 = "Date Requested"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Account Name"
Range("C1").Select
ActiveCell.FormulaR1C1 = "System Name"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Request #"
Range("E1").Select
ActiveCell.FormulaR1C1 = "Requested By"
Range("F1").Select
ActiveCell.FormulaR1C1 = "Approved By"
Range("G1").Select
ActiveCell.FormulaR1C1 = "Ticket Number"
Range("H1").Select
ActiveCell.FormulaR1C1 = "Approval Comments"
Range("J1").Select
ActiveCell.FormulaR1C1 = "Ticket System"
Range("K1").Select
ActiveCell.FormulaR1C1 = "Ticket Number"
Range("L1").Select
ActiveCell.FormulaR1C1 = "Early Expire Reason"
Range("M1").Select
ActiveCell.FormulaR1C1 = "Change Date/Time"
Range("N1").Select
ActiveCell.FormulaR1C1 = "Change Status"
Columns("B:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLe ftOrAbove
Columns("L:L").Select
Selection.Cut Destination:=Columns("B:B" )
Columns("G:G").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLe ftOrAbove
Columns("G:G").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLe ftOrAbove
Columns("F:F").Select
Selection.TextToColumns Destination:=Range("F1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuo te, ConsecutiveDelimiter:=Fals e, Tab:=False, _
Semicolon:=False, Comma:=True, Space:=False, Other:=True, OtherChar:= _
"(", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1)), _
TrailingMinusNumbers:=True
Columns("F:F").Select
Selection.NumberFormat = "General"
Range("G1").Select
Selection.NumberFormat = "General"
ActiveCell.FormulaR1C1 = "Last Name"
Range("H1").Select
Selection.NumberFormat = "General"
ActiveCell.FormulaR1C1 = "First Name"
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLe ftOrAbove
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLe ftOrAbove
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLe ftOrAbove
Columns("I:K").Select
Selection.Cut Destination:=Columns("C:E" )
Columns("I:K").Select
Selection.Delete Shift:=xlToLeft
Columns("B:B").EntireColum n.AutoFit
Columns("C:C").EntireColum n.AutoFit
Columns("D:D").EntireColum n.AutoFit
Columns("E:E").EntireColum n.AutoFit
Columns("F:F").EntireColum n.AutoFit
Columns("G:G").EntireColum n.AutoFit
Columns("H:H").EntireColum n.AutoFit
Columns("I:K").EntireColum n.AutoFit
Columns("Q:Q").EntireColum n.AutoFit
Columns("P:P").EntireColum n.AutoFit
Columns("O:O").EntireColum n.AutoFit
Columns("N:N").EntireColum n.AutoFit
Columns("M:M").EntireColum n.AutoFit
Range("J1").Select
ActiveCell.FormulaR1C1 = "Request Reason"
Range("J2").Select
Cells.Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("A1").Select
Selection.autofilter
End Sub
My issue is with Range("A1").Select and Selection.autofilter. I would expect the autoflter to be placed in the row "A1", but when I run the code it shows up in row "A2". Any ideas as to why?
Range("A:A,I:I,M:M").Selec
Range("M1").Activate
Selection.NumberFormat = "mm/dd/yyyy h:mm:ss"
Rows("1:1").Select
ActiveCell.EntireRow.Inser
Range("A1").Select
ActiveCell.FormulaR1C1 = "Date Requested"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Account Name"
Range("C1").Select
ActiveCell.FormulaR1C1 = "System Name"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Request #"
Range("E1").Select
ActiveCell.FormulaR1C1 = "Requested By"
Range("F1").Select
ActiveCell.FormulaR1C1 = "Approved By"
Range("G1").Select
ActiveCell.FormulaR1C1 = "Ticket Number"
Range("H1").Select
ActiveCell.FormulaR1C1 = "Approval Comments"
Range("J1").Select
ActiveCell.FormulaR1C1 = "Ticket System"
Range("K1").Select
ActiveCell.FormulaR1C1 = "Ticket Number"
Range("L1").Select
ActiveCell.FormulaR1C1 = "Early Expire Reason"
Range("M1").Select
ActiveCell.FormulaR1C1 = "Change Date/Time"
Range("N1").Select
ActiveCell.FormulaR1C1 = "Change Status"
Columns("B:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLe
Columns("L:L").Select
Selection.Cut Destination:=Columns("B:B"
Columns("G:G").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLe
Columns("G:G").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLe
Columns("F:F").Select
Selection.TextToColumns Destination:=Range("F1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuo
Semicolon:=False, Comma:=True, Space:=False, Other:=True, OtherChar:= _
"(", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1)), _
TrailingMinusNumbers:=True
Columns("F:F").Select
Selection.NumberFormat = "General"
Range("G1").Select
Selection.NumberFormat = "General"
ActiveCell.FormulaR1C1 = "Last Name"
Range("H1").Select
Selection.NumberFormat = "General"
ActiveCell.FormulaR1C1 = "First Name"
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLe
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLe
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLe
Columns("I:K").Select
Selection.Cut Destination:=Columns("C:E"
Columns("I:K").Select
Selection.Delete Shift:=xlToLeft
Columns("B:B").EntireColum
Columns("C:C").EntireColum
Columns("D:D").EntireColum
Columns("E:E").EntireColum
Columns("F:F").EntireColum
Columns("G:G").EntireColum
Columns("H:H").EntireColum
Columns("I:K").EntireColum
Columns("Q:Q").EntireColum
Columns("P:P").EntireColum
Columns("O:O").EntireColum
Columns("N:N").EntireColum
Columns("M:M").EntireColum
Range("J1").Select
ActiveCell.FormulaR1C1 = "Request Reason"
Range("J2").Select
Cells.Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("A1").Select
Selection.autofilter
End Sub
My issue is with Range("A1").Select and Selection.autofilter. I would expect the autoflter to be placed in the row "A1", but when I run the code it shows up in row "A2". Any ideas as to why?
ASKER
I just re ran it and it puts the filter in row A2. I have attached the file for troubleshooting.
DlyReleaseActivity-20140903.csv
DlyReleaseActivity-20140903.csv
I didn't see it either.
Otherwise, try this also:
combine the last range/selection to one line:
Range("A1").AutoFilter
Otherwise, try this also:
combine the last range/selection to one line:
Range("A1").AutoFilter
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the suggestion Kimputer but it still puts it in Row "A2".
ASKER
I see, thanks Phil. Let me fix that and rerun it.
A couple of reduced sections of script:
For the section inserting the headers you can go through a loop with a variable for the column number:
Also the section for Autofit can be shrunk to one line:
Thanks
Rob H
For the section inserting the headers you can go through a loop with a variable for the column number:
For C = 1 to 19
Hdr = CHOOSE(C,"Date Requested", "Account Name", "System Name", "Request #", "Requested By", "Approved By", "Ticket Number", "Approval Comments", "Ticket System", "Ticket Number", "Early Expire Reason", "Change Date/Time")
Cells(1,C) = Hdr
Next C
However you then change J1 to "Request Reason". Change "Ticket System" in the line above is this is required.Also the section for Autofit can be shrunk to one line:
Columns("B:M").EntireColumn.AutoFit
Thanks
Rob H
I missed the comment about column I not having a header and might have messed up on the copy and paste to get the headers listed but no doubt you see the gist. For column I just insert "{space}" in the relevant place.
Thanks
Rob H
Thanks
Rob H
ASKER
Phils catch resolved my issue.
I would be surprised if it shows up in A2, since you have inserted an empty row there.
Maybe you should post the spreadsheet prior to running the code, so we can look at it.