Skip to content

Instantly share code, notes, and snippets.

@KMurphs
Last active April 26, 2021 22:33
Show Gist options
  • Save KMurphs/029ff213288b57fd3a365f3ef431d4d2 to your computer and use it in GitHub Desktop.
Save KMurphs/029ff213288b57fd3a365f3ef431d4d2 to your computer and use it in GitHub Desktop.
Excel Macros to merge cells by merging cell contents. Default excel behavior is to keep only the upper left cell content during the merge. These Macros will concatenate the cell content using new lines

Excel Merge Cells Macros

Sub MergeCells()
'
' MergeCells Macro
'
    Dim var As String
    
    rg = Selection.Address    ' Current selected range e.g. "F2:F5"
    Application.DisplayAlerts = False ' No popup when we will merge the cells at the end
    
    Debug.Print rg
    
    var = ""
    ' Selection is the current cell(s) selected e.g. "F2:F5"
    For Each cell In Selection
        var = var & vbCrLf & cell.Value ' Concatenate with newline in between
        
    Next cell
    
    ' Merge original selection, and update its value
    Range(rg).Merge
    Range(rg).Value = var
'
End Sub







Sub ColumnMergeCells()
'
' ColumnMergeCells Macro
'
    Dim rCell As Range
    Dim nCell As Range
    Dim nCols As Integer
    
    deltaCols = 1

    Set rCell = Cells(2, ActiveCell.Column)
    Do While Not IsEmpty(rCell.Value)  'Loops until the active cell is blank.
        
        Set nCell = Cells(rCell.Offset(1, 0).Row - 1, rCell.Offset(1, 0).Column)
        Range(rCell.Offset(0, deltaCols).Address, nCell.Offset(0, deltaCols).Address).Select
        ' Debug.Print Selection.Address
        ' Debug.Print rCell.Address & ":" & nCell.Address
        Application.Run "MergeCells"
        
        Set rCell = rCell.Offset(1, 0)    ' Jump to next cell/group of cell
    Loop

End Sub




Sub MergeRightCells()
'
' Macro2 Macro
'
    Dim var As String
      
    var = ""
    
    ' Selection is the current cell(s) selected e.g. "F2:F5"
    For Each cell In Selection
        ' cell.Offset(, 1) takes the cell on the right e.g. G2, G3, G4, G5
        var = var & vbCrLf & cell.Offset(, 1).Value
        
    Next cell
    
    ' Take the concatenating values and put it back in the original selection
    ActiveCell.Value = var

'
End Sub

References

  1. https://stackoverflow.com/questions/4080741/get-user-selected-range
  2. https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2003/aa203726(v=office.11)?redirectedfrom=MSDN#concatenate-columns-of-data
  3. https://www.automateexcel.com/vba/cell-value-get-set/#Assign_Cell_Value_to_Variable
  4. https://stackoverflow.com/questions/26008268/get-the-value-of-the-cell-to-the-right-in-vba
  5. https://www.ozgrid.com/forum/index.php?thread/66522-supress-merge-cells-warning-message/
  6. https://docs.microsoft.com/en-us/office/vba/api/excel.range.select
  7. https://stackoverflow.com/questions/25191024/translate-a-range-object-in-excel-vba
  8. https://www.automateexcel.com/vba/debug-print-immediate-window
  9. https://stackoverflow.com/questions/9391092/looping-through-merged-cells-in-vba
  10. https://www.ozgrid.com/forum/index.php?thread/70291-return-column-number-of-cell/
  11. https://www.excel-easy.com/vba/examples/loop-through-entire-column.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment