Get data from dynamic location and filename on closed workbook using VBA

Joe006

New Member
Joined
Nov 20, 2023
Messages
23
Office Version
  1. 2016
Platform
  1. Windows
1700555158018.png


Hi, I got a lot of excel files in different location where each files is put in folder named month and year. I need to get the data from it while it's closed and data is pulled based on the drop down list for the month and the year. I have tried using the INDIRECT function and it work only when the file is opened. I just discovered that need to use VBA script to pull while it closed, if using VBA, how to call the data and set formula for the folder name and file dynamically according the chosen drop down list ?

Any helps much appreciated, Thank you
 
i dont know how you put data into summary worksheet then i create macro to open each file in match folder and recalculate summary sheet and close those file after done:
VBA Code:
Private Sub GetFiles()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Dim pFol As String, xcode As String
    Dim fso As Object, fol As Object, subFol As Object, fil As Object
    Dim filPath As String
    Dim wb As Workbook
    pFol = "C:\Users\Desktop\testforstranger" 'change it to your OT Management path"
    xcode = Format(ThisWorkbook.Sheets(1).Range("B2").Value, "00") & ThisWorkbook.Sheets(1).Range("C2").Value 'change ThisWorkbook.Sheets(1) to your summary sheet
    Debug.Print xcode
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fol = fso.GetFolder(pFol)
    For Each subFol In fol.SubFolders 'loop through each folder in your parent folder to find match folder with month and year
        If subFol.Name = xcode Then
            For Each fil In subFol.Files
                filPath = fso.GetAbsolutePathName(fil)
                If filPath Like "*.xlsx" Then 'open each excel file and recalculate summary worksheet
                    Set wb = Workbooks.Open(filPath)
                    ThisWorkbook.Sheets(1).Calculate  'change ThisWorkbook.Sheets(1) to your summary sheet
                    Debug.Print wb.Name
                    wb.Close (False)
                End If
            Next fil
        End If
    Next subFol
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub
VBA Code:
xcode = Format(ThisWorkbook.Sheets("Summary").Range("B2").Value, "00") & ThisWorkbook.Sheets("Summary").Range("C2").Value

is this correct syntax for Summary sheet
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
VBA Code:
xcode = Format(ThisWorkbook.Sheets("Summary").Range("B2").Value, "00") & ThisWorkbook.Sheets("Summary").Range("C2").Value

is this correct syntax for Summary sheet
that sheet in the picture you attach, if that sheet name as "Summary" then it correct
 
Upvote 0
View attachment 102237

Hi, I got a lot of excel files in different location where each files is put in folder named month and year. I need to get the data from it while it's closed and data is pulled based on the drop down list for the month and the year. I have tried using the INDIRECT function and it work only when the file is opened. I just discovered that need to use VBA script to pull while it closed, if using VBA, how to call the data and set formula for the folder name and file dynamically according the chosen drop down list ?

Any helps much appreciated, Thank you
and i saw that E5 equals to sum of all F6 in sheet name "Summary" of input files, if it true then you can use this code:
VBA Code:
Private Sub GetFiles()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Dim pFol As String, xcode As String
    Dim fso As Object, fol As Object, subFol As Object, fil As Object
    Dim filPath As String
    Dim wb As Workbook
    ThisWorkbook.Sheets(1).Range("E5").Value = Empty
    pFol = "C:\Users\Desktop\testforstranger" 'change it to your OT Management path"
    xcode = Format(ThisWorkbook.Sheets(1).Range("B2").Value, "00") & ThisWorkbook.Sheets(1).Range("C2").Value 'change ThisWorkbook.Sheets(1) to your summary sheet
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fol = fso.GetFolder(pFol)
    For Each subFol In fol.SubFolders 'loop through each folder in your parent folder to find match folder with month and year
        If subFol.Name = xcode Then
            For Each fil In subFol.Files
                filPath = fso.GetAbsolutePathName(fil)
                If filPath Like "*.xlsx" Then 'open each excel file and recalculate summary worksheet
                    Set wb = Workbooks.Open(filPath)
                    ThisWorkbook.Sheets(1).Range("E5").Value = ThisWorkbook.Sheets(1).Range("E5").Value + wb.Sheets("Summary").Range("F6").Value 'change ThisWorkbook.Sheets(1) to your summary sheet
                    wb.Close (False)
                End If
            Next fil
        End If
    Next subFol
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
VBA Code:
xcode = Format(ThisWorkbook.Sheets("Summary").Range("B2").Value, "00") & ThisWorkbook.Sheets("Summary").Range("C2").Value

is this correct syntax for Summary sheet
1700638982595.png


I run and comes out error, debug show at xcode line
 
Upvote 0
then you can keep
VBA Code:
ThisWorkbook.Sheets(1)
, no need to modify it
 
Upvote 0
then you can keep
VBA Code:
ThisWorkbook.Sheets(1)
, no need to modify it
Ok it runs, if i want to only pull data with no calculation for the other cell like B6, need to put code outside the loop right?
 
Upvote 0
View attachment 102298
B6:E6 is the cell that only pulls the data, how can i only pull into those cell?
to clearly, i want to confirm that E2 equals with sum of all F6 values in input data files and B6 to E6 equals with values of C12 to F12 in file that named as "90XX SMod.xlsx". Is that true?
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top