Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何允许用户使用组合框选择要导入access的excel工作表?_Excel_Ms Access_Vba - Fatal编程技术网

如何允许用户使用组合框选择要导入access的excel工作表?

如何允许用户使用组合框选择要导入access的excel工作表?,excel,ms-access,vba,Excel,Ms Access,Vba,我创建了一个userform,用于将单个excel工作表导入到特定的excel表中。但是,我需要导入的工作簿包含多个工作表,每个工作表上的工作表是每月的31天。是否有方法在用户窗体上创建组合框,以允许用户使用VBA选择要导入access表的工作表 这是我的密码: Private Sub btnBrowse_Click() Dim diag As Office.FileDialog Dim item As Variant Set diag = Application.Fi

我创建了一个userform,用于将单个excel工作表导入到特定的excel表中。但是,我需要导入的工作簿包含多个工作表,每个工作表上的工作表是每月的31天。是否有方法在用户窗体上创建组合框,以允许用户使用VBA选择要导入access表的工作表

这是我的密码:

Private Sub btnBrowse_Click()
    Dim diag As Office.FileDialog
    Dim item As Variant

    Set diag = Application.FileDialog(msoFileDialogFilePicker)
    diag.AllowMultiSelect = False
    diag.Title = "Please select an Excel Spreadsheet"
    diag.Filters.Clear
    diag.Filters.Add "Excel Spreadsheet", "*.xls, *.xlsx"
    If diag.Show Then
        For Each item In diag.SelectedItems
            Me.txtFileName = item
        Next
    End If
End Sub

Private Sub btnImportSpreadsheet_Click()
    Dim FSO As New FileSystemObject

    If Nz(Me.txtFileName, "") = "" Then
        MsgBox "Please select a file!"
        Exit Sub
    End If
    If FSO.FileExists(Nz(Me.txtFileName, "")) Then
        If MsgBox("Do you want to import this file?", vbYesNo) = vbYes Then
            DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "Dyeing", Me.txtFileName, True
            MsgBox "File Imported"
        Else
            MsgBox "Please select file again"
        End If
    End If
End Sub

例如,当用户选择xls文件时,您需要读取所述的工作表名称列表,并将这些名称添加到combobox:创建一个具有行源类型=
值列表
的combobox,并将名称列表作为字符串添加到combobox
行源
属性中。在更新后的
组合框事件中导入所选工作表。使用TransferSpreadsheet函数的范围参数选择工作表。别忘了添加
到工作表名称的末尾:

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "Dyeing", Me.txtFileName, True, strSelectedWorksheetName & "!"

向我们显示您迄今为止所做的VBA代码。专用子btnBrowse_Click()将diag设置为Office.FileDialog将Dim项设置为变量集diag=Application.FileDialog(msoFileDialogFilePicker)diag.AllowMultiSelect=False diag.Title=“请选择Excel电子表格”diag.Filters.Clear diag.Filters.Add“Excel电子表格”,“*.xls,*.xlsx”如果diag.Show然后为diag.SelectedItems Me.txtFileName=项目下一个End如果End子私有子btnImportSpreadsheet_Click()将FSO设置为新文件系统对象如果Nz(Me.txtFileName,”“”“”),则MsgBox“请选择一个文件!”如果FSO.FileExists(Nz(Me.txtFileName,“”),则退出子端;如果MsgBox(“是否要导入此文件?”,vbYesNo)=vbYes,则退出子端;如果MsgBox(“是否要导入此文件?”,vbYesNo)=vbYes,则DoCmd.TransferSpreadsheet acImport,acSpreadsheetTypeExcel12,“染色”,Me.txtFileName,True MsgBox“已导入文件”否则MsgBox“请再次选择文件”如果结束,则结束如果结束子