0

I'd like to use a variable as a column for autofilter in vba, I think I'm not getting the sintax right, cause it`s not working:

With ActiveSheet
.AutoFilterMode = False
With .Range("A1:" & LastCol + 1 & "1")
.AutoFilter
.AutoFilter field:=5, Criteria1:="Approved"
.AutoFilter field:=6, Criteria1:="Open"
.AutoFilter field:=LastCol + 1, Criteria1:="1"
End With
End With
7
  • Is LastCol defined? Commented Mar 6, 2017 at 0:45
  • Dim LastCol As Integer With ActiveSheet LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column End With Commented Mar 6, 2017 at 0:46
  • 1
    It needs the letter for that syntax instead of integer. "A1:" & LastCol + 1 & "1" Commented Mar 6, 2017 at 0:49
  • Can you edit your post with the rest of the code? Commented Mar 6, 2017 at 0:56
  • actually, is that a way of getting the letter for the last column? all I have is the number (because the last column can change depending on the sheet. or.. is it a way of substituting the sintax for Range in the AutoFilter sub for a numbered column? Commented Mar 6, 2017 at 1:03

1 Answer 1

1

If there are no blank columns, you can use the CurrentRegion instead (similar to a Ctrl + A in A1)

ActiveSheet.AutoFilterMode = False
With Cells.CurrentRegion
    .AutoFilter 5, "Approved"
    .AutoFilter 6, "Open"
    .AutoFilter .Columns.Count, "1"
End With
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.