好的,所以我有一个带有数据的Excel文件。此数据来自已从Access数据库手动复制的查询。由于每天都必须完成,所以我们希望自动完成。访问插入在Excel下面打开的查询在现有数据下面
我已经在Access数据库内部有VBA代码,它打开查询并将它写入到右表的Excel文件中。 然而,它的工作原理当我把一个静态的范围插入所以它实际上只是覆盖我说的范围:
Dim rst As DAO.Recordset
Dim ApXL As Object
Dim xlWBk As Object
Dim xlWSh As Object
Set rst = CurrentDb.OpenRecordset("Query name")
Set ApXL = CreateObject("Excel.Application")
ApXL.Application.ScreenUpdating = False
ApXL.Visible = True
Set xlWBk = ApXL.Workbooks.Open("C:\blabla.xlsm", True, False)
Set xlWSh = xlWBk.Worksheets(1)
xlWSh.Activate
xlWSh.range("A1341").CopyFromRecordset rst
xlWBk.Save
xlWBk.Close False
ApXL.Quit
rst.Close
Set rst = Nothing
通知的xlWSh.range(“A1341”)CopyFromRecordset RST。 它只是粘贴来自这一行的查询,因为我知道这是第一个空行。
我已经尝试过很多其他的代码,但我总是得到错误:
TheLastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
=> Error 424: Object required
Dim MyRange As range
Dim lngLastRow As Long
Set MyRange = xlWSh.range(strColum & "1")
lngLastRow = xlWSh.Cells(65536, MyRange.Column).End(xlUp).Row
=> Compilation-error on Dim MyRange As range: user-defined type not defined
Dim MyRange As Object
Dim lngLastRow As Long
Set MyRange = xlWSh.range(strColum & "1")
lngLastRow = xlWSh.Cells(65536, MyRange.Column).End(xlUp).Row
=> Error 1004: Application-defined or object-defined error
With xlWBk.Sheets("Sheetname")
lastrow = .range("A" & .Rows.Count).End(xlUp).Row
=> Error 1004: Application-defined or object-defined error
End With
With xlWSh
lastrow = .Cells.Find(What:="*", After:=.range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
=> Error 9: Subscript out of range
End With
所以我不知道了该怎么做。 唯一的其他选择是将整个工作表作为List或Array或Table读入Access数据库,然后执行.Count或其他操作,但插入到lastRow + 1或firstEmptyRow中应该更快,更易于编程。
在此先感谢!
非常感谢您的快速回复。 我对此代码使用了第二个选项: 'lastrow = .Cells.Find(What:=“*”,After:=。range(“A1”),SearchOrder:= 1,SearchDirection:= 2).Row + 1' 而这个作品完美! – kvdw