Excel VBA to Merge and Center specific Range of cells after inserting a new sheet

astrid22

New Member
Joined
Mar 3, 2018
Messages
10
Hi, I'm relatively new to VBA. I have the below code to insert a new sheet and some other formatting that I need. What I am trying to do is merge and center cells A1:C1 after inserting the new sheet. And then insert a phrase in the merged cells.
Code:
Sub GenerateConf()
  Dim ws As Worksheet
  Dim NameCount As Long
  Dim NameBase As String
  
  NameBase = Format(Date, "mm.dd.yyyy OA ")
  For Each ws In Worksheets
    If ws.Name Like NameBase & "#*" Then NameCount = NameCount + 1
  Next ws
  Sheets.Add(After:=Sheets(Sheets.Count)).Name = NameBase & NameCount + 1
  Sheets(Sheets.Count).Tab.ColorIndex = 50
  End Sub

I tried to use the below code to merge and center A1:C1 but I am getting an error:
Code:
With Worksheet.Range("FirstCell:LastCell")
  .HorizontalAlignment = xlCenter
 .VerticalAlignment = xlCenter
 .Merge
End With

Any ideas?
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi, I have solved this. Thank you.
Code:
Sub GenerateConf()
  Dim ws As Worksheet
  Dim NameCount As Long
  Dim NameBase As String
  
  NameBase = Format(Date, "mm.dd.yyyy OA ")
  For Each ws In Worksheets
    If ws.Name Like NameBase & "#*" Then NameCount = NameCount + 1
  Next ws
  Sheets.Add(After:=Sheets(Sheets.Count)).Name = NameBase & NameCount + 1
  Sheets(Sheets.Count).Tab.ColorIndex = 50
  
  Range("A1:C1").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.Merge
    Range("A2:C2").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
  
End Sub
[\CODE]
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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