by
12k 31

Excel VBA Merge cells with same value

Merge cells in different columns with the same value in an ordered list
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/700364/Excel-VBA-Merge-cells-with-same-value?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
Sub merge_cells() Application.DisplayAlerts = False 'row to be avaluated iRow = 1 'number of columns to be evaluated iNumberOfColumns = 12 'first Column to be evaluated iFirstColumn = 4 For i = iFirstColumn + 1 To iNumberOfColumns + iFirstColumn + 1 If Cells(iRow, i) <> Cells(iRow, i - 1) Then iLastRow = i - 1 'get first column letter vArr = Split(Cells(1, iFirstColumn).Address(True, False), "$") sFirstColumn = vArr(0) 'get last column letter vArr = Split(Cells(1, i - 1).Address(True, False), "$") sLastColumn = vArr(0) sRange = sFirstColumn & iRow & ":" & sLastColumn & iRow Range(sRange).Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = True .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge iFirstColumn = i End If Next i Application.DisplayAlerts = True End Sub
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus