简体   繁体   English

Outlook VBA在Excel工作表中查找最后一行

[英]Outlook VBA find last Row in Excel Worksheet

I'm writing a function in Outlook VBA that involves reading content from an excel workbook. 我正在Outlook VBA中编写一个函数,涉及从excel工作簿中读取内容。

The part I'm struggling with is finding the last row in a column (column A in this example). 我正在努力的部分是找到一列中的最后一行(本例中的A列)。 While the 1st line in the highlighted block correctly displays the content of A1 cell in given worksheet, the second line gives a Error "424" - object required. 当突出显示的块中的第一行正确显示给定工作表中A1单元格的内容时,第二行给出错误“424” - 需要对象。

Any suggestions into the problem would be greatly appreciated. 对此问题的任何建议将不胜感激。

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

If you want to have the ability to use Excel constants within your code, you will need to either 如果您希望能够在代码中使用Excel常量,则需要使用

a) Include a reference to a Microsoft Excel Object Library, or a)包含对Microsoft Excel对象库的引用,或

b) Create your own constant, eg b)创建自己的常量,例如

End(-4162)

or 要么

Const xlUp As Long = -4162
...
... End(xlUp)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM