Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

create a new sheet with the headers and borders every time

0

I  design  macro  to  create  the  headers  and  borders   so  far  it's  ok.  but when  I  add  some  lines  to  add  a new  sheet   every  time.  it  gives  error . 

so  what  I  want   every  time  run  the  macro  should  create  a new  sheet  with  create  the  headers and  borders  .

Sub InsertHeaders()
Dim ws As Worksheet
Set ws = Sheets.Add
With Sheets("ws")
   .Cells(1).Resize(1, 6).Value = Array("Fiscal Year", "Month", "Month_Year", "Project", "Local Expense", "Base Expense")
   .Range("A1:f12").Borders.Weight = xlMedium
   .Range("A1:f12").HorizontalAlignment = xlCenter
   .Cells(1).Resize(1, 6).Interior.ColorIndex = 53
   .Cells(1).Resize(1, 6).Font.Bold = True
   End With
End Sub

thanks  in advance 

Answer
Discuss

Answers

0
Selected Answer

Leap

You added a sheet but didn't name it "ws" so the With selection didn't work.

Try this instead, noting that I made the header font white (for better contrast) and used autofit once the headers where in place (these changes in bold):

Sub InsertHeaders()
Dim ws As Worksheet
Set ws = Sheets.Add
With ws
    .Range("A1:f12").Borders.Weight = xlMedium
    .Range("A1:f12").HorizontalAlignment = xlCenter
    With .Cells(1).Resize(1, 6)
        .Value = Array("Fiscal Year", "Month", "Month_Year", "Project", "Local Expense", "Base Expense")
        .Interior.ColorIndex = 53
        .Font.Bold = True
        .Font.Color = vbWhite
        .EntireColumn.AutoFit
    End With
End With
End Sub

If you want EVERY new sheet in that workbook to have that format, use the Workbook event NewSheet instead. At the ThisWorkbook level, add this code (changes from above in bold) then every time you press the + button (to the right of the the sheet tabs) a sheet like this will appear after the existing ones:

Private Sub Workbook_NewSheet(ByVal Sh As Object)

With Sh
    .Move After:=Sheets(Sheets.Count)
    .Range("A1:f12").Borders.Weight = xlMedium
    .Range("A1:f12").HorizontalAlignment = xlCenter
    With .Cells(1).Resize(1, 6)
        .Value = Array("Fiscal Year", "Month", "Month_Year", "Project", "Local Expense", "Base Expense")
        .Interior.ColorIndex = 53
        .Font.Bold = True
        .Font.Color = vbWhite
        .EntireColumn.AutoFit
    End With
End With
End Sub

Hope this works for you.

Discuss

Discussion

John
the  code   in workbook and  your  formatting  are  great  . actually  I  like  it .
thanks  so  much .
leap (rep: 46) Jul 11, '21 at 2:16 pm
Glad you like my suggestions! 

Happy to help and thanks for selecting my answer Leap 
John_Ru (rep: 6152) Jul 11, '21 at 3:27 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login