Unable to merge cells

briannnnleong

New Member
Joined
May 17, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I am intending to merge every cells between the two objects "FirstCell" and "SecondCell". Still a relatively new user to vba, hence not really sure what went wrong. I have also declared "EmptyRow" in a previous function.

Thank you!!!
 

Attachments

  • Capture.PNG
    Capture.PNG
    7.4 KB · Views: 7

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
What you were trying to align here? The syntax is not correct. Should be something like
VBA Code:
Sub MergeCells()

Dim FirstCell As Range
Dim SecondCell As Range

Set FirstCell = Cells(EmptyRow, 2)
Set SecondCell = Cells(EmptyRow + 8, 2)

With Range(FirstCell, SecondCell)
    .Merge
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
End With

End Sub

You have
xlLeft
xlRight
xlJustify
xlCenterAcrossSelection
 
Upvote 0
What you were trying to align here? The syntax is not correct. Should be something like
VBA Code:
Sub MergeCells()

Dim FirstCell As Range
Dim SecondCell As Range

Set FirstCell = Cells(EmptyRow, 2)
Set SecondCell = Cells(EmptyRow + 8, 2)

With Range(FirstCell, SecondCell)
    .Merge
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
End With

End Sub

You have
xlLeft
xlRight
xlJustify
xlCenterAcrossSelection
Thanks for your help but I'll have an error saying "Application-defined or object-defined error" on the statement of set firstcell = ...
 
Upvote 0
If you hover over "emptyrow" what does it show ?
Where is emptyrow declared ?
Is it a public declaration ?
 
Upvote 0
In your sub MergeCells(), EmptyRow is not defined. Where is this EmptyRow defined? Unless you declare as Public variable, you need to pass this value into subroutine
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,393
Members
449,081
Latest member
JAMES KECULAH

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