简体   繁体   English

从访问表单到 excel 的 1 个字段的 Vba 代码

[英]Vba code for 1 field from access form to excel

我可以放在访问表单按钮中的最简单的 VBA 代码是什么,该按钮将字段值导出到 excel 中的指定单元格?

Please do search before submitting a question in StackOverflow.请在 StackOverflow 中提交问题之前进行搜索。 There a multiple answers to this question and you did not include sufficient background too.这个问题有多种答案,而且您也没有提供足够的背景知识。
Is your Excel open?你的Excel打开了吗?
Is your Excel even created?您的 Excel 是否已创建?
I I'll attach the code to create a new instance of Excel, and write text to a specific cell.我将附加代码以创建 Excel 的新实例,并将文本写入特定单元格。

Private Sub Button_Click()
Dim objExcel As Excel.Application
Dim objSheet As Excel.Workbook

Set objExcel = New Excel.Application
Set objSheet = objExcel.Workbooks.Open("C:\MyExcelfile.xlsx")

objExcel.Range("C6").Value = "Some text to write here"
objExcel.Range("D7").Value = me.textboxSomething.value

objSheet.save
Set objSheet = Nothing
objExcel.Quit
Set objExcel = Nothing
End sub

You have to deal with error handling in case of error.如果出现错误,您必须处理错误处理。
Do note you have to add "Microsoft Excel 1#.0 Object library" reference, in your access vba editor, under tools menu.请注意,您必须在访问 vba 编辑器的工具菜单下添加“Microsoft Excel 1#.0 对象库”引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM