Dir Exists Loop

geospatial

Active Member
Joined
Sep 2, 2008
Messages
290
I currently have the following VBA code
Code:
Dim directory as string
directory = "C:\New Folder\Working Folder\"
 
If Dir(directory & "Jan2011.xls") <> = "" Then
  workbooks.Open fileName:=
    "C:\New Folder\Existing\jan2011.xls"
 
Else 
If Dir(directory & "Feb.xls") <> = "" Then
  workbooks.Open fileName:=
    "C:\New Folder\Existing\feb2011.xls"

This works fine except I now would like it to loop. If it finds jan2011.xls open it and then if it find feb2011.xls it also opens it and continues doing this until it doesnt find the specified file.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I have a directory that has the files in it. For each month of the year a file is created so in August another file will be created called aug2011.xls. The way I do it now is copy the code from the previous month and then paste it and change the file name.

Never done a list of the file names and not sure what it would need to contain.
 
Upvote 0
Do you actually want to loop through all the files in the directory?
 
Upvote 0
If thats the easiest way to do it. Only the files I will need will be in that given directory.

What I am really wanting it to do is for each workbook in directory open the workbook delete row 1 shift data up then copy the remaining data to an empty worksheet. Then close that workbook open the next delete row 1 shift data up, copy the remaining data and paste that below the data in the workbook that was just pasted and keep doing that until I hit all the files in the directory.

I have done this before, but it was all the same code copied and pasted over and over again for each month. Trying to narrow it down and make it a lot easier to manage.
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,792
Members
452,942
Latest member
VijayNewtoExcel

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