• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Loop files in a specific order of a folder|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Loop files in a specific order of a folder|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosLoop files in a specific order of a…
sp_PrintTopic sp_TopicIcon
Loop files in a specific order of a folder
Avatar
sandy lin

New Member
Members

Power BI
Level 0
Forum Posts: 2
Member Since:
January 12, 2022
sp_UserOfflineSmall Offline
1
February 9, 2023 - 12:38 pm
sp_Permalink sp_Print

Hi All,

I have a folder named ABC and a file called H.xlsm inside. There is a subfolder named ABCs. Inside the folder, there are some excel files. My current codes will go to the ABC folders. Open each file, run the query, and then close and save the file. But now I need to open file A, then B and the rest of the files. The order for the rest of the file doesn't matter.

Maybe I can rename the file names A_1, B_2, C_3, and D_4. So it can sort in ascending order. I know there is a function dir.

A.xlsm

B.xlsm

C.xlsm

D.xlsm

 

Sub RefreshABCs(Folder)

   Dim File

     Dim Worksheet

For Each File In Folder.Files

      Workbooks.Open (File), False

      Dim lCnt As Long

       With ActiveWorkbook

       For lCnt = 1 To .Connections.Count

      If .Connections(lCnt).Type = xlConnectionTypeOLEDB Then

    .Connections(lCnt).OLEDBConnection.BackgroundQuery = False

   End If

     Next lCnt

End With 

ActiveWorkbook.RefreshAll

ActiveWorkbook.Save

ActiveWorkbook.Close

Next

End Sub

 

Thank you so much!

Avatar
sandy lin

New Member
Members

Power BI
Level 0
Forum Posts: 2
Member Since:
January 12, 2022
sp_UserOfflineSmall Offline
2
February 9, 2023 - 3:20 pm
sp_Permalink sp_Print

I check Chatgpt

Sub LoopThroughFiles()

Dim myFile As String

Dim myDir As String

Dim i As Integer

' Specify the directory containing the files myDir = "C:\Your\File\Path\"

' Loop through the files in the directory i = 1

myFile = Dir(myDir & i & ".xlsx") Do While myFile <> ""

' Open the file and perform your operations here

Workbooks.Open (myDir & myFile)

' Add your code here to process the file

' Close the file Workbooks(myFile).Close SaveChanges:=False

' Move to the next file

i = i + 1 myFile = Dir(myDir & i & ".xlsx") Loop

End Sub

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Alexandra Radu
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 871
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Raj Mattoo
Mark Luke
terimeri dooriyan
Jack Aston
AndyC
Denise Lloyd
michael serna
mashal sana
Tiffany Kang
Leah Gillmore
Forum Stats:
Groups: 3
Forums: 24
Topics: 6219
Posts: 27276

 

Member Stats:
Guest Posters: 49
Members: 31903
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.