简体   繁体   English

Excel VBA-从同一文件夹中的文件导入,并带有任意名称,但末尾有特定编号

[英]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.

相关问题 Excel VBA:将excel列从一个文件复制到相同名称的另一个文件(在不同文件夹中) - Excel VBA: copy excel columns from one file to other file (in different folder) of same name VBA根据不同工作表上单元格中的名称将图表从excel选项卡中拉到特定的文件夹和名称文件 - VBA to exort chart from excel tab to specific folder and name file based on a name in a cell on a different worksheet 使用VBA将特定行从文本文件导入Excel电子表格 - Import specific lines from text file into Excel spreadsheet using VBA 使用Excel VBA从SharePoint导入时使用部分文件名 - Use Partial file name on import from SharePoint with Excel VBA Excel VBA忽略目录中的特定文件夹/文件 - excel VBA ignore specific folder/file in directory Excel VBA检查并显示文件夹中的文件数 - Excel VBA check and display number of file in folder VBA:默认情况下在特定文件夹中使用名称保存文件 - VBA: Save a file with a name by default in a specific folder 使用VBA将特定文件夹中的多个Excel工作簿导入到单个工作表中 - Import Multiple Excel Workbook From Specific Folder Into Single Worksheet Using VBA 从 Excel VBA 中的路径获取文件夹名称 - Get folder name from path in Excel VBA vba excel-列出文件夹中文件创建日期与星期几和星期几名称匹配的文件吗? - vba excel - list files in folder where file created date matches week number and weekday name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM