0

I was using autofilter (not in VBA) menus to filter the items in my warehouse, and the shelves they were on.
When I filtered out everything but the shelves I wanted to add inventory to, the values I added (through a VBA programmed button that basically copies everything in the "add to stock" (AKA "C4:C1000" row and adds it into the "currently in stock" row (AKA "D1:d1000")) got added to the wrong row.

My solution was to use the autofilters to find the correct shelf, write the amount added to the stock, and then press the button.
The VBA code of the button would (in my new plan) then do exactly as before, only this time it would first remove the filters, execute as before, and then re-apply filters.

How do I turn the autofilters back on with VBA?

The closest I can find to what I want is the following:

Activesheet.range("a4").autofilter

That does nothing but stop my code from completing its execution mid-way.

The full code for one of the pages:

Sub AddtoInnTotalandclear()
'The macro is used to move all amounts plottet into "INN" colum. Amounts are moved into "TOTAL AMOUNT IN STORAGE" while clearing the "INN" colum simultaneously

'Removes flickering from the screen (part 1 of 2)
Application.ScreenUpdating = False

'removes the protection on the worksheet (NB! you have to have the current password in the code line for this to work)
ActiveSheet.Unprotect Password:="kirk"

'Copies the values from the "inn" colum
Range("c4:c1000").Copy

'Adds the copied values to the values already in the "Total Amount" colum
Range("d4:d1000").PasteSpecial Operation:=xlPasteSpecialOperationAdd

'Clears the "inn" colum
Range("c4:c1000").ClearContents

'Disable marchiing atnsa around copied range
Application.CutCopyMode = False

Range("d4:d1000").Select

Selection.Locked = True

'Allows autofilter usage despite the document being locked 
'&
'Re-Activates the password protection
With ActiveSheet
    .Protect Password:="kirk", AllowFiltering:=True
    .EnableSelection = xlNoRestrictions
End With

'Determines where you end up when you are finished
Worksheets("in").Range("c4").Select

'Removes flickering from the screen (part 2 of 2)
Application.ScreenUpdating = True

End Sub

I want to remove/disable autofilters when I press the button that activates this VBA code, then re-activate the autofilters once the entire procedure is done.

4
  • It is unclear what you did. Did you remove AutoFilter from the worksheet completely or did you just ShowAllData on all filtered columns?? Dec 18, 2020 at 17:49
  • I went into VBA and added the following code to the sub. Activesheet.autofiltermode=false Dec 18, 2020 at 18:01
  • 1
    If you're having a problem with your code then it's always a good idea to include it in your question. Dec 18, 2020 at 18:08
  • Tim: Duely noted.. I have added the full code. Dec 20, 2020 at 7:00

1 Answer 1

1

Here is a demo example. Say we have data in cols A through D with the headers in row#1. If you run:

Sub qwerty()
    Dim s As Worksheet
    Set s = ActiveSheet
    
    s.AutoFilterMode = False
    s.Range("A:D").AutoFilter
End Sub

You will end up with filtering on cols A through D, but with no criteria on any of those columns applied.

EDIT#1:

If your header row is row #3 and we are filtering cols A through D, then:

Sub qwerty2()
    Dim s As Worksheet, N As Long
    Set s = ActiveSheet
    
    N = Cells(Rows.Count, "A").End(xlUp).Row
    s.AutoFilterMode = False
    s.Range("A3:D" & N).AutoFilter
End Sub
7
  • Hello Gary, and thanks for taking the time. I already found the code you describe online and though the following code works: activesheet.autofiltermode= False the following does not: activesheet.range("A3:D3") The data starts on A3, B3 etc all the way to H3. The code won't work though. Dec 18, 2020 at 18:27
  • @John123456789 Does the data start at A3 or the column A header start at A3??? Dec 18, 2020 at 18:33
  • The error messege I get is "run-time error 424 object required" Dec 18, 2020 at 18:35
  • I stand corected gary.. the header starts on A3, the actual data is in A4 and B4 etc. I also tried simply writing it exactly as you did "...Range("A:H") but no dice. Dec 18, 2020 at 18:36
  • @John123456789 See my EDIT#1 Dec 18, 2020 at 19:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.