Continue to next iteration in Do While Loop

vidyanand

New Member
Joined
Jul 19, 2014
Messages
32
Hello, I am trying to update files of one of specific folder. I am using Do While loop to update these files. But I want to add condition. If in these excel files there is particular sheet with previous month name then loop should skip that file & move to next.

I am not even amateur in VBA programming. So I check microsoft help which suggesting that I can use 'Continue do ' but when I used it in my code it is giving Syntax error. My code is as follows

Sub file_folder_update()


Dim folderPath As String
Dim filename As String
Dim wb As Workbook

folderPath = "C:\Users\Vidyanand\Documents\Error Test\" 'change to suit

If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"

filename = Dir(folderPath & "*.xlsx")
Do While filename <> ""
Application.ScreenUpdating = False
Set wb = Workbooks.Open(folderPath & filename)

sheetname = Format(Date - 30, "MMM YY")

Dim ws As Worksheet

Set ws = Sheets(sheetname)
If Not ws Is Nothing Then
continue do
Else
'Call a subroutine here to operate on the just-opened workbook
Call portfolio_worksheet_update
wb.Save
wb.Close
End If
filename = Dir
Loop
Application.ScreenUpdating = True
MsgBox "macro run finished"
End Sub


So could anyone please help me to correct this code?

Thanx in advance

Vidyanand
 
If you want to skip that file when a sheet with the previous month is in it, you will have to loop through each sheet and stop either when you reach the last sheet or find the sheet for last month?

I would be looking at initialising a flag after each file is opened.
Set this flag if the sheet is found, exit that loop early if found
If flag is set do not call the subroutine
loop for next file

HTH

I am not looping through sheets but files of the folder. Call macro is used to update those files. But before updating those files, I want to put condition where I want to know if any of the files have sheet with previous month name. If particular file does have such sheet then loop should skip that file for updating & move on to next file. I think this is what I have try to do in my original query.

I am not pro in VBA coding & nor I am aware of its syntax. So if possible could you please demonstrate your latest suggestion, taking my original code as example.
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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