Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

For and Next statements must be in same block

For and Next statements must be in same block

For and Next statements must be in same block

I know this probably absurd, but I've been playing with this for two days now and I think I've just burned out on it and I'm not seeing the obvious somewhere. I'm getting the default Attachmate Editor error message on compile "For and Next statements must be in same block" and I have access to no other VB debugger tools and the help is disabled on the company Macro Editor. :( I have Ultra Edit and Notepad2 to use so I'm hoping someone can put another set of eyes on this code and tell me where I've botched the For statements.
Again, I'm prepared for my beating as I know this is something obvious I've missed, but help an old guy out? The only thing I edited out was the actual values in the 'y' For statement that I'm writing to the spreadsheet as there is several hundred lines of just assigning values and writing them to the row(s) in the spreadsheet. I called out this section with the "****" line... btw, "x" "y" and "r" are all dimmed Global as Long (reused throughout the code in different Subs.

CODE -->

Sub Incoming
	rowcount = 2 'Start at second line on Incoming spreadsheet
	For r = 2 To MyRange.Rows.Count + 1 'NOTE: we are using Sheet 1 column A to do the loops for the dates!
		'Move to DATE field
		Call WaitScreen 
		Do Until (SCR.WaitForCursor(22, 33))            
		    SCR.MoveTo 22, 33 'Move the cursor       
		Sess.Screen.PutString DT.Rows(r).Value, 22, 33 'Write the first date field desired
		Call PressEnter
		Call WaitScreen 
		If SCR.Search("<< DATE NOT FOUND >>") = "<< DATE NOT FOUND >>" Then
			'We know this date does not exist so skip the row
			Goto Error4	'Go to next r
		End If
		IsCheck = Trim(Sess.Screen.GetString(17, 47, 2))'Incoming Returns Count
		If IsCheck = "0" Then
			Goto Error4	'Go to next r there are no transmittal errors for this date
			'There exists Incoming Returns: continue running
		End If
		Do Until (SCR.WaitForCursor(17, 35))            
		    SCR.MoveTo 17, 35 'Move the cursor to the question mark       
		Call PressEnter
		'collect child screens
		For x = 12 to 16 'numbers of the lines to check
			'Reject Selection Screen 
			Dim IncomingRejCnt As String
			IncomingRejCnt = Trim(Sess.Screen.GetString(x, 18, 02))
			If IncomingRejCnt = "0" Or IncomingRejCnt = "" Then
				'Go to next x as there are no "currently on file" records to capture
				Goto NextX
				'Gather child pages
				Do Until (SCR.WaitForCursor(x, 02))            
				    SCR.MoveTo x, 02 'Move the cursor to the line      
				Call PressEnter
				IncomingRtns.Select 'select the spreadsheet to write to
			'We now have to parse through the sub categories
				For y = 12 to 17
				Dim cntme As Long
				cntme = x 'using this as the variable within y to get back to the right x row when cycling through the loops
					Dim CurntOnFile As String
					CurntOnFile = Trim(Sess.Screen.GetString(y, 19, 02))
					If CurntOnFile = "0" Then
						'do nothing we don't collect that line
						Goto NextY
						'Collect that line
						Do Until (SCR.WaitForCursor(y, 02))            
						    SCR.MoveTo y, 02 'Move the cursor to the line      
						Call PressEnter								
					'Code to collect child pages here
							'Variable allocation Note: reusing variables whenever possible!!!
							StartPage = Trim(Sess.Screen.GetString(23, 63, 04))
							LastPage = Trim(Sess.Screen.GetString(23, 75, 04))

							rowcount = rowcount + 1
							'Determine if we are at the last page
							If StartPage = LastPage Then Exit Do
							Call PressEnter 'Otherwise advance the page
					End If
					'There is no shortcut key back to the "y" category selection screen.
					'We must therefore go all the way back to the "x" screen and work our way back down
					Call PressF5
					Do Until (SCR.WaitForCursor(cntme, 02))            
					    SCR.MoveTo cntme, 02 'Move the cursor to the x line      
					Call PressEnter
					'We are now back at the y screen
					'next y line check					
				Next y
		Call PressF5 'Go back to the x screen
		Next x
	'Done collecting all child pages.
	Call PressF3
	Next r 'Loop, Change Date and do it again 	 		
End Sub 

If you bring that sentence in for a fitting, I can have it shortened by Wednesday!

RE: For and Next statements must be in same block


You're missing an End If between the Next y and Next x. The error message is not necessarily correct!

Since you're doing stuff in Excel, I'd code EVERYTHING in Excel VBA. It's the difference between a Yugo and a Cadillac!

Also I code all my block structures first completely (For...Next, If...Then...Else...End If, With...End With, Do...Loop, etc), like


If x Then

   End If 
then fill in the code. That would have solved your issue.

You have the right idea when you issue a SendKeys, you need to wait for the cursor, but you need a DoEvents


SCR.MoveTo 1, 1 'Move the cursor AWAY from the screen rest position      
    Do Until (SCR.WaitForCursor(22, 33))   'wait for cursor at screen rest position         


glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: For and Next statements must be in same block

Thanks! I could have had a V8! I didin't think about the Excel VBA tool! You're right on the structure building too. I will start doing that.

If you bring that sentence in for a fitting, I can have it shortened by Wednesday!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close