|
-
Apr 14th, 2009, 04:48 PM
#1
Thread Starter
New Member
[RESOLVED] How to force Next before end?
In BASIC I could write something like
For x = 1 to 3
If x=2 then next x (or goto line number of above code)
print x+" is an odd number."
next x
vba does not seem to recognise the "next x" when in the if nest though.
How do I get around this? I can do x=x+1 because some of the exceptions are consecutive and will not be caught when together (unlike my even number example).
Thanks.
-
Apr 14th, 2009, 08:16 PM
#2
Addicted Member
Re: How to force Next before end?
hi palantir, don't know exactly what you want to do but hope this is what you mean:
one more thing don't put goto line number which is above your "for loop" because it will be an eternal loop ...
Code:
For x = 1 to 3
If x = 2 then
Goto line_u_want '(goto line_u_want should be below for loop)
Exit For
End if
Next x
The taller the bamboo grows the lower it bends... 
-
Apr 14th, 2009, 10:13 PM
#3
Re: How to force Next before end?
vb Code:
For x = 1 to 3 If x=2 then ' do nothing next x (or goto line number of above code) else print x & " is an odd number." end if next x
or
vb Code:
For x = 1 to 3 If Not x=2 then print x &" is an odd number." next x
note indenting to make code readable
use & rather than + to concantenate strings, + may work, but may give incorrect results sometimes
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Apr 15th, 2009, 02:09 AM
#4
Re: How to force Next before end?
These simulate what you want. However, for better practice, if possible you should try to avoid using GoTo. Use the way westconn1 mentioned above.
Code:
Sub XXX()
Dim x As Long
For x = 1 To 10
If x Mod 2 = 0 Then GoTo Next_x '-- use Goto Label
'... ...
Debug.Print x & " is an odd number."
Next_x:
Next
End Sub
Sub XYZ()
Dim x As Long
For x = 1 To 10
If x Mod 2 = 0 Then GoTo 99 '-- use Goto Line number
'... ...
Debug.Print x & " is an odd number."
99 Next
End Sub
-
Apr 15th, 2009, 05:16 AM
#5
Thread Starter
New Member
Re: How to force Next before end?
Thank you anhn and all. Resolved. It was the GoTo Label I needed.
-
Apr 15th, 2009, 06:15 AM
#6
Re: How to force Next before end?
it is not considered to be good to use goto or gosub in VB, except for error handling
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Apr 15th, 2009, 11:30 AM
#7
Thread Starter
New Member
Re: How to force Next before end?
So how else can it be done?
Here's my final working and tested code (excerpt:
Code:
Sub EDUmation()
...
For NumEntry = 1 To Range("B4").Value
FirstRow = ((NumEntry * 33) - 33) + 10 ' gets first row of each edu entry per loop
LastRow = FirstRow + 31 ' adds 31 rows to include all of entry
For MyRow = FirstRow To LastRow ' loop through each row of the table
If Cells(MyRow, 2).Value = "" Then GoTo Next_MyRow ' Gets next row if current row has no items
mystr = ""
mystr = Cells(MyRow, 1).Value & String(17 - Len(Cells(MyRow, 1).Value), " ") ' element name + horizontal spacer
mystr = mystr & Cells(MyRow, 2).Value ' adds item in row 1
For CheckRow = 3 To 31 ' this loop adds any other items except where empty
If Not Cells(MyRow, CheckRow).Value = "" Then mystr = mystr & ", " & Cells(MyRow, CheckRow).Value
Next CheckRow
Print #1, mystr
Next_MyRow:
Next MyRow
Print #1, vbCrLf
Next NumEntry
Close #1
Sheets("DATA").Range("G2").ClearContents ' note that this row expects the worksheet tab to be named DATA
Sheets("DATA").Hyperlinks.Add Range("G2"), PageName
End Sub
-
Apr 15th, 2009, 11:51 AM
#8
Re: How to force Next before end?
 Originally Posted by palantir
So how else can it be done?
How about
Code:
For NumEntry = 1 To Range("B4").Value
FirstRow = ((NumEntry * 33) - 33) + 10 ' gets first row of each edu entry per loop
LastRow = FirstRow + 31 ' adds 31 rows to include all of entry
For MyRow = FirstRow To LastRow ' loop through each row of the table
If Cells(MyRow, 2).Value <> "" Then
mystr = ""
mystr = Cells(MyRow, 1).Value & String(17 - Len(Cells(MyRow, 1).Value), " ") ' element name + horizontal spacer
mystr = mystr & Cells(MyRow, 2).Value ' adds item in row 1
For CheckRow = 3 To 31 ' this loop adds any other items except where empty
If Not Cells(MyRow, CheckRow).Value = "" Then mystr = mystr & ", " & Cells(MyRow, CheckRow).Value
Next 'CheckRow
Print #1, mystr
End If
Next 'MyRow
Print #1, vbCrLf
Next 'NumEntry
Close #1
-
Apr 15th, 2009, 12:15 PM
#9
Thread Starter
New Member
Re: How to force Next before end?
You're a genius. Thank you so much! Resolved.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|