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
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
if you want to use vba, the idea is macro will open specificity file each time when change value in B2 or C2, update data and close these files. is that OK?
 
Upvote 0
if you want to use vba, the idea is macro will open specificity file each time when change value in B2 or C2, update data and close these files. is that OK?
Hi, can it be pulled without the file is opened? If using VBA cant do that, any other method suggestion?
 
Upvote 0
in my knowledge, vba need open these files and get data from it then auto close them when done
 
Upvote 0
Oh okay, can you help me on how this can be done?
in that case, you need to show me folder type name that we need to get data files and what type of name files we need to get data or just get all excel file in that folder
 
Upvote 0
The folder is like in Picture 1. The excel files inside the folder in Picture 2. So data is pulled from different cells from the source , E5 is sum up in Picture 3

Picture 1:

1700626737481.png


Picture 2:
1700626783839.png


Picture 3:
1700626988480.png
 
Upvote 0
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
 
Upvote 0
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
Thank you! , will try this
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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