Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Option Explicit
Sub CollapseLastColumn()
Dim rngDatabase As Range
Dim rngCurrentRow As Range
Dim nColumns As Integer
Dim nRow As Long
Dim nRowsDeleted As Long
Set rngDatabase = ActiveCell.CurrentRegion
nColumns = rngDatabase.Columns.Count
' Assume first row is column headings
' Change to nRow = 1 if no headings.
nRow = 2
While nRow < rngDatabase.Rows.Count
Set rngCurrentRow = Intersect(rngDatabase, rngDatabase.Rows(nRow))
If NextRowMatches(rngCurrentRow) Then
CollapseNextRow rngCurrentRow
nRowsDeleted = nRowsDeleted + 1
Else
nRow = nRow + 1
End If
Wend
For nRow = 1 To nRowsDeleted
rngCurrentRow.Offset(1, 0).Insert (xlDown)
Next nRow
Set rngCurrentRow = Nothing
Set rngDatabase = Nothing
End Sub
Private Function NextRowMatches(CurrentRow As Range) As Boolean
Dim nCol As Integer
NextRowMatches = True
For nCol = 1 To CurrentRow.Cells.Count - 1
If CurrentRow.Cells(1, nCol) <> CurrentRow.Cells(2, nCol) Then
NextRowMatches = False
Exit For
End If
Next nCol
End Function
Private Sub CollapseNextRow(CurrentRow As Range)
With CurrentRow
.Cells(1, .Cells.Count).Value = "'" & _
.Cells(1, .Cells.Count).Text & ", " & _
.Cells(2, .Cells.Count).Text
CurrentRow.Offset(1, 0).Delete (xlUp)
End With
End Sub