[英]Excel VBA - Import from a file in the same folder with an arbitrary name but a specific number at the end
I want to be able to import data from a file in the same folder, but it doesn't matter what it's called, but the number at the end does. 我希望能够从同一文件夹中的文件导入数据,但是调用的名称并不重要,但末尾的数字确实可以。 Currently i have
目前我有
Sub Import_Data()
Dim rng As Range
Dim WB2 As Workbook
Dim FName As String
Dim c1 As Worksheet
Set c1 = Sheets("c")
FName = Application.ActiveWorkbook.Path + "_w" & Format((WorksheetFunction.WeekNum(Now) - 1), "00")
Set WB2 = Workbooks.Open(Filename:=FName)
c1.Range("L2:O6").Value = WB2.Worksheets(3).Range("M2:P6").Value
c1.Range("L14:O18").Value = WB2.Worksheets(3).Range("M14:P18").Value
WB2.Close
End Sub
I want to change the following line from 我想将以下行从
FName = Application.ActiveWorkbook.Path + "_w" & Format((WorksheetFunction.WeekNum(Now) - 1), "00")
To something like 像
FName = Application.ActiveWorkbook.Path + *Any Name before the last 2 digits* & Format((WorksheetFunction.WeekNum(Now) - 1), "00")
So it will basically not look at the name of the sheet but will look at the 2 digits at the end. 因此,它基本上不会查看工作表的名称,而是会查看末尾的两位数。
The week numbers will always be unique in the folder but the names might be different. 周数在文件夹中将始终是唯一的,但名称可能有所不同。 Eg
例如
Folder may contain the following files: 文件夹可能包含以下文件:
Worksheet 1 w21
Different Name w22
Alternate w23
If we are in w24, i'd like it to import from "Alternate w23" 如果我们在w24中,我希望它从“ Alternate w23”中导入
I hope this makes sense. 我希望这是有道理的。 Thanks
谢谢
EDIT - Alternative 编辑-替代
If this isn't possible, It would also work if the previous weeks file had the same name as the current file. 如果这不可能,那么如果前几周的文件与当前文件具有相同的名称,则该文件也将起作用。 So if the active workbook was called "Workbook w23" Then it would find "Workbook w22" in the same folder.
因此,如果活动工作簿被称为“ Workbook w23”,则它将在同一文件夹中找到“ Workbook w22”。 But it would need to be possible to generalise the name of the sheet.
但是,可能需要概括表的名称。 So instead of the original suggestion of Any Name before the last 2 digits it would be The same name as the active workbook with 2 different digits at the end
因此,与原来的建议“最后一个2位数字之前的任何名称”不同 ,它将与活动工作簿的名称相同,末尾有2个不同的数字
如果我的解释正确,那么您只需要一个通配符:
FName = Dir(Application.ActiveWorkbook.Path & "\\*w" & Format((WorksheetFunction.WeekNum(Now) - 1), "00") & ".xlsm")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.