Results 1 to 3 of 3

Thread: Merging Cells using VBA

  1. #1
    Neophyte Frankf1970's Avatar
    Join Date
    Aug 2018
    Posts
    1
    Articles
    0
    Excel Version
    Office 2016 for Mac

    Merging Cells using VBA



    Register for a FREE account, and/
    or Log in to avoid these ads!

    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

  2. #2
    Magician NoS's Avatar
    Join Date
    Jan 2013
    Location
    British Columbia
    Posts
    769
    Articles
    0
    Excel Version
    Excel 2010 64bit
    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

  3. #3
    Super Moderator p45cal's Avatar
    Join Date
    Dec 2012
    Posts
    1,595
    Articles
    0
    Excel Version
    365
    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 by p45cal; 2018-08-16 at 10:37 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •