Hi Everyone,
I am after some help please!
I am trying to nest a do loop within a do loop so that I can generate multiple iterations. ie the second do loop executes for every loop of the first loop.
I want to do this because i am trying to write a macro that scans a file system for a folder with a time stamp in the file name (have to use the file name - cannot use date modified) and need to loop backwards through all the possible combinations of hours, days, months and years until the latest folder is found.
The code that I have written with help doesn’t display any results when I execute it.
When I comment out each do loop in turn so that there is only one loop executing the message "...is a valid folder/path." is displayed but the folder that the message specifies does not exist when I check
Please help!
![]()
Sub FolderExists() Dim fso AsObject Dim day AsInteger, month2 AsInteger, year AsInteger Dim month3 AsString Dim hour AsInteger Dim partial AsString Dim folder AsString day = 30 year = 2010 hour = 18 month2 = 12 Do month3 = Format(DateSerial(year, month2, 1), "mmm") Set fso = CreateObject("Scripting.FileSystemObject") Do partial = "\\cefileserver\groups\EMO\Forecasting\Elec\ShortTerm\Forecasts\Half Hourly/" folder = partial & day & month3 & year & "_" & hour If fso.FolderExists(folder) Then MsgBox folder & " is a valid folder/path.", vbInformation, "Path Exists" Else EndIf hour = hour - 1 Loop Until fso.FolderExists(folder) Or hour < 9 If fso.FolderExists(folder) Then MsgBox folder & " is a valid folder/path.", vbInformation, "Path Exists" Else EndIf month2 = month2 - 1 Loop Until fso.FolderExists(folder) Or month2 < 0 End Sub











LinkBack URL
About LinkBacks
Register To Reply

Bookmarks