Snippets

Piotr Szrajber VBA - Remove Headers from table imported from PDF

Created by Piotr Szrajber
' PDF imported to WORD first and all ^p changed to " "
' Some manual cleanup required
' Be careful for the ending condition

Sub RemoveHeadersFromTableImportedFromPDF()
    Dim ObjectIdCell As Range
    Dim lhs, rhs As String
    
    Do
        Set ObjectIdCell = Range("A2", "J3000").Find("OBJECTID")
        
        'MsgBox ObjectIdCell.Row
        If IsEmpty(Cells(ObjectIdCell.Row - 1, 1)) Then
            If IsEmpty(Cells(ObjectIdCell.Row + 1, 1)) Then
                For i = 1 To 9
                    rhs = Cells(ObjectIdCell.Row + 1, i).Value
                    If Not IsEmpty(rhs) Then
                        lhs = Cells(ObjectIdCell.Row - 2, i).Value
                        If Right(lhs, 1) = "-" Then
                            lhs = Left(lhs, Len(lhs) - 1)
                        End If
                        Cells(ObjectIdCell.Row - 2, i).Value = lhs & rhs
                    End If
                Next i
                Rows(ObjectIdCell.Row + 1).Delete
            End If
            Rows(ObjectIdCell.Row - 1).Delete
            Rows(ObjectIdCell.Row).Delete
        Else
            'MsgBox Cells(ObjectIdCell.Row - 1, 1).Value
        End If
    Loop Until ObjectIdCell Is Nothing
End Sub

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.