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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

smakatura

Board Regular
Joined
May 27, 2011
Messages
141
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
 

Forum statistics

Threads
1,137,154
Messages
5,679,911
Members
419,862
Latest member
Bluewings666

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
Top