excel - 如何从vba写入excel

标签 excel vba ms-access

我正在尝试在我的 Access 表单上创建一个按钮,该按钮采用当前条目并将信息写入 Excel 文档

Public Sub ExportExcel()
Dim ObjEx As Object
Dim WhatIsThisVariableFor As Object
Dim Selec As Object
Dim Column As Integer
Dim Row As Integer

Dim CustName As String
Dim Org As String
Dim Contact As String
Dim Product As String
Dim Quantity As Integer

Dim rst As DAO.Recordset

Set ObjEx = CreateObject("EXCEL.APPLICATION")
Set WhatIsThisVariableFor = ObjEx.Workbooks.Add

'Set rst = CurrentDb.OpenRecordset("Select") <---- This will be used when I get the code working
Column = 1
Row = 1
CustName = "Test" '<---- This is only used for the test

Cells(Row, Column) = CustName

ObjEx.Visible = True

Set Selec = ObjEx.Selection
End Sub

此代码创建 Excel 文档,但将其留空。我认为 Cells(Row, Column)命令不起作用,因为它必须从 excel 中调用?我不确定(我对 VBA 很陌生)

如何从 Access 中写入电子表格单元格?

最佳答案

您需要验证您的 Cells() Excel 的应用程序对象的函数。

ObjEx.ActiveSheet.Cells(Row, Column).Value = CustName

我建议您也明确选择工作表对象:
Dim ws As object
Set ws = ObjEx.Worksheets("Sheet1")

ws.Cells(row, column).value = CustName

关于excel - 如何从vba写入excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54294051/

相关文章:

excel - 在 Excel Power Query 中添加唯一索引

Excel:显示从开始日期和结束日期生成的月份名称集合?

ms-access - MS Access 中的每个表都需要主键吗?

ms-access - rs.MoveFirst 是否隐含在 Access VBA 中?

vba - 如何在 VBA Excel 中使用 sql FULL OUTER JOIN 查询

Java Apache POI 错误

regex - VBA 宏运行时错误

python - 使用 Python xlsxwriter 在工作簿中运行 VBA 宏

excel - VBA删除事件工作表

sql - 如何以一张表为基础连接Microsoft Access中的4张表?