Loop Without Do Error and Do/While with multiple conditions

Berglund

New Member
Joined
Oct 3, 2016
Messages
11
Hi all,

I am having trouble taking my macro from a basic single loop to multi-loop based on certain conditions. The first loop below works properly but I cannot get a second and third identical loop to work without giving a "Loop Without Do" error. In my second and third loops I am replacing the "var" variable with var2 and var3, otherwise the code and variables remain the same.

I've searched multiple forums but I cannot identify where I have gone wrong. Any help would be great!

Code:
' Set path to folder
Const MyPath As String = "c:/example/
' Get first file in folder that includes var variable in name
MyFile = Dir(MyPath & "*" & var & "*")
' Loop through until no files remain
Do While Len(MyFile) > 0
' Set variable as last modified date of current file
LMD = Format(FileDateTime(MyPath & MyFile), "Short Date")
' Compare last modified date of file to input date from user, if equal, add 1 to counter
If LMD = dteFile Then
ctr = ctr + 1
End If
' Get next file from the folder
MyFile = Dir
Loop

' Get first file in folder that includes var2 variable in name
MyFile = Dir(MyPath & "*" & var2 & "*")
' Loop through until no files remain
Do While (Len(MyFile) > 0 And Range("B1") > 24)
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Update: I found my missing End If that was causing the "Loop Without Do" error but I am not able to get the macro to go into the second (and subsequent loops) and I cannot figure out why. I am largely reusing variables, is that causing the issue? Is it an issue with the way the loop is structured?
 
Upvote 0
Can you explain, in words, what the code is supposed to do?
 
Upvote 0

Forum statistics

Threads
1,215,231
Messages
6,123,754
Members
449,119
Latest member
moudenourd

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