Clear Contents from Specific Hidden Tabs

rkeen0822

New Member
Joined
Sep 12, 2010
Messages
11
Office Version
  1. 2021
  2. 2019
  3. 2016
Platform
  1. Windows
Good Morning,
Looking to shorten up the code to clear out the contents from about 90 tabs. Tab Names are numbered for each data pull (i.e. Hour 1, Hour 2, Hour 3, Line 1, Line 2, Line 3, Item 1, Item 2, Item 3 ....etc). Each category goes to 13.

I don't want to use the easy "Clear from all Hidden" as there are some hidden tabs that are meant for calculations that I don't want to clear. Any assistance and advice would be great.

Thanks in advance.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi @rkeen0822. Thanks for posting on the forum.

I can see that the names of your categories have a space and then the number.
1681669396438.png


So with the following code we check only the left part of the sheet name.
It only captures the categories in the Array and the Range of cells to Clear.

If it's all cells then change this:
VBA Code:
sh.Range("A2:C4").ClearContents
to this:
VBA Code:
sh.Cells.Clear


Try this code
:
VBA Code:
Sub ClearContentsSpecificHiddenTabs()
  Dim sh As Worksheet
  Dim cats As Variant
  Dim sName As String
  
  cats = Array("Hour", "Line", "item", "etc")   'Here the names of the categories
  
  For Each sh In Sheets
    sName = "|" & Split(sh.Name, " ")(0) & "|"
    If InStr(1, "|" & Join(cats, "|") & "|", sName, vbTextCompare) > 0 Then
      sh.Range("A2:C4").ClearContents           'Fit the range of cells to clear
    End If
  Next
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Upvote 0
Solution
Hey Dante,

thanks for the reply. this cleaned up well and will be adding this to my code bank for clearing content.

Thanks
 
Upvote 1

Forum statistics

Threads
1,215,051
Messages
6,122,872
Members
449,097
Latest member
dbomb1414

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