Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用VBA从Access附加到Excel_Excel_Ms Access_Vba - Fatal编程技术网

使用VBA从Access附加到Excel

使用VBA从Access附加到Excel,excel,ms-access,vba,Excel,Ms Access,Vba,由于这个特殊的问题,我无法获得代码来将Access导出的数据附加到Excel。我创建了一个简单的Access数据库,其中一些数据显示在表单上。之后,可以使用代码将显示的记录导出到Excel 到目前为止还不错。但当我导出下一条记录时,它会覆盖Excel第一行上先前导出的数据。我希望代码附加到下一行,依此类推 我已经找到了一些关于如何附加“ActiveCell.Value”和“ActiveCell.Offset”的主题,但我的知识太有限,无法将其用于代码。从我认为我得到它的那一刻起,VBE就出现了错

由于这个特殊的问题,我无法获得代码来将Access导出的数据附加到Excel。我创建了一个简单的Access数据库,其中一些数据显示在表单上。之后,可以使用代码将显示的记录导出到Excel

到目前为止还不错。但当我导出下一条记录时,它会覆盖Excel第一行上先前导出的数据。我希望代码附加到下一行,依此类推

我已经找到了一些关于如何附加“ActiveCell.Value”和“ActiveCell.Offset”的主题,但我的知识太有限,无法将其用于代码。从我认为我得到它的那一刻起,VBE就出现了错误。看来我搞不懂

Private Sub Command15_Click()
Dim oExcel          As Object
Dim oExcelWrkBk     As Object
Dim oExcelWrSht     As Object
Dim bExcelOpened    As Boolean

'Start Excel
On Error Resume Next
Set oExcel = GetObject(, "Excel.Application")    'Bind to existing instance of Excel
If Err.Number <> 0 Then    'Could not get instance of Excel, so create a new one
    Err.Clear
    On Error GoTo Error_Handler
    Set oExcel = CreateObject("excel.application")
    bExcelOpened = False
Else    'Excel was already running
    bExcelOpened = True
End If
On Error GoTo Error_Handler
oExcel.ScreenUpdating = False
oExcel.Visible = False   'Keep Excel hidden until we are done with our manipulation
'Set oExcelWrkBk = oExcel.Workbooks.Add()    'Start a new workbook
Set oExcelWrkBk = oExcel.Workbooks.Open("C:\test.xlsx")     'Open an existing Excel file
Set oExcelWrSht = oExcelWrkBk.Sheets(1) 'which worksheet to work with

'Start copying over your form values to the Excel Spreadsheet
'Cells(8, 3) = 8th row, 3rd column
oExcelWrSht.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) = Me.1
oExcelWrSht.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0) = Me.2
oExcelWrSht.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0) = Me.3
oExcelWrSht.Cells(Rows.Count, 4).End(xlUp).Offset(1, 0) = Me.4
oExcelWrSht.Cells(Rows.Count, 5).End(xlUp).Offset(1, 0) = Me.5
oExcelWrSht.Cells(Rows.Count, 6).End(xlUp).Offset(1, 0) = Me.6
oExcelWrSht.Cells(Rows.Count, 7).End(xlUp).Offset(1, 0) = Me.7
oExcelWrSht.Cells(Rows.Count, 8).End(xlUp).Offset(1, 0) = Me.8
oExcelWrSht.Cells(Rows.Count, 9).End(xlUp).Offset(1, 0) = Me.9
'... and so on ...

oExcelWrSht.Range("A1").Select  'Return to the top of the page

'    oExcelWrkBk.Close True, sFileName 'Save and close the generated workbook
'    'Close excel if is wasn't originally running
'    If bExcelOpened = False Then
'        oExcel.Quit
'    End If Error_Handler_Exit:
On Error Resume Next
oExcel.Visible = True   'Make excel visible to the user
Set oExcelWrSht = Nothing
Set oExcelWrkBk = Nothing
oExcel.ScreenUpdating = True
Set oExcel = Nothing
Exit Sub Error_Handler:
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
       "Error Number: " & Err.Number & vbCrLf & _
       "Error Source: Export2XLS" & vbCrLf & _
       "Error Description: " & Err.Description _
       , vbOKOnly + vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit End Sub
Private子命令15_Click()
作为对象的Dim oExcel
Dim oExcelWrkBk作为对象
Dim oExcelWrSht作为对象
Dim被扩展为布尔值
'启动Excel
出错时继续下一步
将oExcel=GetObject(,“Excel.Application”)'绑定到现有的Excel实例
如果错误号为0,则“无法获取Excel实例,请创建一个新实例。”
呃,明白了
关于错误转到错误处理程序
设置oExcel=CreateObject(“excel.application”)
bExcelOpened=False
Else的Excel已在运行
bExcelOpened=真
如果结束
关于错误转到错误处理程序
oExcel.ScreenUpdating=False
oExcel.Visible=False“将Excel隐藏,直到我们完成操作
'设置oExcelWrkBk=oExcel.Workbooks.Add()'启动新工作簿
设置oExcelWrkBk=oExcel.Workbooks.Open(“C:\test.xlsx”)'打开现有Excel文件
设置oExcelWrSht=oExcelWrkBk.Sheets(1)'要使用的工作表
'开始将表单值复制到Excel电子表格
'单元格(8,3)=第8行,第3列
oExcelWrSht.Cells(Rows.Count,1).End(xlUp).Offset(1,0)=Me.1
oExcelWrSht.Cells(Rows.Count,2).End(xlUp).Offset(1,0)=Me.2
oExcelWrSht.Cells(Rows.Count,3).End(xlUp).Offset(1,0)=Me.3
oExcelWrSht.Cells(Rows.Count,4).End(xlUp).Offset(1,0)=Me.4
oExcelWrSht.Cells(Rows.Count,5).End(xlUp).Offset(1,0)=Me.5
oExcelWrSht.Cells(Rows.Count,6).End(xlUp).Offset(1,0)=Me.6
oExcelWrSht.Cells(Rows.Count,7).End(xlUp).Offset(1,0)=Me.7
oExcelWrSht.Cells(Rows.Count,8).End(xlUp).Offset(1,0)=Me.8
oExcelWrSht.Cells(Rows.Count,9).End(xlUp).Offset(1,0)=Me.9
'... 等等
oExcelWrSht.Range(“A1”)。选择“返回页面顶部”
'oExcelWrkBk.Close True,sFileName'保存并关闭生成的工作簿
“”如果最初未运行,请关闭excel
'如果bExcelOpened=False,则
“oExcel,退出
'如果错误处理程序退出,则结束:
出错时继续下一步
oExcel.Visible=True“使excel对用户可见”
设置oExcelWrSht=Nothing
设置oExcelWrkBk=Nothing
oExcel.ScreenUpdating=True
设置oExcel=Nothing
退出子错误\u处理程序:
MsgBox“发生以下错误”&vbCrLf&vbCrLf&_
“错误编号:”&错误编号&vbCrLf&_
“错误源:Export2XLS”&vbCrLf&_
“错误说明:”&错误说明_
,vbOKOnly+vbCritical,“发生错误!”
恢复错误\u处理程序\u退出结束子节点

我已经尝试过了,没有问题,所以假设您有对正确excel库的引用,您能看看这是否有效吗

Sub Test()
Dim oExcel As Excel.Application
Dim oExcelWrkBk As Excel.Workbook
Dim oExcelWrSht As Excel.Worksheet

'Start Excel
On Error Resume Next
Set oExcel = GetObject(, "Excel.Application")
If Err <> 0 Then
    Err.Clear
    On Error GoTo Error_Handler
    Set oExcel = CreateObject("Excel.Application")
Else
    On Error GoTo Error_Handler
End If

oExcel.ScreenUpdating = False
oExcel.Visible = False 'This is false by default anyway

Set oExcelWrkBk = oExcel.Workbooks.Open("C:\test.xlsx")
Set oExcelWrSht = oExcelWrkBk.Sheets(1)

oExcelWrSht.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) = "Test1"
oExcelWrSht.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0) = "Test2"
oExcelWrSht.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0) = "Test3"

oExcelWrSht.Range("A1").Select

oExcelWrkBk.Save

oExcel.ScreenUpdating = True
oExcel.Visible = True

Exit_Point:
Set oExcelWrSht = Nothing
Set oExcelWrkBk = Nothing
Set oExcel = Nothing
Exit Sub

Error_Handler:
MsgBox Err & " - " & Err.Description
GoTo Exit_Point
End Sub
子测试()
Dim oExcel作为Excel.Application
将oExcelWrkBk作为Excel.工作簿进行调整
将oExcelWrSht设置为Excel.工作表
'启动Excel
出错时继续下一步
设置oExcel=GetObject(,“Excel.Application”)
如果错误为0,则
呃,明白了
关于错误转到错误处理程序
设置oExcel=CreateObject(“Excel.Application”)
其他的
关于错误转到错误处理程序
如果结束
oExcel.ScreenUpdating=False
oExcel.Visible=False'默认情况下这是False
设置oExcelWrkBk=oExcel.Workbooks.Open(“C:\test.xlsx”)
设置oExcelWrSht=oExcelWrkBk.Sheets(1)
oExcelWrSht.Cells(Rows.Count,1).End(xlUp).Offset(1,0)=“Test1”
oExcelWrSht.Cells(Rows.Count,2).End(xlUp).Offset(1,0)=“Test2”
oExcelWrSht.Cells(Rows.Count,3).End(xlUp).Offset(1,0)=“Test3”
oExcelWrSht.范围(“A1”)。选择
oExcelWrkBk.Save
oExcel.ScreenUpdating=True
oExcel.Visible=True
出口点:
设置oExcelWrSht=Nothing
设置oExcelWrkBk=Nothing
设置oExcel=Nothing
出口接头
错误\u处理程序:
MsgBox错误&“-”&错误说明
转到出口点
端接头

如果您使用的是数据库,为什么要将记录附加到Excel中?为什么不将记录存储在Access中(这是数据库的用途)并让Excel从数据库中提取所需的记录?因此每次运行此操作时,它肯定只会设置第10行的值?我错过了你试着把它移到下一排的尝试?@jkpieterse:我怎么能做到这一点?你能把我推到正确的方向吗?@Tim Edwards:对不起,正是代码将导出的数据写入第10行。我尝试使用“ActiveCell.Value”和“ActiveCell.Offset”(代码中未显示)转到下一行,但每次尝试都会从VBE中得到错误(我连续尝试了3天,但没有成功)。因此,我又回到了代码工作的地方。因此,为了下一行,您需要使用类似于
单元格(Rows.Count,10)。End(xlUp)。Offset(1,0)=“我的值”
,但将10更改为您想要的任何列。代码立即工作!非常感谢你。我对一些库引用有一些问题,例如Excel对象库和Microsoft Form 2.0。我添加了这些和错误。但有一个问题;将数据导出到Excel后,打开的Excel文件是否会自动保存,而不关闭Excel文件?是的,我会将其添加进去。再次感谢您用我有限的VBA知识帮助我。代码完全符合我的要求。酷。如果您觉得我已经充分解决了您的问题,那么您可以使用勾号将其标记为答案。只是说你是一个新用户,所以你可能不知道