Create Worksheet for each new CC added in a list

a_lexia89

New Member
Joined
Aug 11, 2020
Messages
1
Office Version
  1. 2019
Platform
  1. Windows
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:

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
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of sheets "MD" and "master charge". Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0

Forum statistics

Threads
1,214,405
Messages
6,119,315
Members
448,886
Latest member
GBCTeacher

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