Merging Cells using VBA

Frankf1970

New member
Joined
Aug 16, 2018
Messages
1
Reaction score
0
Points
0
Excel Version(s)
Office 2016 for Mac
Hello,

I’m relatively new to Excel VBA and I need some help. I’m trying to figure out how to merge selected cells using VBA. I can do it by including a cell range in my code, but what I want to do is allow a user to select a range of cells, they would then click on a button to merge these cells. They could then select a different range of cells, they would then click the button and this would merge this new range. So lets say User1 selects say A1:A10, they would then be able to click a button and these cells would be merged. User2 then comes in and selects say C5:C20, they would then click a button and these cells would be merged and so on. Hopefully I’ve explained this well that someone understands.

Many thanks

Frank
 
Try using an input box, something along the lines of
Code:
Sub testing()
    Dim rng As Range
    
    Set rng = Application.InputBox("Select your desired range with the mouse.", "YOUR RANGE", Type:=8)
    
    If rng Is Nothing Then
        MsgBox "You haven't selected a range"
    Else
        MsgBox "The range you selected is  " & rng.Address(0, 0)
    End If

End Sub
 
To merge the cells too, add after (or change from):
Code:
MsgBox "The range you selected is  " & rng.Address(0, 0)
the line:
Code:
rng.MergeCells = True

But to merge what's currently selected then assign this to a button:
Code:
Sub testing2()
  Selection.MergeCells = True
End Sub
 
Last edited:
Back
Top