excelvbapdf

Concatenate imported data from PDF file, that was split into multiple rows, into one row


I imported data from a PDF file into Excel.

Some of the columns have been split into two rows.

This image shows the issue and the outcome I would like.
The issue and outcome

There are over 1000 rows. (Note that some rows do not need to be modified such as Contract 3.)

I was thinking of VBA code that would check if the row below in Column A is blank/null. If true, then it would concatenate the rows in Columns B and C.


Solution

  • Here's one way to do it. Select the data (excluding the headers) and run this:

    Sub ConsolidateRows()
        Dim rw As Range, data As Range, r As Long, rwDest As Range, col As Long, v
        
        Set data = Selection
        For r = data.Rows.Count To 2 Step -1
            Set rw = data.Rows(r)
            If Len(rw.Cells(1).Value) = 0 Then ' "extra row" has no id in first cell
                'find next row above with a value in first cell
                Set rwDest = rw.Cells(1).End(xlUp).Resize(1, data.Columns.Count)
                If rwDest.Row < data.Rows(1).Row Then Exit Sub 'out of the data
                
                'copy values up and append to destination row
                For col = 2 To data.Columns.Count
                    v = rw.Cells(col).Value
                    If Len(v) > 0 Then 'anything to copy?
                        With rwDest.Cells(col)
                            .Value = .Value & IIf(.Value <> "", " ", "") & v
                        End With
                    End If
                Next col
                rw.Delete 'remove this row
            End If
        Next r
    End Sub