Formatting each sheet in new workbooks with vba code

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have code that creates a workbook if it doesn't exist. I then want to format the worksheets but with my code I get the error, object doesn't support this property or method. The line that is highlighted is in SetupSheets:
VBA Code:
For Each Worksheet In ThisWorkbook


VBA Code:
Sub AddYP()
Application.DisplayAlerts = False
Dim newyp As String
    newyp = Tracker.Cells(5, 4)
    YP.Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = newyp
        Call CreateWB(newyp)
    ThisWorkbook.Names.Add Name:="tblYPNames", _
    RefersTo:=Range("tblYPNames").Resize(Range("tblYPNames").Rows.Count + 1)
        Tracker.cboYP.ListFillRange = "tblYPNames"
        Tracker.cboDeleteYP.ListFillRange = "tblYPNames"
Application.DisplayAlerts = True
End Sub


Sub CreateWB(newyp As String)
Dim V
        CheckFolderExists
        Workbooks.Add.SaveAs ThisWorkbook.Path & "\Young People\" & newyp, 52
    For Each V In Split("7 8 9 10 11 12 1 2 3 4 5 6")
        Sheets.Add(, Sheets(Sheets.Count)).Name = MonthName(V)
    Next
    Call SetupSheets
    Sheets("sheet1").Delete
ActiveWorkbook.Close savechanges:=True
Tracker.Cells(5, 4).Clear
End Sub

Sub SetupSheets()
Dim Worksheet As Worksheet
    For Each Worksheet In ThisWorkbook
        Range("A1").Value = "Date"
        Range("A2").Value = "GL Code"
        Range("A3").Value = "Supplier"
        Range("A4").Value = "Amount"
        Range("A5").Value = "Comments"
        With Rows(1)
            .Font.Bold = True
            .Interior.ColorIndex = 15
        End With
        
    Next
End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
VBA Code:
Sub SetupSheets()
Dim ws As Worksheet
    For Each ws In Worksheets
        With ws
            .Range("A3").Value = "Date"
            .Range("B3").Value = "GL Code"
            .Range("C3").Value = "Supplier"
            .Range("D3").Value = "Amount"
            .Range("E3").Value = "Comments"
            With ws.Rows(3)
                 .Font.Bold = True
                 .Interior.ColorIndex = 15
            End With
            With .Range("A1")
                .Value = ws.Name
                .Font.Size = 16
            End With
           . Range("A:A,B:B,D:D").ColumnWidth = 15
            .Range("C:C").ColumnWidth = 30
            .Range("E:E").ColumnWidth = 60
        End With
    Next ws
End Sub
 
Upvote 0
Thanks Michael. I now need to add a total cell. What have I done wrong with my code as it says there is a syntax error with my formula to add column D from D4 to the bottom.

VBA Code:
Sub SetupSheets()
Dim ws As Worksheet, LastRow As Long
    For Each ws In Worksheets
        With ws
            .Range("A3").Value = "Date"
            .Range("B3").Value = "GL Code"
            .Range("C3").Value = "Supplier"
            .Range("D3").Value = "Amount"
            .Range("E3").Value = "Comments"
            With ws.Rows(3)
                 .Font.Bold = True
                 .Interior.ColorIndex = 15
            End With
            With .Range("A1")
                .Value = ws.Name
                .Font.Size = 16
            End With
            .Range("A:A,B:B,D:D").ColumnWidth = 15
            .Range("C:C").ColumnWidth = 30
            .Range("E:E").ColumnWidth = 60
            With .Range("I10")
                .Value = "Monthly Total"
                .Font.Bold = True
                .Font.Size = 16
            End With
            LastRow = .Range("F4").End(xlUp).Row
            .Range("I11").formula="=sum(D4:D & Lastrow & ")"
        End With
    Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,491
Messages
6,125,109
Members
449,205
Latest member
ralemanygarcia

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