nested do-while loop

smakatura

Board Regular
Joined
May 27, 2011
Messages
141
I have multiple folders(level 1) with folders (level 2) in them. I want to delete the level 2 folders (but not all of them).

so I want to go in to folder 1 and delete folders a-g (but not h-z), then move to folder 2 and repeat the same thing.

I have the list of the folder1's in column a of a document and a1 is the active cell.

below is the code I wrote for it.

HTML:
Dim fso As Object
Dim fullfilelocation As String
Dim rptmonth As String
Dim rptday As Integer
Dim rptyear As String
Dim foldername As String
Dim filelocation As String
rptmonth = "06"
rptday = 1
rptyear = "11"
Set fso = CreateObject("Scripting.FileSystemObject")
foldername = ActiveCell.Value

Do Until IsEmpty(ActiveCell.Offset(1, 0))
    foldername = ActiveCell.Value
    fullfilelocation = "C:\Users\smakatura\Documents\call report\prep documents\test\" & foldername & "\" & rptmonth & "-" & rptday & "-" & rptyear
    ActiveCell.Offset(1, 0).Select
    MsgBox (fullfilelocation)
     Do Until rptday = 32
        If rptday < 10 Then
             fullfilelocation = "C:\Users\smakatura\Documents\call report\prep documents\test\" & foldername & "\" & rptmonth & "-0" & rptday & "-" & rptyear
             Else: fullfilelocation = "C:\Users\smakatura\Documents\call report\prep documents\test\" & foldername & "\" & rptmonth & "-" & rptday & "-" & rptyear
        End If
        
        
        If fso.FolderExists(fullfilelocation) Then
            fso.DeleteFolder (fullfilelocation)
        End If
        
        
     
        rptday = rptday + 1
     Loop
     rptday = 1
Loop
 
 
 
Set fso = Nothing

the trouble I am having is the second do-while loop. it will change the rptday but is not deleting the files.

prior to adding the going through the folders the deleting do-while loop worked. and the first do-while loop appears to be working.

any suggestions
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
please ignore the code above, below is the coding....and just an fyi, cell a1 had the words heading in it so that when it increments before starting the loops it is on the first folder name.

HTML:
Dim fso As Object
Dim fullfilelocation As String
Dim rptmonth As String
Dim rptday As Integer
Dim rptyear As String
Dim foldername As String
Dim filelocation As String
rptmonth = "06"
rptday = 1
rptyear = "11"
Set fso = CreateObject("Scripting.FileSystemObject")
 
Do Until IsEmpty(ActiveCell.Offset(1, 0))
    ActiveCell.Offset(1, 0).Select
    foldername = ActiveCell.Value
    
     Do Until rptday = 32
        If rptday < 10 Then
             fullfilelocation = "C:\Users\smakatura\Documents\call report\prep documents\test\" & foldername & "\" & rptmonth & "-0" & rptday & "-" & rptyear
             Else: fullfilelocation = "C:\Users\smakatura\Documents\call report\prep documents\test\" & foldername & "\" & rptmonth & "-" & rptday & "-" & rptyear
        End If
                       
        If fso.FolderExists(fullfilelocation) Then
            fso.DeleteFolder (fullfilelocation)
        End If
                   
        rptday = rptday + 1
     Loop
     rptday = 1
     
     
Loop
 
 
 
Set fso = Nothing
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

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