Copy all tab colors from one workbook to another

Joined
Sep 19, 2023
Messages
4
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi, all,
I've used Excel for years and am pretty strong with formulas, but am only now getting into VBA. My problem is that my company has workbooks that are inundated with named formulas, so many that even macros to delete them crash the workbook rather than help. The only solution I've come across is remaking the workbook, which is tedious, so I decided to make a macro for it. Between searches for the bits I needed and using what I've learned so far, I have the below:
VBA Code:
Option Explicit

Sub NewWBandPasteSpecialALLSheets()
   Dim wb As Workbook
   Dim wbNew As Workbook
   Dim sh As Worksheet
   Dim shNew As Worksheet
   Dim ReplacementText As String
   Dim rCell As Range
  
   Set wb = ActiveWorkbook
   Workbooks.Add ' Open a new workbook
   Set wbNew = ActiveWorkbook

   On Error Resume Next

   For Each sh In wb.Worksheets
     

      'add new sheet into new workbook with the same name
      With wbNew.Worksheets

          Set shNew = Nothing
          Set shNew = .Item(sh.Name)

          If shNew Is Nothing Then
              .Add After:=.Item(.Count)
              .Item(.Count).Name = sh.Name
              Set shNew = .Item(.Count)
          End If
      End With
     sh.Cells.Copy shNew.Range("A1")
         
     Next
    For Each shNew In wbNew.Worksheets
   
    shNew.Tab.Color.RGB = sh.Tab.Color
    For Each rCell In shNew.UsedRange
    If InStr(rCell.Formula, wb.Name) > 0 Then
                rCell.Replace What:="[" & wb.Name & "]", Replacement:=""
            End If
    Next
    Next
    Application.DisplayAlerts = False
    wbNew.Sheets("Sheet1").Delete
    Application.DisplayAlerts = True
End Sub

It works for the most part, making new sheets and naming them the same, copying everything over, removing references to the old workbook, and deleting the Sheet1 from the new workbook, but for the life of me, I can't seem to get it to copy the tab colors over. It's a relatively minor problem, but surely there's a way to do this.

I also have a couple other questions:
1. Will the above take care of charts, tables, and other objects as well, or will it just do data? By take care of, I mean not only copying, but removing the reference to the old workbook as well.
2. That old phrase, "You don't know what you don't know" applies here. The new workbook must be like the old one in all regards, including hidden tabs (current plan is to just manually hide them, but assume there's a better way), save for the named formulas. Is there anything else I'm missing?

Thanks in advance for your help and please let me know if anything needs clarity or if the code can be made more elegant.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi, thanks for your reply. That was actually my first attempt, but it doesn't seem to work. I don't know if the command is in the wrong place or something. Also tried it with .RGB appended to both ends and with it only on the right side. No joy.
 
Upvote 0
Update to original post: I have figured out how to automatically hide any tabs in the new workbook that were hidden in the old. Progress!
 
Upvote 0

Forum statistics

Threads
1,215,089
Messages
6,123,058
Members
449,091
Latest member
ikke

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