Using same macro in a copied worksheet

dmjexcel

New Member
Joined
Nov 29, 2016
Messages
6
Hello,

I am very very new to using macros. I currently have a macro set up (I used the record macro function, like I said, very new) to hide all values in a table that equal 0 and another macro to unhide the data. I would like to be able to copy this worksheet into the same workbook and have the macros work for that new worksheet. Is there a way to code the original macro to allow this? Currently I get an error on the copied worksheet and the only way I have been able to get around is to record the macro again in the new worksheet. This specific workbook could have up to 15 of the same tables in it, I would really like to avoid having to rerecord the macros that many times.

Here are the two current macros' codes:

Sub Hide()
'
' Hide Macro
'


'
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=6, Criteria1:= _
"<>0", Operator:=xlAnd
End Sub

Sub Unhide()
'
' Unhide Macro
'


'
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=6
End Sub

Thank you!
 
My Aswer Is This,

That worked perfectly! Thank you so much!
Does it work on a copied sheet? I ask because Excel doesn't always assign consecutive numbers to tables on copied sheets.
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
JoeMo I wasn't able to get yours to work. When I went into the debugger it said that Lob = nothing. I wasn't sure how to fix it.
My error, try this revision.
Code:
Sub Hide()
Dim sht As Worksheet, Lob As ListObject
For Each sht In Worksheets
    For Each Lob In sht.ListObjects
        Lob.Range.AutoFilter Field:=6, Criteria1:= _
        "<>0", Operator:=xlAnd
    Next Lob
Next sht
End Sub
 
Upvote 0
Glad too see you have what you want but I never saw in your original post where you said I want a script that will perform this same script on all my sheets at the same time.
 
Upvote 0

Forum statistics

Threads
1,216,446
Messages
6,130,699
Members
449,586
Latest member
spg5150

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