access 2010在excel 2010中获取最大行数
我在通过MS access 2010访问excel 2010时遇到问题。 从access 2010中,我想从excel数据中获取最大行数。 这是我的代码:access 2010在excel 2010中获取最大行数,excel,ms-access,vba,Excel,Ms Access,Vba,我在通过MS access 2010访问excel 2010时遇到问题。 从access 2010中,我想从excel数据中获取最大行数。 这是我的代码: 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\myd
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")
Set XlBook = GetObject(MySheetPath)
Xl.Visible = True
XlBook.Windows(1).Visible = True
Set XlSheet = XlBook.Worksheets(1)
With XlSheet
lastRow = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
End With Dim Xl作为Excel应用程序
将XlBook作为Excel.工作簿进行编辑
将工作表设置为Excel。工作表
Dim lastRow为长,i为整数
MySheetPath=“C:\Users\myaccount\Desktop\LRLV\mydata.xlsx”
Set Xl=CreateObject(“Excel.Application”)
设置XlBook=GetObject(MySheetPath)
Xl.Visible=True
XlBook.Windows(1).Visible=True
设置XlSheet=XlBook.工作表(1)
使用XlSheet
lastRow=.Cells.Find(What:=“*”,After:=[A1],SearchOrder:=xlByRows,SearchDirection:=xlPrevious)。行
以
当我没有打开excel时,一切正常。但是,当我打开了一个或多个excel时,variabel“lastRow”总是给我“类型不匹配”错误。现在我需要知道如何修复它。在此之前非常感谢。
.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
你的问题是对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