复制表格中的数据以访问现有的Excel文件

时间:2019-05-29 20:15:47

标签: vba access-vba

我想将表或查询中的记录复制到文件夹中的现有excel文件中

我的代码将数据导出到新的Excel文件中。它不适用于我在此处定义的当前文件path = CurrentProject.path&“ \”&“ BTO Export.xlsx”

Private Sub cmdGetIDs_Click()

DoCmd.SetWarnings False

Dim db As Database
Dim rs As Recordset
Set db = CurrentDb

Dim path As String

path = CurrentProject.path & "\"& "BTO Export.xlsx"

Dim mySql As String

 On Error GoTo HandleError

mySql = "SELECT DISTINCT TABLEDD.[Code] as [ID] From TABLEDD"

 On Error Resume Next
     CurrentDb.QueryDefs.Delete "temp2"
 On Error GoTo 0

db.CreateQueryDef "temp2", mySql


DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "temp2", CurrentProject.path & "\" & " IDS Generated" & Format(Now(), " mm dd yyyy hh mm"), True


CurrentDb.QueryDefs.Delete "temp2"
Set db = Nothing


DoCmd.SetWarnings True


MsgBox "List of IDS processed successfully"



Exit Sub

HandleError:
    MsgBox Err.Description

End Sub

我正在尝试将访问时的表中的ID列表复制到第一个工作表调用的特定Excel文件中,从单元格A2到A101的“数据”,如果我有100个ID,如果我有50个ID,那将来自从a2到a51。我不知道如何做到这一点。如果有人能指出正确的方向,我将不胜感激

0 个答案:

没有答案