我需要使用MS Word中的VBA在Excel工作表上进行操作。
当我尝试捕获LastCol值时,我无法理解为什么会出错:
Dim mXL As Object
Dim mWBook As Object
Dim mSheet As Object
Dim C As Long, R As Long
Dim LastRow As Long, LastCol As Long
Set mXL = CreateObject("Excel.Application")
mXL.Visible = True
Set mWBook = mXL.Workbooks.Open("c:\test\test.xlsx")
Set mSheet = mWBook.sheets(1)
LastCol = mSheet.Cells(1, mSheet.Columns.Count).End(xlToLeft).Column 'THIS ISN'T WORKIMG
LastCol = mSheet.Cells(1, 1).End(xlToRight).Column 'This also gives error
答案 0 :(得分:2)
您使用的是后期绑定,因此Word不知道xlToLeft
和xlToRight
是什么,因为它们来自 Excel 对象模型。
添加以下内容:
Const xlToLeft As Long = -4159
Const xlToRight As Long = -4161
基于XLDirection枚举中的相应值。
答案 1 :(得分:1)
如@BigBen所述,您不能在后期绑定中使用Excel的枚举。请改用枚举(https://docs.microsoft.com/en-us/office/vba/api/excel.xldirection)