Results 1 to 2 of 2

Thread: [RESOLVED] Help with a For Next statement

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    167

    Resolved [RESOLVED] Help with a For Next statement

    Guys,

    Wonder if you can help me, I have written some code that when tested gives me a message saying that I have a Next statement without a For statement.

    Code:
    Dim strFilter As String
      Dim strg As String, count As Integer
     ' Start of the For Next statement to loop through the Seasonal Tabs
     
     
     For l = 1 To 10
        
        ' Based on count of l a different Filter assignment is declared to the variable
       
        If l = 2 Then strFilter = "x" Else
        If l = 3 Then strFilter = "b" Else
        If l = 4 Then strFilter = "m" Else
        If l = 5 Then strFilter = "f" Else
        If l = 6 Then strFilter = "s" Else
        If l = 7 Then strFilter = "w" Else
        If l = 8 Then strFilter = "v" Else
        If l = 9 Then strFilter = "e" Else
        If l = 10 Then strFilter = "h" Else
        If l = 1 Then strFilter = "g"
        
        Selection.AutoFilter Field:=4, Criteria1:="1"
        Selection.AutoFilter Field:=5, Criteria1:=strFilter
        
        ' Selects the range to count
        Range("a1").Select
        Range(Selection, Selection.End(xlDown)).Select
        ' Checks the selection for values below the header row
        count = 0
        strg = Selection.SpecialCells(xlCellTypeVisible).Address
    
        For i = 1 To Len(Trim(strg))
    
        If Mid(Trim(strg), i, 1) = "," Then count = count + 1
        
        Next i
        
        ' selects only the visible cells
        If count > 0 Then
        Selection.SpecialCells(xlCellTypeVisible).Select
        ' declares the variable as the count of the selection
        strCount = Selection.count
        ' deducts from the count the header fields
        strCount = (strCount - 1)
        
        Else
        strCount = 0
        ' Reverts to Dashboard sheet and pastes count value
        Windows("Events Dashboard Template.xls").Activate
        ActiveCell.Value = strCount
        Selection.Offset(1, 0).Select
        strDate = "1504"
        Windows("Import & Home Tracker (interim) " & strDate & ".xls").Activate
        
    Next l
    My code originally omitted this part and it worked fine.

    Code:
    ' Checks the selection for values below the header row
        count = 0
        strg = Selection.SpecialCells(xlCellTypeVisible).Address
    
        For i = 1 To Len(Trim(strg))
    
        If Mid(Trim(strg), i, 1) = "," Then count = count + 1
        
        Next i
    Do you know now why I am receiving the compile error message?

    SJ

  2. #2
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: [RESOLVED] Help with a For Next statement

    All your If...Then...Else statements have no End If.
    That makes the Next statement belong to an [If...End If] block even End If not reached yet.
    And within that [If...End If] block, the Next has no For --> compiler complains.
    The top For is outside the [If...End If] block.

    The syntax of [If...End If] statement is:
    Code:
    If condition1 Then
       ... ...
    End If
    or
    Code:
    If condition1 Then
       ... ...
    ElseIf condition2 Then
       ... ...
    Else
       ... ...
    End If
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width