VBA Copy Formatting and apply to all tabs

ffionnah

Board Regular
Joined
Jun 12, 2018
Messages
61
Good morning,
I found and adapted this macro to take the formatting from one tab and apply it to any existing tabs. I want to place this in a separate workbook, instead of having to add it into every workbook I apply it to. I can't figure out what to update. Could you please help me to update this macro to apply to the active workbook and all tabs, as opposed to the workbook it resides in?

Thank you so much!

Code:
Sub Wsh_PasteSpecial_Test()
Dim aWshExcluded As Variant, vWshExc As Variant
aWshExcluded = Array("Exclude(1)", "Exclude(2)")
Dim WshSrc As Worksheet
Dim WshTrg As Worksheet


Rem Set Source Worksheet
Set WshSrc = ThisWorkbook.ActiveSheet
Application.ScreenUpdating = 0


    Rem Process All Worksheets
    For Each WshTrg In WshSrc.Parent.Worksheets


        Rem Exclude Worksheet Source
        If WshTrg.Name <> WshSrc.Name Then


            Rem Validate Worksheet vs Exclusion List
            For Each vWshExc In aWshExcluded
                If WshTrg.Name = vWshExc Then GoTo NEXT_WshTrg
            Next


            Rem Process Worksheet Target
            With WshTrg.Cells
                WshSrc.Cells.Copy
                .PasteSpecial Paste:=xlPasteFormats     'Source format is pasted.
                .PasteSpecial Paste:=xlPasteComments    'Comments are pasted.
                .PasteSpecial Paste:=xlPasteValidation  'Validations are pasted.
                Application.CutCopyMode = False
                Application.Goto .Cells(1), 1
    End With: End If:


NEXT_WshTrg:


    Next


Application.Goto WshSrc.Cells(1), 1
Application.ScreenUpdating = 1




End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
.
With the exception of the Array line, there doesn't appear to be anything in the code that pertains to the original workbook.

Have you tried using the code, as is, in the new workbook ? What happens ? If there is an error, what line of code is highlighted
and what does the error code say ?

For what it is worth, I ran the code here without changing anything and did not receive any errors. However, I don't really understand
what the code is supposed to do, so I can't tell if it works correctly. There were no changes noted in the sheet data here.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,592
Members
449,089
Latest member
Motoracer88

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