【问题标题】:access 2010 getting max row in excel 2010访问 2010 在 excel 2010 中获得最大行
【发布时间】:2013-06-18 11:02:15
【问题描述】:

我在通过 MS access 2010 访问 excel 2010 时遇到问题。 从 access 2010 开始,我想从我的 excel 数据中获取最大行数。 这是我的代码:

Dim Xl 作为 Excel.Application
将 XlBook 调暗为 Excel.Workbook
将 XlSheet 调暗为 Excel.Worksheet
将 lastRow 变暗,我为整数
MySheetPath = "C:\Users\myaccount\Desktop\LRLV\mydata.xlsx"
设置 Xl = CreateObject("Excel.Application")
设置 XlBook = GetObject(MySheetPath)
Xl.Visible = 真
XlBook.Windows(1).Visible = True
设置 XlSheet = XlBook.Worksheets(1)
使用 XlSheet
lastRow = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
结束于
当我没有打开excel时,一切都很好。但是当我有 1 个或多个已打开的 excel 时,变量“lastRow”总是给我“类型不匹配”错误。现在我需要知道如何解决它。之前非常感谢你。

【问题讨论】:

    标签: excel ms-access vba


    【解决方案1】:

    您的问题是对 A1 的不合格引用。我也不确定当你已经有一个应用程序引用时为什么要使用 GetObject

    Dim Xl As Excel.Application
    
    Dim XlBook As Excel.Workbook
    
    Dim XlSheet As Excel.Worksheet
    
    Dim lastRow As Long, i As Integer
    
    MySheetPath = "C:\Users\myaccount\Desktop\LRLV\mydata.xlsx"
    
    Set Xl = CreateObject("Excel.Application")
    Xl.Visible = True
    Set XlBook = XL.Workbooks.Open(MySheetPath)
    
    XlBook.Windows(1).Visible = True
    
    Set XlSheet = XlBook.Worksheets(1)
    
    With XlSheet
    
        lastRow = .Cells.Find(What:="*", After:=.Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    End With
    

    【讨论】:

      【解决方案2】:

      .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious) 在此实例中未返回有效范围。这意味着查找不成功。这就是你得到类型不匹配的原因。

      要解决,请执行以下操作:

      Dim rng As Excel.Range
      
      Set rng = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
      
      If Not rng Is Nothing Then
          lastRow = rng.Row
      Else
          'ToDo - handle the error here
      EndIf
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-16
        • 2011-06-04
        相关资源
        最近更新 更多