简体   繁体   中英

Vba code for 1 field from access form to excel

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

Please do search before submitting a question in StackOverflow. There a multiple answers to this question and you did not include sufficient background too.
Is your Excel open?
Is your Excel even created?
I I'll attach the code to create a new instance of Excel, and write text to a specific cell.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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