0

目前,我在运行 VBA 的访问表单中有一个按钮。此 VBA 创建一个文件夹,然后将文件从 access db 导出到该文件夹​​中的 excel 文件中。代码是这样的:

MkDir CurrentProject.Path & "\W46"
Dim outputlot3mah As String
outputlot3mah = CurrentProject.Path & "\" & "W46" & "\" & "Lot03_MAHs.xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Lot03_MAHs", outputlot3mah, True

有没有一种方法,当用户单击按钮时,他可以输入一个名称,这将是文件夹的名称?因此,例如,如果他选择 W78 作为名称,则文件夹命名为 W78,并且在该文件夹中输入文件。

那么它会是这样的

Dim getFolderName As String
MkDir CurrentProject.Path & getFolderName
Dim outputlot3mah As String
outputlot3mah = CurrentProject.Path & "\" & getFolderName & "\" & "Lot03_MAHs.xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Lot03_MAHs", outputlot3mah, True

我是 VBA 新手,所以我不知道这是否可能。谢谢!

4

1 回答 1

2

您可以使用输入框从用户那里获取字符串。

像这样的东西

Dim getFolderName As String

getFolderName = InputBox("Give me the name of the folder")

MkDir CurrentProject.Path & getFolderName
Dim outputlot3mah As String
outputlot3mah = CurrentProject.Path & "\" & getFolderName & "\" & "Lot03_MAHs.xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Lot03_MAHs", outputlot3mah, True

你还必须有一些错误控制:

  • 验证用户是否点击取消

  • 检查文件夹是否存在

  • 如果文件夹不存在,则创建该文件夹

于 2019-11-14T12:02:21.667 回答