vba - MS Access VBA acImport 工作表名称,带空格和范围

标签 vba ms-access import ms-access-2010 spreadsheet

我正在构建一个带有名为“Go”的按钮的表单,以从 Excel 文件中导入特定范围的特定 Excel 工作表。另一个子/函数负责在名为 text_filepathdata 的字段中的表单上保存文件名。

我发现工作表名称中的空格有问题,但是可以通过使用特殊引号来解决。

Dim sheetname As String
sheetname = "'Commit Volumes!'"
DoCmd.Transferspreadsheet acImport, acSpreadsheetTypeExcel12, _
    "lcl_ImportedFile", text_filepathdata, True, sheetname

这有效,但由于特殊引号,我无法指定除工作表名称之外的范围......(即“'Commit Volumes!A6:Z5000'”不起作用,两者的串联也不起作用像“'提交卷'”和“A6:Z5000”)

我已经尝试了各种引用和连接方式,但唯一有效的是:
sheetname = "'Commit Volumes!'"
sheetname = "'Commit Volumes$'"
sheetname = "CommitVolumes!A6:Z5000"

无法修改 Excel 表,因为它来自另一个系统,并且文件(更新后)必须上传回该系统。

这是我的代码:
Private Sub button_Go_Click()
On Error GoTo Err_button_Go_Click

Dim sheetname As String
sheetname = "'Commit Volumes!'"

If IsNull(text_filepathdata) Or text_filepathdata = "" Then
    MsgBox "Please browse and select a valid file to import.", _
        vbCritical, "Invalid File"
Else
    DoCmd.OpenForm "f_PleaseWait_Processing", acNormal, , , acFormReadOnly, acWindowNormal
    If ifTableExists("lcl_ImportedFile") = True Then
        DoCmd.DeleteObject acTable, "lcl_ImportedFile"
    End If
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12,_
        "lcl_ImportedFile", text_filepathdata, True, sheetname

'Call module to process the imported data
        Call MainProcess
    End If

Exit_button_Go_Click:
    Exit Sub

Err_button_Go_Click:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_button_Go_Click

End Sub

根据问题,我收到两种不同的错误消息。

如果名称中有空格,我会得到:运行时错误 3129:无效的 SQL 语句;应为“删除”、“插入”、“过程”、“选择”或“更新”。

如果名称有问题,否则我会得到:运行时错误 3011:Microsoft Access 数据库引擎找不到对象“Commit Volumes$A1:Z5000”。确保该对象存在并且您正确拼写了它的名称和路径。如果“Commit Volumes$A1:Z5000”不是本地对象,请检查您的网络连接或联系服务器管理员。

任何帮助将不胜感激!

账单

我尝试过的变化:
sheetname = "'Commit Volumes'!A6:Z5000"; 
sheetname = "'Commit Volumes!'A6:Z5000" 
sheetname = "'Commit Volumes$'A6:Z5000" 
sheetname = "'Commit Volumes'$A6:Z5000" 
sheetname = "'Commit Volumes'$A$6:$Z$5000" 
sheetname = "'Commit Volumes!'$A$6:$Z$5000" 
sheetname = "'Commit Volumes$'$A$6:$Z$5000"

最佳答案

如果文件以已知\集合格式从其他系统导出,您可以通过索引而不是名称来引用它:

例如:sheets(1)、sheets(2)。然后添加范围。

关于vba - MS Access VBA acImport 工作表名称,带空格和范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16066903/

相关文章:

vba - Excel/VBA : Open New Workbook In New Window

vba - 微软 Access : Import CSV file from a list of multiple files

forms - MsAccess 表单 - 最初为空记录,但一旦数据输入开始,就需要强制填写

ms-access - 链接表只读

python - 进口声明的地方

typescript - 引用路径不使用导入捕获接口(interface) - IntelliJ

VBA:如何获取使用新的自动保存功能的工作簿的本地文件路径(因此在技术上是从 OneDrive 打开的)?

Excel VBA : referencing another worksheet using a cell value

vba - Application.OnTime 的多个变量参数

python - 无法导入 random.randint