How to merge cells in excel using VBA?

SilentRomance

New Member
Joined
Aug 4, 2021
Messages
46
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
Hi guys,

I am using VBA code to merge a cells below the row that has data.
1628649442086.png



I can't use specific value of the RANGE() because the data will be increased or decreased anytime.

This the code i"m using but when I run the code the macro give me a warning message
1628649709680.png


VBA Code:
Sub Cert()

Dim cer As Long


cer = Cells(Rows.Count, 1).End(xlUp).Row + 1

With Range("cer + 2" & 1 & ",cer + 2" & 6 & "")
.Merge
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With

End Sub

Can you help me about this
I am new in VBA language so sorry for the **** logic.

And also that merged cells have a value like "This all the total of credit".
 
Last edited:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
you don't need the quotes in your Range declaration and if you want to use numbers for rows and columns you need to use the Cells object. Below is your code cleaned up and the example worksheet I created. The gray highlighted cells are the ones that were merged.

VBA Code:
Sub Cert()
Dim cer As Long

cer = Cells(Rows.Count, 1).End(xlUp).Row + 1

With Range(Cells(cer + 2, 1), Cells(cer + 2, 6))
.Merge
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With

End Sub

Book7
ABCDEF
1
2
3
4Any Data
5
6
7
8
9
10
11
Sheet1
 
Upvote 0
Solution
Hi, there are a few mistakes in the code.

First, 'cer' is based on column A, which might be empty in your sample data -- meaning this will always give you a "1", and you will always merge the wrong cells.

Next, the String version of cell addresses might be tough to understand in VBA syntax for beginners, using the Cells function to return using row/column numbers can be less confusing.
VBA Code:
Sub Cert()

    Dim cer As Long

    cer = Cells(Rows.Count, 3).End(xlUp).Row + 1 'now the last row is based on column 3 (C)
    
    With Range(Cells(cer + 2, 3), Cells(cer + 2, 5)) 'get the range of columns 3:5 (C:E), row = cer + 2
        .Merge
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .Value = "This all the total of credit" 'this is to add the value
    End With

End Sub
 
Upvote 0
Further to the remedial posts, I would suggest to @SilentRomance that you consider avoiding using Mergede Cells and VBA as they are prone to issues that may adversely affect the perfornace of the code and / or the worksheet itself
Merged cells can cause all sorts of issues especially when filtering, formulas, etc
Instead of MergedCells try highlighting the desired cells, then using >>Format cells>>Alignment>>Horizontal dropdownBox>>"Center across selection"
 
Upvote 0

Forum statistics

Threads
1,215,236
Messages
6,123,799
Members
449,127
Latest member
Cyko

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