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
- https://stackoverflow.com/questions/4080741/get-user-selected-range
- https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2003/aa203726(v=office.11)?redirectedfrom=MSDN#concatenate-columns-of-data
- https://www.automateexcel.com/vba/cell-value-get-set/#Assign_Cell_Value_to_Variable
- https://stackoverflow.com/questions/26008268/get-the-value-of-the-cell-to-the-right-in-vba
- https://www.ozgrid.com/forum/index.php?thread/66522-supress-merge-cells-warning-message/
- https://docs.microsoft.com/en-us/office/vba/api/excel.range.select
- https://stackoverflow.com/questions/25191024/translate-a-range-object-in-excel-vba
- https://www.automateexcel.com/vba/debug-print-immediate-window
- https://stackoverflow.com/questions/9391092/looping-through-merged-cells-in-vba
- https://www.ozgrid.com/forum/index.php?thread/70291-return-column-number-of-cell/
- https://www.excel-easy.com/vba/examples/loop-through-entire-column.html