Next usage in For Each Loop

jaybee3

Active Member
Joined
Jun 28, 2010
Messages
307
Code:
 Dim oFile As Object, oFolder As Object
 
Set oFolder = CreateObject("scripting.filesystemobject").GetFolder("C:\")
For Each oFile In oFolder.Files
    If InStr(1, oFile.Name, ".pdf", vbTextCompare) Then MsgBox oFile.Path Else: Next oFile
...
Next oFile
End Sub

Naturally the above will throw up a "Next w/o For" error. Is there anyway to have a oneline if that can contain a next in the else? Or code that effectively does the same.

Reason being, I have a fair number of nested if statements that come after the above in the example.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
What does the rest of the code do?
 
Upvote 0
Your structure needs to be like this:
Code:
 Dim oFile As Object, oFolder As Object
 
Set oFolder = CreateObject("scripting.filesystemobject").GetFolder("C:\")
For Each oFile In oFolder.Files
    If InStr(1, oFile.Name, ".pdf", vbTextCompare) Then 
        MsgBox oFile.Path 
    ElseIf next test Then
        MsgBox something else
    ElseIf next next test Then
        MsgBox another something else
    End If
Next oFile
End Sub
 
Upvote 0
There might be a better way but I do it like this:
Code:
Option Explicit
 
Private Sub Example()
 
Dim r As Range, SomethingToTest As Boolean
 
For Each r In ActiveSheet
 
    If Not SomethingToTest Then GoTo skiPPy
    'do whatever you'd do
 
skiPPy:
Next r
 
End Sub

Regards
Adam
 
Upvote 0
Remove Else and put the EndIf above the Next oFile

you'll just have to nest them a bit more

or move your tests into their own function

there is no Loop function for "For Each"
 
Upvote 0
Are you going to do all the parsing in the loop?

Why not create a function/sub to do that?

You could pass the filename to that, have it do all the parsing and then return to the main sub to continue with the loop.

PS You really should avoid using Goto.
 
Upvote 0
Are you going to do all the parsing in the loop?

Why not create a function/sub to do that?

You could pass the filename to that, have it do all the parsing and then return to the main sub to continue with the loop.

PS You really should avoid using Goto.

You're almost certainly correct - but for my own interest why is GoTo a bad thing? Thanks.

Adam
 
Upvote 0
Adam

Try searching for 'spaghetti code'.:)
 
Upvote 0
Stuck to standard code structure and put a handful of my checks in some other subs/functions as suggested. The problem I was having was with the nesting...visually. Since I like it all idented, I was having to scroll across the page to read the next line.

Basically, it loops thru the files, checks the information's there, takes the information, looks through another folder for each of the details in the previous files and does further checks...before reconciling it. Rinse and repeat for further files before moving onto the next .pdf in this example.
 
Upvote 0

Forum statistics

Threads
1,223,956
Messages
6,175,611
Members
452,660
Latest member
Zatman

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