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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
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,214,788
Messages
6,121,603
Members
449,038
Latest member
Arbind kumar

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