Formatting of worksheets newly created by VBA.

tbablue

Active Member
Joined
Apr 29, 2007
Messages
488
Office Version
  1. 365
Platform
  1. Windows
Sub CreateWorksheets(Names_Of_Sheets As Range)
Dim No_Of_Sheets_to_be_Added As Integer
Dim Sheet_Name As String
Dim i As Integer
No_Of_Sheets_to_be_Added = Names_Of_Sheets.Rows.Count
For i = 1 To No_Of_Sheets_to_be_Added
Sheet_Name = Names_Of_Sheets.Cells(i, 1).Value
'Only add sheet if it doesn't exist already and the name is longer than zero characters
If (Sheet_Exists(Sheet_Name) = False) And (Sheet_Name <> "") Then
Worksheets.Add().Name = Sheet_Name

Sheets("sample detail sheet").Select
Selection.Copy
Sheets("sample").Select
ActiveSheet.Paste

End If
Next i
End Sub

After help from this forum my current code is above. I can create new sheets and name them - as per the items in specified range C4:C13. So far so good.

But now I want to apply the same formatting to all my newly created sheets - and to this end thought it easier to copy and paste my formatting from a template sheet', "sample detail sheet".

The code in red is from the macro recorder but I'm struggling to implement it in the way I want; I can't figure out how to paste to anything but the active sheet - I need to paste the selected formatting to all my new sheets.

Any help welcomed.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Sub CreateWorksheets(Names_Of_Sheets As Range)
Dim No_Of_Sheets_to_be_Added As Integer
Dim Sheet_Name As String
Dim i As Integer
No_Of_Sheets_to_be_Added = Names_Of_Sheets.Rows.Count
For i = 1 To No_Of_Sheets_to_be_Added
Sheet_Name = Names_Of_Sheets.Cells(i, 1).Value
'Only add sheet if it doesn't exist already and the name is longer than zero characters
If (Sheet_Exists(Sheet_Name) = False) And (Sheet_Name <> "") Then
Worksheets.Add().Name = Sheet_Name

Sheets("sample detail sheet").Select
Selection.Copy
Sheets("sample").Select
ActiveSheet.Paste

End If
Next i
End Sub

After help from this forum my current code is above. I can create new sheets and name them - as per the items in specified range C4:C13. So far so good.

But now I want to apply the same formatting to all my newly created sheets - and to this end thought it easier to copy and paste my formatting from a template sheet', "sample detail sheet".

The code in red is from the macro recorder but I'm struggling to implement it in the way I want; I can't figure out how to paste to anything but the active sheet - I need to paste the selected formatting to all my new sheets.

Any help welcomed.

Maybe like this?

Code:
    Sheets("Sample detail sheet").Cells.Copy
    Sheets("sample").Cells.Select
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,874
Members
452,949
Latest member
Dupuhini

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