HyraxWithAFlamethrower
New Member
- Joined
- Sep 19, 2023
- Messages
- 4
- Office Version
- 365
- 2016
- Platform
- 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:
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.
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.