Set a variable as ActiveCell in Excel VBA

gaa

New Member
Joined
May 22, 2020
Messages
2
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:

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
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Why not this?
VBA Code:
    resultRow = ActiveCell.Row
 
  • Like
Reactions: gaa
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,715
Members
448,985
Latest member
chocbudda

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top