Results 1 to 3 of 3

Nested Do Loops

Threaded View

ross88guy Nested Do Loops 12-08-2010, 05:23 AM
snb Re: Nested Do Loops 12-08-2010, 05:35 AM
romperstomper Re: Nested Do Loops 12-08-2010, 09:10 AM
  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

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