I am copying tables from a word .docx file into an excel sheet.
My code below works fine, however, my issue is that I want to set my resultRow variable to be the ActiveCell.row instead of a fixed integer. So that I can paste my table into an active cell rather than a defined cell range. I have tried changing it to resultRow = ActiveCell and Set resultRow = ActiveCell but they are not working. Any help is appreciated. My code is shown below:
My code below works fine, however, my issue is that I want to set my resultRow variable to be the ActiveCell.row instead of a fixed integer. So that I can paste my table into an active cell rather than a defined cell range. I have tried changing it to resultRow = ActiveCell and Set resultRow = ActiveCell but they are not working. Any help is appreciated. My code is shown below:
VBA Code:
Sub ImportWordTable()
Dim wdDoc As Object
Dim wdFileName As Variant
Dim tableNo As Integer 'table number in Word
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel
Dim resultRow As Long
Dim tableStart As Integer
Dim tableTot As Integer
On Error Resume Next
ActiveSheet.Range("A:AZ").ClearContents
wdFileName = Application.GetOpenFilename("Word files (*.docx),*.doc", , _
"Browse for file containing table to be imported")
If wdFileName = False Then Exit Sub '(user cancelled import file browser)
Set wdDoc = GetObject(wdFileName) 'open Word file
With wdDoc
tableNo = wdDoc.Tables.Count
tableTot = wdDoc.Tables.Count
If tableNo = 0 Then
MsgBox "This document contains no tables", _
vbExclamation, "Import Word Table"
ElseIf tableNo > 1 Then
tableNo = InputBox("This Word document contains " & tableNo & " tables." & vbCrLf & _
"Enter the table to start from", "Import Word Table", "1")
End If
resultRow = 1
For tableStart = tableNo To tableTot
.Tables(tableStart).Borders.Enable = True
.Tables(tableStart).Range.Copy
ActiveSheet.Range("A" & resultRow).PasteSpecial Paste:=xlPasteAll
resultRow = ActiveSheet.Range("A" & ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row).End(xlUp).Row + 1
Next
ActiveSheet.Range("A1:AZ" & resultRow).UnMerge
End With
End Sub