每天工作簿名称更改时在VBA中引用工作簿的最佳方法?

问题描述 投票:0回答:2

当名称每天更改时,从另一个工作簿引用工作簿的最佳方法是什么?我有一个引用多个工作表的工作簿,我的目标是每天下载报告时提取新数据,但报告名称发生变化(例如InvoiceSoldReport2019-4-15 [兼容模式]与InvoiceSoldReport2019-4-16 [兼容模式] )。

excel vba excel-formula
2个回答
1
投票

如果您知道,只有工作簿名称中的日期更改,那么您可以动态地将名称中的日期包含为字符串。喜欢:

Dim current_date as Date
Dim wb_name as string
Dim wb_open as Workbook
...
date = ...
wb_name = "InvoiceSoldReport"
Set wb_open = Workbooks.Open(wb_name & date) 'concatenate name and date
....

0
投票

你可以在工作中使用文件选择器。

Dim wb2 As Workbook

Dim fdl As FileDialog
Dim FileChosen As Integer

Set fdl = Application.FileDialog(msoFileDialogFilePicker)

fdl.Title = "Please Select the XXX file"
'Set the InitialFile Path
fdl.InitialFileName = "D:\"
'Set the Folder View
fdl.InitialView = msoFileDialogViewSmallIcons
'Set the filter
fdl.Filters.Clear
fdl.Filters.Add "XLSX", "*.XLSX"
'Optional if you know the file type is constant, else no need of filter
FileChosen = fdl.Show

If FileChosen <> -1 Then
'Not choosen anything / Clicked on CANCEL
MsgBox "No file choosen"

Else
'fdl.SelectedItems(1) display name and complete path of file chosen
 Set wb2 = Workbooks.Open(fdl.SelectedItems(1))
End If
© www.soinside.com 2019 - 2024. All rights reserved.