Hi StackOverflow community. I am new at VBA coding and am trying to import table data from a Word document into Excel.
The number of tables in the Word document will be fixed at 5, and so will the number of rows and columns in each specific table.
I am having succes with importing all of the data, but the next step, where I would like to format the imported data, I can't seem to figure out.
Please see the inserted pictures below of the outcome that I get, and the outcome that I would like to get.
Please find the code that I have written below:
Sub CommandButton1_Click()
'Declare variables
Dim wdDoc As Object
Dim wdFileName As Variant
Dim tableNo As Integer
Dim irow As Long
Dim icolumn As Long
row_number = 1
col_number = 1
'Open specific Word-document to import table
wdFileName = Application.GetOpenFilename("Word File(*.docx), *.docx", , "Select Word File", , False)
If wdFileName = False Then Exit Sub
Set wdDoc = GetObject(wdFileName)
With wdDoc
'Count the number of tables
tableNo = .tables.Count
If tableNo = 0 Then
MsgBox "There are no tables in the specified Word Document. Please select the correct Word Document"
Else
'Import of text/data in the tables from Word-document to specified range in Excel. Starts with table 1, then 2 and so on
For i = 1 To 1
With .tables(i)
For icolumn = 1 To .Rows.Count
Application.Range("C6:D7").Cells(col_number, 1).Value = WorksheetFunction.Clean(.cell(icolumn, 1).Range.Text)
Application.Range("C6:D7").Cells(col_number, 2).Value = WorksheetFunction.Clean(.cell(icolumn, 2).Range.Text)
col_number = col_number + 1
row_number = row_number + 1
Next icolumn
End With
Next i
For i = 2 To 2
With .tables(i)
For icolumn = 1 To .Rows.Count
Application.Range("C7:D8").Cells(col_number, 1).Value = WorksheetFunction.Clean(.cell(icolumn, 1).Range.Text)
Application.Range("C7:D8").Cells(col_number, 2).Value = WorksheetFunction.Clean(.cell(icolumn, 2).Range.Text)
col_number = col_number + 1
row_number = row_number + 1
Next icolumn
End With
Next i
For i = 2 To 2
With .tables(i)
For icolumn = 1 To .Rows.Count
Application.Range("C8:D9").Cells(col_number, 1).Value = WorksheetFunction.Clean(.cell(icolumn, 3).Range.Text)
Application.Range("C8:D9").Cells(col_number, 2).Value = WorksheetFunction.Clean(.cell(icolumn, 4).Range.Text)
col_number = col_number + 1
row_number = row_number + 1
Next icolumn
End With
Next i
For i = 3 To 3
With .tables(i)
For icolumn = 1 To .Rows.Count
Application.Range("C9:D10").Cells(col_number, 1).Value = WorksheetFunction.Clean(.cell(icolumn, 1).Range.Text)
Application.Range("C9:D10").Cells(col_number, 2).Value = WorksheetFunction.Clean(.cell(icolumn, 2).Range.Text)
Application.Range("C9:D10").Cells(col_number, 3).Value = WorksheetFunction.Clean(.cell(icolumn, 3).Range.Text)
Application.Range("C9:D10").Cells(col_number, 4).Value = WorksheetFunction.Clean(.cell(icolumn, 4).Range.Text)
Application.Range("C9:D10").Cells(col_number, 5).Value = WorksheetFunction.Clean(.cell(icolumn, 5).Range.Text)
Application.Range("C9:D10").Cells(col_number, 6).Value = WorksheetFunction.Clean(.cell(icolumn, 6).Range.Text)
Application.Range("C9:D10").Cells(col_number, 7).Value = WorksheetFunction.Clean(.cell(icolumn, 7).Range.Text)
Application.Range("C9:D10").Cells(col_number, 8).Value = WorksheetFunction.Clean(.cell(icolumn, 8).Range.Text)
col_number = col_number + 1
row_number = row_number + 1
Next icolumn
End With
Next i
For i = 4 To 4
With .tables(i)
For icolumn = 1 To .Rows.Count
Application.Range("C10:D11").Cells(col_number, 1).Value = WorksheetFunction.Clean(.cell(icolumn, 1).Range.Text)
Application.Range("C10:D11").Cells(col_number, 2).Value = WorksheetFunction.Clean(.cell(icolumn, 2).Range.Text)
col_number = col_number + 1
row_number = row_number + 1
Next icolumn
End With
Next i
For i = 5 To 5
With .tables(i)
For icolumn = 1 To .Rows.Count
Application.Range("C11:D12").Cells(col_number, 1).Value = WorksheetFunction.Clean(.cell(icolumn, 1).Range.Text)
Application.Range("C11:D12").Cells(col_number, 2).Value = WorksheetFunction.Clean(.cell(icolumn, 2).Range.Text)
col_number = col_number + 1
row_number = row_number + 1
Next icolumn
End With
Next i
End If
End With
End Sub
I hope that the community can help with this :)