Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compile Error: Next without For

Status
Not open for further replies.

TBoz776

Technical User
Mar 30, 2017
2
0
0
US
Code:
Sub MUFS_Scrubbing()
      
      
Dim Sessions As Object
Dim System As Object



Set System = GetObject("", "Extra.system") ' Gets the system object
If (System Is Nothing) Then
    MsgBox "Could not create the EXTRA System object.  Stopping macro playback."
Stop
End If

Set Sessions = System.Sessions

If (Sessions Is Nothing) Then
    MsgBox "Could not create the EXTRA System object.  Stopping macro playback."
Stop
End If

' Set the default wait timeout value
    g_HostSettleTime = 1      ' milliseconds
    OldSystemTimeout& = System.Timeoutvalue
    If (g_HostSettleTime > OldSystemTimeout) Then
        System.Timeoutvalue = g_HostSettleTime
    End If
    
    
'--------------------------------------------------------------------
'Get the necessary Session Object
Dim Sess0 As Object
Set Sess0 = System.ActiveSession
If (Sess0 Is Nothing) Then
MsgBox "Could not create the Session object. Stopping macro playback."
Stop
End If
If Not Sess0.Visible Then Sess0.Visible = True
Sess0.screen.WaitHostQuiet (g_HostSettleTime)

 
'Start the Loop

 Dim Row As Long
 
    
    With Worksheets("MUFS Scrub")
        
    
    End With
    
    LastRow = ActiveSheet.UsedRange.Rows.Count
    
    For Row = 2 To LastRow
    
        
              
            Do
                Sess0.screen.SendKeys ("<PF11>")
                Sess0.screen.WaitHostQuiet (g_HostSettleTime)
                Sess0.screen.SendKeys ("21")
                Sess0.screen.WaitHostQuiet (g_HostSettleTime)
                Sess0.screen.MoveTo 6, 19
                Sess0.screen.PutString Trim(Cells(Row, 1)), 6, 19
                Sess0.screen.SendKeys ("<tab>")
                Sess0.screen.MoveTo 6, 25
                Sess0.screen.PutString Trim(Cells(Row, 2)), , 6, 25
                Sess0.screen.SendKeys ("<tab>")
                Sess0.screen.MoveTo 6, 36
                Sess0.screen.PutString Trim(Cells(Row, 3)), , 6, 36
                Sess0.screen.SendKeys ("<Enter>")
                Sess0.screen.WaitHostQuiet (g_HostSettleTime)
                Primary_Zip = Sess0.screen.getstring(14, 66, 5)
                Cells(Row, 11).Value = Primary_Zip
           
        
    Row = Row + 1
    
    Next Row
        
    Loop
                    
    
          
End Sub

I am relatively new to writing macros in Excel. I am not sure why I am receiving the compile error "Next without For". Eventually, there will be more steps in the looped process, not just pulling a zip code. However, I wanted to get the look-up code all set first. The code works except for the loop. Without the "Next Row", it looks up the first record, pulls back the zip code and continuously loops through the first record (obviously because it's not going to the next row on the sheet).

Any assistance will be appreciated.
 
It pays to indent your code so you can see what's going on.

Code:
For Row = 2 To LastRow
    Do
        ....           
        Row = Row + 1[red]
    Next Row
Loop[/red]

for every [tt]For[/tt] you need corresponding [tt]Next[/tt]
for every [tt]Do[/tt] you need corresponding [tt]Loop[/tt]
but you cannot do what you did (in [red]RED[/red])

Code:
For Row = 2 To LastRow
    Do
        ....           
        Row = Row + 1[blue]
    Loop
Next Row [/blue]

But your [tt]Do - Loop[/tt] does not have a "way out"

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Hi,

Your loops cannot overlap. Therefore, your Do...Loop MUST br within the For...Next loop or else TILT!

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top