Private Sub compact1()
Dim nCurRow As Integer
Dim nCurCol As Integer
Dim nDestRow As Integer
Dim wsSource As Worksheet, wsDest As Worksheet
Dim cColHead As String
Dim vColSplit As Variant
Dim lcBranch As String ' current branch
Dim nBranchRow As Integer ' count the members / current branch for columns header
Dim nBranchRowMax As Integer ' count the max number of members / branch for columns header
Set wsSource = Workbooks("EM Contact v.01.xlsm").Worksheets("EM Contact")
Set wsDest = Workbooks("Branch Emergency Contact List V.01.xlsx").Worksheets("Final")
With wsSource.Cells(1, 1).CurrentRegion
' Copy the columns
nDestRow = 1
nBranchRowMax = 0
For nCurRow = 2 To .Rows.Count
If IsEmpty(lcBranch) Then ' first branch
nDestRow = 1
lcBranch = wsSource.Cells(nCurRow, 1).Value
nBranchRow = 0
End If
If lcBranch <> wsSource.Cells(nCurRow, 1).Value Then ' New Branch
lcBranch = wsSource.Cells(nCurRow, 1).Value
nDestRow = nDestRow + 1 ' New destination row
If nBranchRowMax < nBranchRow Then
nBranchRowMax = nBranchRow
End If
nBranchRow = 0
Else ' new member of the same branch
nBranchRow = nBranchRow + 1
End If
If nCurRow = .Rows.Count Then ' last branch
If nBranchRowMax < nBranchRow Then
nBranchRowMax = nBranchRow
End If
End If
wsDest.Cells(nDestRow, 1).Value = wsSource.Cells(nCurRow, 1)
For nCurCol = 2 To .Columns.Count
wsDest.Cells(nDestRow, nBranchRow * (.Columns.Count - 1) + nCurCol).Value = wsSource.Cells(nCurRow, nCurCol).Value
Next
Next
' Copy the columns header
wsDest.Cells(1, 1).Value = wsSource.Cells(1, 1).Value
For nCurRow = 1 To nBranchRowMax + 1
For nCurCol = 2 To .Columns.Count
vColSplit = Split(wsSource.Cells(1, nCurCol).Value)
cColHead = vColSplit(0) & " " & nCurRow & Mid(wsSource.Cells(1, nCurCol).Value, Len(vColSplit(0)) + 1)
wsDest.Cells(1, (nCurRow - 1) * (.Columns.Count - 1) + nCurCol).Value = cColHead
Next
Next
End With
End Sub