+ Reply to Thread
Results 1 to 3 of 3

Nested Do Loops

Hybrid View

  1. #1
    Registered User
    Join Date
    12-08-2010
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    14

    Nested Do Loops

    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
    Last edited by romperstomper; 12-08-2010 at 09:10 AM. Reason: code tags

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Nested Do Loops

    Your solution is here.



  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,013

    Re: Nested Do Loops

    Please note that when posting code on the forum you must use code tags. I have added them for you on this occasion as it's your first post, but please use them in future.
    Everyone who confuses correlation and causation ends up dead.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1