Writing a line of code that tells the macro to go to the next row if a row already has content.

Hello There,

I have a macro down below.

Im just wondering if there's a way for that macro to go to the next row if there is content on the row that its about to use.
I was thinking of editing the GetCellText() function, but i receive an error.

Option Explicit

Sub WordToExcel()

    Dim sh As Excel.Worksheet
    Dim strFolder As String
    Dim strFile As String
    Dim strFullName As String
    Dim r As Integer
    
    Set sh = ThisWorkbook.Worksheets("Sheet1")
    'Insert Folder Link
    strFolder = "C:\MyFolder\"
    strFile = Dir(strFolder & "*.doc*")
    r = 2
    Do Until strFile = ""
        strFullName = strFolder & strFile
        CopyTableFromDocx strFullName, sh, r
        strFile = Dir()
        r = r + 1
    Loop

End Sub

Sub CopyTableFromDocx(strMSWordFileName As String, sh As Worksheet, r As Integer)

    Dim objDoc As Word.Document
    'Dim lngTableIndex As Long
    Dim objWordTable As Word.Table
    Dim objWordCell As Word.cell
    Dim strLabel As String
    Dim strData As String
    Const mtrDsc As String = "? "
    Dim c As Integer
    Dim t As Integer
    Dim bFound As Boolean
    'Dim lngRowIndex As Long, lngColumnIndex As Long
    'Dim strCleanCellValue As String

    On Error GoTo CleanUp

    'get reference to word doc
    Set objDoc = GetObject(strMSWordFileName)

    objDoc.Application.Visible = True
    
    t = 0
    
    For Each objWordTable In objDoc.Tables 'step through tables
        t = t + 1
        'iterate cells
        For Each objWordCell In objWordTable.Range.Cells
            Select Case GetCellText(objWordCell)
                Case "Type:", "Category:", "Name:", "AlternateName:", "ID:", "Class:", "Width:", "Text:", "Text-Align:", "Border-Radius:", "Margin:"
                
                strLabel = UCase(Trim(GetCellText(objWordCell)))
        
                c = 1
                bFound = False
                Do Until sh.Cells(1, c).Value = ""
                    If sh.Cells(1, c).Value = strLabel Then
                        bFound = True
                        Exit Do
                    End If
                    c = c + 1
                Loop
                If Not bFound Then
                    sh.Cells(1, c).Value = strLabel
                End If
                strData = Trim(GetCellText(objWordTable.cell(objWordCell.RowIndex, objWordCell.ColumnIndex + 1)))
                sh.Cells(r, c).Value = strData
                
            End Select 'shaded cell?
        Next objWordCell
        
        'success
        Debug.Print "Successfully copied table # " & t & " from " & strMSWordFileName
    Next objWordTable
    objDoc.Close
CleanUp:
    If Err.Number <> 0 Then
        Debug.Print Err.Number & " " & Err.Description
        Err.Clear
    End If

End Sub

Function GetCellText(cl As Word.cell) As String
Dim rng As Word.Range
Dim I As Long
Dim arrData()

    Set rng = cl.Range

    If rng.FormFields.Count >0 1 Then
        ReDim arrData(1 To rng.FormFields.Count)
        For I = 1 To rng.FormFields.Count
            arrData(I) = rng.FormFields(I).Result
        Next I
        GetCellText = Join(arrData, " ")
    Else
        rng.MoveEnd wdCharacter, -1
        GetCellText = rng.Text
    End If
    
End Function

Open in new window

rilAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Neil FlemingConsultant and developerCommented:
Do you mean you want to skip non-blank rows in Excel?

If so, then before:

sh.Cells(r, c).Value = strData

Open in new window


you could add:
While sh.Cells(r,c)<>""
r=r+1
Wend 

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.