允许访问用户selectExcel工作表进行链接

所以我在Access中使用VBA创buildExcel和Access之间的链接表。 足够简单,一些在线资源引导我,我决定使用TransferSpreadsheet命令。 所以我跑了一些代码来testing,如果我有正确的语法,并得到这个

DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel12, _ "Link Name", "File Path", True, "Sheet Name!" 

所以这个工作完美,但我想自动化,所以不明白如何编码的人可以使用该function。 所以对于文件path,我设置了一个文件对话框,用户可以selectexcel文件。 再次工作很好。

所以现在到了这一步,我想创build一个对话框让用户selectexcel工作表链接以及。 所以基本上用户将首先selectexcel文件,然后从下拉框中select他们要链接的工作表。 这可能吗? 如果是的话,我将如何去做。 这是我的代码到目前为止:

 Public Sub linksheet() Dim fd As FileDialog Dim strpath As String Set fd = Application.FileDialog(msoFileDialogFilePicker) fd.AllowMultiSelect = False fd.Title = "Select Routing File" 'get the number of the button chosen Dim FileChosen As Integer FileChosen = fd.Show If FileChosen <> -1 Then Else strpath = fd.SelectedItems(1) DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel12, _ "Test Link", strpath, True, "Current!" End If End Sub 

为了进一步添加,我试图利用这个代码,我发现获得的名称,但我不确定如何将它们存储为variables使用。

 Public Function WorkSheetNames(strwspath As String) As Boolean Dim xlApp As Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet Dim strarray(25) Set xlApp = CreateObject("Excel.application") Set xlBook = xlApp.Workbooks.Open(strwspath, 0, True) For Each xlSheet In xlBook.Worksheets Debug.Print xlSheet.Name Next xlSheet xlBook.Close False xlApp.Quit Set xlBook = Nothing Set xlApp = Nothing Set xlSheet = Nothing End Function 

上面的代码做的是列出每个表名到debugging窗口,我只是发现很难将这些值推到一个数组。

我不明白为什么你需要将表格名称存储在一个数组中。 你可以将它们作为一个列表存储在一个stringvariables中。 然后,您可以将该string分配为combobox的RowSource属性,从而允许用户select其中一个可用表单。

 Dim strSheets As String ' adapt your existing code to use this ... For Each xlSheet In xlBook.Worksheets 'Debug.Print xlSheet.Name strSheets = strSheets & ";" & xlSheet.Name Next xlSheet ' discard leading semi-colon strSheets = Mid(strSheets, 2) 

收集表单名称后,将其应用为combobox源。

 ' ComboName should have Value List as Row Source Type Me.ComboName.RowSource = strSheets