Running code for all workbooks in excel

Status
Not open for further replies.

Camus

Active member
Joined
Oct 4, 2007
Messages
28
Programming Experience
Beginner
I'm using this code within an excel macro to cycle through all workbooks in a directory and execute code within each one -


VB.NET:
Dim wb As Workbook
Dim TheFile As String
Dim MyPath As String
MyPath = "C:\Temp"
ChDir MyPath
TheFile = Dir("*.csv")

Do While TheFile <> ""
  Set wb = Workbooks.Open(MyPath & "\" & TheFile)


[I]code to be executed here[/I]


  wb.Close
  TheFile = Dir

This code is obviously set up to look at a specific directory but the directories I want to use are constantly changing.

Is it possible to modify it so that it looks at the folder that is currently in use? So when I open a single excel file and i hit the macro, it will cycle through the other files within that same folder?
 
This is a VB.Net forum, to get help with VBA I advise you to seek VBA forums.
 
Status
Not open for further replies.
Back
Top