VBA - Can not close my For Each Loop with If/Else ?

NessPJ

Active Member
Joined
May 10, 2011
Messages
416
Office Version
  1. 365
Hello all,

I have a VBA routine where the debugger keeps telling me that i am using a Next statement without a For present. But it is actually there.
Did i create a structural error somewhere, so the code is no longer 'seeing' the For statement?

VBA Code:
Private Sub Process()

Dim CheckTEXT1 As Long
Dim TEXT1 As String, TEXT2 As String
Dim TEXT1LR As Long, TEXT2LR As Long       'LR = LastRow
Dim MsgNr As String, MsgType As String, WCSMsgNr As String, SenRec As String, DateTime As String, Giftnumber As String, Location As String

MsgType = Sheets("Parameters").Range("B76").Value
SenRec = Sheets("Parameters").Range("B77").Value

TEXT1LR = Sheets("i25").Range("A65534").End(xlUp).Row

'Set variables used for all interfaces to process
    Set Rng = Sheets("i25").Range("A1:A" & TEXT1LR)
 
'Loop each cell in range
    For Each Cell In Rng.Cells

    TEXT1 = Cell.text
   
    'HEADER1
    CheckTEXT1 = InStr(1, Cell.text, "HEADER1-TEXT1", vbTextCompare)       'Check if the Message is of the right type

    If CheckTEXT1 > 0 Then
   
    DateTime = Format(Now - TimeSerial(0, 0, 1), "YYYYMMDDHHMMSS")      'Now (huidige datum/tijd) minus 1 seconde
   
    MsgNr = Mid(Cell.text, 13, 8)       
   
   
    WCSMsgNr = Mid(Cell.text, 21, 8)
    Giftnumber = Mid(Cell.text, 49, 9)
    Location = WorksheetFunction.text(Mid(Cell.text, 58, 8), "00000000")
   
    'Create HBST-TEXT2 message
    TEXT2 = MsgType & MsgNr & WCSMsgNr & SenRec & DateTime & Giftnumber & Location & "000"
    'This message is padded with 3 zeroes at the end for some reason
   
    TEXT2LR = Sheets("i22").Range("A65534").End(xlUp).Row + 1
   
        If IsEmpty(Sheets("i22").Range("A1").Value) = True Then
   
        Sheets("i22").Range("A1").Value = TEXT2
   
        Else
       
        Sheets("i22").Range("A" & TEXT2LR).Value = TEXT2
       
        End If
       
        Sheets("i25").Range(Cell.Address).Font.Color = -11489280
   
   
    Next
   
    Else        'If/Else HEADER1/HEADER2
   
    'HEADER2
    CheckTEXT1 = InStr(1, Cell.text, "HEADER2-TEXT1", vbTextCompare)       'Check if the Message is of the right type
   
    If CheckTEXT1 > 0 Then
   
    DateTime = Format(Now - TimeSerial(0, 0, 1), "YYYYMMDDHHMMSS")      'Now (huidige datum/tijd) minus 1 seconde
   
    MsgNr = Mid(Cell.text, 13, 8)       'Takes original messagenumber from message
   
   
    WCSMsgNr = Mid(Cell.text, 21, 8)
    Giftnumber = Mid(Cell.text, 49, 9)
    Location = WorksheetFunction.text(Mid(Cell.text, 58, 6), "000000")
   
    'Create HBST-TEXT2 message
    TEXT2 = MsgType & MsgNr & WCSMsgNr & SenRec & DateTime & Giftnumber & Location & "000"
    'This message is padded with 3 zeroes at the end for some reason
   
    TEXT2LR = Sheets("i22").Range("A65534").End(xlUp).Row + 1
   
        If IsEmpty(Sheets("i22").Range("A1").Value) = True Then
   
        Sheets("i22").Range("A1").Value = TEXT2
   
        Else
       
        Sheets("i22").Range("A" & TEXT2LR).Value = TEXT2
       
        End If
       
        Sheets("i25").Range(Cell.Address).Font.Color = -11489280
   
    End If
   
    Next


End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi,
you do have two Next in your code for only one For.
 
Upvote 0
Hi,
you do have two Next in your code for only one For.

Hello,

If i change the code like this (i had it this way in the first place), i receive exactly the same error "For Without Next".

VBA Code:
Private Sub Process()

Dim CheckTEXT1 As Long
Dim TEXT1 As String, TEXT2 As String
Dim TEXT1LR As Long, TEXT2LR As Long       'LR = LastRow
Dim MsgNr As String, MsgType As String, WCSMsgNr As String, SenRec As String, DateTime As String, Giftnumber As String, Location As String

MsgType = Sheets("Parameters").Range("B76").Value
SenRec = Sheets("Parameters").Range("B77").Value

TEXT1LR = Sheets("i25").Range("A65534").End(xlUp).Row

'Set variables used for all interfaces to process
    Set Rng = Sheets("i25").Range("A1:A" & TEXT1LR)
 
'Loop each cell in range
    For Each Cell In Rng.Cells

    TEXT1 = Cell.text
   
    'HEADER1
    CheckTEXT1 = InStr(1, Cell.text, "HEADER1-TEXT1", vbTextCompare)       'Check if the Message is of the right type

    If CheckTEXT1 > 0 Then
   
    DateTime = Format(Now - TimeSerial(0, 0, 1), "YYYYMMDDHHMMSS")      'Now (huidige datum/tijd) minus 1 seconde
   
    MsgNr = Mid(Cell.text, 13, 8)       
   
   
    WCSMsgNr = Mid(Cell.text, 21, 8)
    Giftnumber = Mid(Cell.text, 49, 9)
    Location = WorksheetFunction.text(Mid(Cell.text, 58, 8), "00000000")
   
    'Create HBST-TEXT2 message
    TEXT2 = MsgType & MsgNr & WCSMsgNr & SenRec & DateTime & Giftnumber & Location & "000"
    'This message is padded with 3 zeroes at the end for some reason
   
    TEXT2LR = Sheets("i22").Range("A65534").End(xlUp).Row + 1
   
        If IsEmpty(Sheets("i22").Range("A1").Value) = True Then
   
        Sheets("i22").Range("A1").Value = TEXT2
   
        Else
       
        Sheets("i22").Range("A" & TEXT2LR).Value = TEXT2
       
        End If
       
        Sheets("i25").Range(Cell.Address).Font.Color = -11489280
   
   
    Else        'If/Else HEADER1/HEADER2
   
    'HEADER2
    CheckTEXT1 = InStr(1, Cell.text, "HEADER2-TEXT1", vbTextCompare)       'Check if the Message is of the right type
   
    If CheckTEXT1 > 0 Then
   
    DateTime = Format(Now - TimeSerial(0, 0, 1), "YYYYMMDDHHMMSS")      'Now (huidige datum/tijd) minus 1 seconde
   
    MsgNr = Mid(Cell.text, 13, 8)       'Takes original messagenumber from message
   
   
    WCSMsgNr = Mid(Cell.text, 21, 8)
    Giftnumber = Mid(Cell.text, 49, 9)
    Location = WorksheetFunction.text(Mid(Cell.text, 58, 6), "000000")
   
    'Create HBST-TEXT2 message
    TEXT2 = MsgType & MsgNr & WCSMsgNr & SenRec & DateTime & Giftnumber & Location & "000"
    'This message is padded with 3 zeroes at the end for some reason
   
    TEXT2LR = Sheets("i22").Range("A65534").End(xlUp).Row + 1
   
        If IsEmpty(Sheets("i22").Range("A1").Value) = True Then
   
        Sheets("i22").Range("A1").Value = TEXT2
   
        Else
       
        Sheets("i22").Range("A" & TEXT2LR).Value = TEXT2
       
        End If
       
        Sheets("i25").Range(Cell.Address).Font.Color = -11489280
   
    End If
   
    Next


End Sub
 
Upvote 0
The number of "Next" statements must match the number of "For" statements.
Unless you have single line "If ... Then" statements, the number of "End If" statements must match the number of "If ... Then" statements.

If you have any sort of imbalance, you will get the kind of error you are seeing.
So go through your code, and make sure all those counts match up.
If you properly indent your code, this will be easier to see also.
 
Upvote 0
Can you please clarify? For Each statement, does not need an End statement? :)
I added an End If at the end of your code (before the End Sub) and it worked, the message about the For ... Next loop did not apper anymore.
When you count the Ifs and the End Ifs, you see there is one If more than End If.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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