Next Sheet Loop

English Accountant

New Member
Joined
Mar 18, 2021
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I am trying to clear nearly all the tabs on a spreadsheet, but when it gets to the last tab it I get the error message "Object variable or With block variable not set".

I am struggling to find a comparable example so any help would be gratefully received!

This is my VBA code:



VBA Code:
Clear entire workbook
'
' Keyboard Shortcut:
'
Dim ws As Worksheet

Application.Goto Reference:="ClearAll"

For Each ws In ActiveWorkbook.Worksheets

If (ActiveSheet.AutoFilterMode And ActiveSheet.FilterMode) Or ActiveSheet.FilterMode Then
  ActiveSheet.ShowAllData
End If

     'ActiveSheet.ShowAllData

   
    Range("A16:E215").Select
    Selection.ClearContents
    Range("G16:H215").Select
    Selection.ClearContents
    Range("K16:M215").Select
    Selection.ClearContents
    Range("A16").Select

ActiveSheet.Next.Select

Next

End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
is this what you wanted?

VBA Code:
Sub XX()
'
' Keyboard Shortcut:
'
Dim Ws As Worksheet

Application.Goto Reference:="ClearAll"

For Each Ws In ActiveWorkbook.Worksheets

    If (Ws.AutoFilterMode And Ws.FilterMode) Or Ws.FilterMode Then
        Ws.ShowAllData
    End If
    
    'ActiveSheet.ShowAllData
    
    
    Ws.Range("A16:E215").ClearContents
    Ws.Range("G16:H215").ClearContents
    Ws.Range("K16:M215").ClearContents
    
    'ActiveSheet.Next.Select
    
Next

End Sub
 
Upvote 0
Thanks for your reply but no as it should clear one tab and then move to the next tab then clear and the next until the 53rd tab then end but it comes up with the error message.
 
Upvote 0
What exactly is on that tab that it is erroring on?
Do you have any errors on that tab, or perhaps some protected ranges or merged cells?
 
Upvote 0
I think it is closing the "loop" because it is looking for a "next" tab that doesn't exist. "Activesheet.Next.Select" which I to clear down nearly all of the tabs.
 
Upvote 0
I think it is closing the "loop" because it is looking for a "next" tab that doesn't exist. "Activesheet.Next.Select" which I to clear down nearly all of the tabs.
Did you notice that Nemmi commented that line out of the code (commenting a line of code is the same thing as removing it altogether - it is not active)?

By prefacing all the ranges with the "ws" worksheet reference, like Nemmi did, their is no need to change the ActiveSheet.
 
Upvote 0
Thanks, nearly there but there are two tabs that I don't want clearing the ranges specified down and this clears all tabs including information i need to keep.

Apologies, I'm not the best at VBA!
 
Upvote 0
Thanks, nearly there but there are two tabs that I don't want clearing the ranges specified down and this clears all tabs including information i need to keep.

Apologies, I'm not the best at VBA!
OK, that is an important detail you failed to mention before now.

You can simply imbed the ClearContents lines in an IF block that checks to make sure that you are not in one of those two sheets:
VBA Code:
Sub XX()
'
' Keyboard Shortcut:
'
Dim Ws As Worksheet

Application.Goto Reference:="ClearAll"

For Each Ws In ActiveWorkbook.Worksheets

    If (Ws.AutoFilterMode And Ws.FilterMode) Or Ws.FilterMode Then
        Ws.ShowAllData
    End If
   
    If (Ws.Name <> "Sheet1") And (Ws.Name <> "Sheet2") Then
        Ws.Range("A16:E215").ClearContents
        Ws.Range("G16:H215").ClearContents
        Ws.Range("K16:M215").ClearContents
    End If
   
Next Ws

End Sub
Just change "Sheet1" and "Sheet2" with the actual names of your two sheets you want excluded from this clearning.
 
Upvote 0
Solution
You are welcome.
Glad we were able to help and you were able to get code that does what you need.

Typically, you want to try to include as all the pertinent information in your initial question, if possible. That will increase the chances of you getting an answer that works for you quicker (and with less "back-and-forth").

Worst case scenarios, I have seen people drop big bomb shells that change the whole nature of the question in follow-up posts. Sometimes that backfires because the original responder may not know how to help with the expanded question (and now the question no longer shows up on the "Unanswered threads" list, meaning it will get less people seeing it). Or sometimes people oversimplify the question, which can also be a mistake too, because you may get answers that answer the question you asked, but don't actually work for your real problem.

So I usually advise people that to get the best possible help, try to be as detailed as possible in your original post, and don't oversimplify things (unless you are really confident that you can take a simplified response and expand it to what you need yourself).

Just some tips for getting the most out of our board.
Happy Excel-ling!
:)
 
Upvote 0

Forum statistics

Threads
1,215,819
Messages
6,127,045
Members
449,356
Latest member
tstapleton67

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top