我正在 Outlook VBA 中编写一个函数,该函数涉及从 Excel 工作簿中读取内容。
我正在努力的部分是找到列中的最后一行(本例中的列 A)。虽然突出显示 block 中的第一行正确显示给定工作表中 A1 单元格的内容,但第二行给出错误“424” - 需要对象。
任何有关该问题的建议将不胜感激。
Public Function openExcel()
Dim xlApp As Object
Dim sourceWorkBook
Dim sourceWorkSheet
Dim cellVal As String
Dim lastRow As Long
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Visible = True
.EnableEvents = True
End With
Set sourceWorkBook = xlApp.Workbooks.Open("C:\SAMPLEPATH\Template.xlsx")
Set sourceWorkSheet = sourceWorkBook.Worksheets("Sheet1")
sourceWorkBook.Activate
With Activesheet
cellVal = sourceWorkSheet.Cells(1, 1) lastRow = sourceWorkSheet.Cells(.Rows.Count, "A").End(xlUp).Row
End With
sourceWorkBook.Save
sourceWorkBook.Close
xlApp.Quit
End Function
最佳答案
如果您希望能够在代码中使用 Excel 常量,则需要
a) 包括对 Microsoft Excel 对象库的引用,或
b)创建自己的常量,例如
End(-4162)
或者
Const xlUp As Long = -4162
...
... End(xlUp)
关于vba - Outlook VBA 在 Excel 工作表中找到最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44425157/