Ok, this code presumes no empty rows between first charge for anyone and last
charge for anyone. It actually only changes one line of code, but the whole
thing is
here so you can just copy this and replace the existing routine easily. I
did add a statement: Application.ScreenUpdating = False that will improve the
speed for it. You won't see anything happening on the screen until the
process is completed, but this will get it all done much faster.
Sub TransposeChargeData()
Const nameColumn = "A" ' change as needed
Const startRow = 2 ' row w/1st name in it
Dim lastNameRow As Long
Dim nameRow As Long
Dim rOffset As Long
Dim cOffset As Long
Dim baseCell As Range
If Val(Left(Application.Version, 2)) < 12 Then
'pre-2007 Excel
lastNameRow = Range(nameColumn & Rows.Count).End(xlUp).Row
Else
'Excel 2007 or later
lastNameRow = Range(nameColumn & Rows.CountLarge).End(xlUp).Row
End If
Set baseCell = Range(nameColumn & startRow)
Application.ScreenUpdating = False ' improve performance
Do Until rOffset > lastNameRow
If IsEmpty(baseCell.Offset(rOffset + 1, 0)) _
And Not (IsEmpty(baseCell.Offset(rOffset, 0))) Then
cOffset = 1 ' initialize/reset
Do Until Not IsEmpty(baseCell.Offset(rOffset + cOffset, 0)) _
Or IsEmpty(baseCell.Offset(rOffset + cOffset, 1))
baseCell.Offset(rOffset, cOffset + 1).Value = _
baseCell.Offset(rOffset + cOffset, 1).Value
baseCell.Offset(rOffset + cOffset, 1) = "" ' remove entry
cOffset = cOffset + 1 ' look for next charge
Loop
End If
rOffset = rOffset + 1 ' look for next name
Loop
Application.ScreenUpdating = True
End Sub
If you want the process to actually physically delete the rows between the
names that had charges on them that were moved up to same row with names,
then use this code instead (same change as above, with delete rows function
added).
Sub TransposeChargeData()
Const nameColumn = "A" ' change as needed
Const startRow = 2 ' row w/1st name in it
Dim lastNameRow As Long
Dim nameRow As Long
Dim rOffset As Long
Dim cOffset As Long
Dim baseCell As Range
Dim TestRow As Long
If Val(Left(Application.Version, 2)) < 12 Then
'pre-2007 Excel
lastNameRow = Range(nameColumn & Rows.Count).End(xlUp).Row
Else
'Excel 2007 or later
lastNameRow = Range(nameColumn & Rows.CountLarge).End(xlUp).Row
End If
Set baseCell = Range(nameColumn & startRow)
Application.ScreenUpdating = False ' improve performance
Do Until rOffset > lastNameRow
If IsEmpty(baseCell.Offset(rOffset + 1, 0)) _
And Not (IsEmpty(baseCell.Offset(rOffset, 0))) Then
cOffset = 1 ' initialize/reset
Do Until Not IsEmpty(baseCell.Offset(rOffset + cOffset, 0)) _
Or IsEmpty(baseCell.Offset(rOffset + cOffset, 1))
baseCell.Offset(rOffset, cOffset + 1).Value = _
baseCell.Offset(rOffset + cOffset, 1).Value
baseCell.Offset(rOffset + cOffset, 1) = "" ' remove entry
cOffset = cOffset + 1 ' look for next charge
Loop
End If
rOffset = rOffset + 1 ' look for next name
Loop
'delete empty rows left behind
TestRow = lastNameRow
rOffset = 0 ' reset
Do Until baseCell.Offset(rOffset, 0).Row >= lastNameRow
'have to recalculate as we are deleting rows
lastNameRow = Range(nameColumn & TestRow).End(xlUp).Row
Do While IsEmpty(baseCell.Offset(rOffset, 0)) And _
baseCell.Offset(rOffset, 0).Row < _
Range(nameColumn & TestRow).End(xlUp).Row
baseCell.Offset(rOffset, 0).EntireRow.Delete
Loop
rOffset = rOffset + 1
Loop
Application.ScreenUpdating = True
End Sub