作者:哒哒愛嘬萌 | 来源:互联网 | 2023-09-11 17:40
SoIamusingVBAinAccesstocreatelinkedtablesbetweenExcelandAccess.Simpleenoughandass
So I am using VBA in Access to create linked tables between Excel and Access. Simple enough and as some online resources guided me I decided to utilize the TransferSpreadsheet command. So I ran some code to test out if I had the syntax correct and got this
所以我在Access中使用VBA在Excel和Access之间创建链接表。很简单,当一些在线资源引导我时,我决定使用TransferSpreadsheet命令。所以我运行了一些代码来测试我的语法是否正确并得到了这个
DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel12, _
"Link Name", "File Path", True, "Sheet Name!"
So that worked perfectly, but I wanted to automate it so someone who doesn't understand how to code can use the function. So for the file path I set up a file dialog box to come up so the user can select the excel file. Again worked great.
所以这很好用,但我想自动化它,所以不懂代码的人可以使用这个功能。因此,对于文件路径,我设置了一个文件对话框,以便用户可以选择excel文件。再次工作得很好。
So now to the point, I want to create a dialog box for users to select the excel sheet to link as well. So essentially the user would select the excel file first and then select from a drop down box the sheet they want to link. Is this possible? If so how would I go about doing it. Here is my code so far:
所以到现在为止,我想创建一个对话框供用户选择要链接的Excel工作表。因此,用户基本上首先选择excel文件,然后从下拉框中选择他们想要链接的工作表。这可能吗?如果是这样,我将如何去做呢。这是我到目前为止的代码:
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
To add to this further I was attempting to utilize this code I found to get the names but I'm unsure how to store them as variables to use.
为了进一步增加这一点,我试图利用我发现的代码来获取名称,但我不确定如何将它们存储为要使用的变量。
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
What the above code does is list out each sheet name into the debug window, I just am finding it difficult to push those values to an array.
上面的代码所做的是将每个工作表名称列入调试窗口,我发现很难将这些值推送到数组中。
1 个解决方案