Hello,
I have the following code which copies a master worksheet with formulas and renames it with the company code and cost center. Everything is happening in the same file, which is a database with cost centers and companies to be charged out. Each time a new cost center is added to the list, the macro should be started to add a new worksheet and rename it accordingly: new CC + company code.
My code is working: copy master worksheet and rename it with the filtered cost center and company code, from a specific list with cost centers.
What is not working and I do not know how to do it: when a worksheet is already created with the same name of cost center & company code, to ignore it, not overwrite, do nothing, and go to the next cost center and copy master sheet and rename with cost center and company code.
My code is:
Thank you,
I hope someone can help me :D
I have the following code which copies a master worksheet with formulas and renames it with the company code and cost center. Everything is happening in the same file, which is a database with cost centers and companies to be charged out. Each time a new cost center is added to the list, the macro should be started to add a new worksheet and rename it accordingly: new CC + company code.
My code is working: copy master worksheet and rename it with the filtered cost center and company code, from a specific list with cost centers.
What is not working and I do not know how to do it: when a worksheet is already created with the same name of cost center & company code, to ignore it, not overwrite, do nothing, and go to the next cost center and copy master sheet and rename with cost center and company code.
My code is:
VBA Code:
Sub CreateChargingWorksheets()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Dim CC As Range
ThisWorkbook.Activate
'ThisWorkbook.Worksheets("master charge").Cells(11, 2).Select
Dim ws As Worksheet
Set wh = Worksheets(ActiveSheet.Name)
Worksheets("MD").Activate - this is the list with cost centers which will be monthly updated and new cost centers will be added
Worksheets("MD").Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
For Each CC In Selection
Worksheets("master charge").Cells(11, 2) = CC.Text - this is reffering to the master file where the formulas are, to calculate the costs for that CC
Worksheets("master charge").Copy after:=Worksheets(Sheets.Count)
ActiveSheet.Shapes.Range(Array("Picture 1")).Select - since it is copying the master file, where the macro button is, i want that to be removed from the worksheets which is multipling
Selection.Delete
If Worksheets("master charge").Range("B11").Value <> "" Then
ActiveSheet.Name = wh.Range("B10").Value & " " & wh.Range("B11").Value - i believe in here i should add something for when the sheet for one cost center is already created, to ignore that and move on to the next cost center from the MD sheet
End If
wh.Activate
Next CC
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "Charge worksheets are created."
End Sub
Thank you,
I hope someone can help me :D