2017-05-26 136 views
0

我有12個文件夾(月),並且在每個月的文件夾中,每週都有一個文件夾。在每週的文件夾中都有一些excel文件。在每月文件夾名稱(每週更改)中打開每月文件夾名稱內的所有文件

在每週的基礎上,我需要打開本週文件夾內的所有文件,以便數據填充間接匹配公式。希望通過點擊按鈕來做到這一點...

有沒有辦法引用指向相關月份和周的路徑?例如。我更新了主文件中的一個單元格,它顯示了月份和星期,VBA指向那裏,並打開這些文件?我正在嘗試將一些VBA修改爲另一個主文件,我只保留在相同的文件夾位置。

Public Sub test() 
'DECLARE AND SET VARIABLES 
Dim wbk As Workbook 
Dim Filename As String 
Dim Path As String 
Path = "S:\Accounts\FP&A\1. Weekly Sales Figures\FY 18\" 
Filename = Dir(Path & "*.xlsm") 

'Optimize Macro Speed 
    Application.ScreenUpdating = False 
    Application.EnableEvents = False 
    Application.Calculation = xlCalculationManual 

'-------------------------------------------- 
'OPEN EXCEL FILES 
Do While Len(Filename) > 0 'IF NEXT FILE EXISTS THEN 
    Set wbk = Workbooks.Open(Path & Filename) 
    ' 
    ' CODE GOES HERE 
    ' 
    Filename = Dir 

ResetSettings: 
    'Reset Macro Optimization Settings 
    Application.EnableEvents = True 

Loop 

ThisWorkbook.Activate 

End Sub 

提前非常感謝, 奔

文件夾結構 後,將\ FY18 \(在文件路徑上面所示)我有12頁的文件夾(例如'03 - 2017' 年4月「例如'04。055.2017','11。055.2017','18。055.2017'

希望這可以幫助您Max?謝謝

+1

是的,這是可能的,但你給少一點信息。我需要知道文件夾的文件夾結構和語法。 – Max

+0

Hi Max,將在我的問題底部編輯... –

回答

1

你可以使用類似這樣的出發點來定義文件的路徑,

Dim MonthFolder as String 
    Dim DayFolder as String 
    Dim Path As String 

' These could be referenced from the Current Date or Cell Values, Input Boxes... 
' as long as they match your folder structure. 
    MonthFolder = May 
    DayFolder = 26 

    Path = "S:\Accounts\FP&A\" & MonthFolder & "\" & DayFolder & "\" 
相關問題