circusnight
New Member
- Joined
- Jan 7, 2025
- Messages
- 5
- Office Version
- 365
- Platform
- Windows
Hey Everyone,
I am in Word VBA trying to populate a table that sources from an Excel file. The Excel file has hardcoded numbers and no formulas at all. There are several worksheets and currently, there is only one Named Ranged, which is StartRow. StartRow is workbook scoped and highlights the entire row 20. When I test the code using cell coordinates (i.e. I20) it works however the moment I change to NamedRanged, whether an entire row or just the cell, it causes the error Method 'Range of object'_Worksheet. One thing I have noticed is that every time the macro opens up the Excel, a repair message pops, even though I have already tried to repair it. Code's purpose is to ensure that I don't have to manually update the row number in case we add/remove rows in Excel. I have posted this in another forum however it seems we may be stuck so wanted to see if anyone else has any ideas (https://stackoverflow.com/q/79324694/25349484). See below the code and picture reference of excel. I cannot download the add-in since I am on work laptop. Let me know if any additional information is needed.
Line highlighted during debugging: Xlr = XlWkSht.Range("StartRow").RefersToRange.Row
I am in Word VBA trying to populate a table that sources from an Excel file. The Excel file has hardcoded numbers and no formulas at all. There are several worksheets and currently, there is only one Named Ranged, which is StartRow. StartRow is workbook scoped and highlights the entire row 20. When I test the code using cell coordinates (i.e. I20) it works however the moment I change to NamedRanged, whether an entire row or just the cell, it causes the error Method 'Range of object'_Worksheet. One thing I have noticed is that every time the macro opens up the Excel, a repair message pops, even though I have already tried to repair it. Code's purpose is to ensure that I don't have to manually update the row number in case we add/remove rows in Excel. I have posted this in another forum however it seems we may be stuck so wanted to see if anyone else has any ideas (https://stackoverflow.com/q/79324694/25349484). See below the code and picture reference of excel. I cannot download the add-in since I am on work laptop. Let me know if any additional information is needed.
Line highlighted during debugging: Xlr = XlWkSht.Range("StartRow").RefersToRange.Row
VBA Code:
Option Explicit
Sub EPS_QTD()
Application.ScreenUpdating = False
' Declare variables
Dim XlApp As New Excel.Application, XlWkBk As Excel.Workbook, XlWkSht As Excel.Worksheet
Dim r As Long, c As Long, Xlr As Long
'Open Excel Workbook
Set XlWkBk = XlApp.Workbooks.Open(HCLocation, ReadOnly:=True, CorruptLoad:=xlExtractData)
Set XlWkSht = XlWkBk.Sheets("EPS"): Xlr = XlWkSht.Range("StartRow").RefersToRange.Row
'Do the processing
With ActiveDocument.Tables(1)
For r = 3 To .Rows.Count
For c = 2 To .Columns.Count
If XlWkSht.Cells(Xlr, c + 7).Text <> "" Then .Cell(r, c).Range.Text = XlWkSht.Cells(Xlr, c + 7).Text
Next
Xlr = Xlr + 1
Next
End With
XlWkBk.Close SaveChanges:=False: XlApp.Quit
'Clear variables
Set XlWkSht = Nothing: Set XlWkBk = Nothing: Set XlApp = Nothing
End Sub