Consulting

Results 1 to 4 of 4

Thread: Solved: Help using For loop in a IF then statement

  1. #1
    VBAX Mentor
    Joined
    Aug 2008
    Posts
    323
    Location

    Solved: Help using For loop in a IF then statement

    hello guys ,

    I am designing a custom ribbon for my application and here is my code [vba]Sub FacilityType_Change(control As IRibbonControl, text As String)

    Dim Rw As Integer
    Dim F As Integer

    Worksheets("Report").Activate

    Rw = ActiveSheet.Cells(Rows.Count, "d").end(xlUp).Rows

    If text = "All Facilities" Then
    Cells.Select
    Range("E1").Activate
    Selection.EntireRow.Hidden = False
    If text = "SIS Facilities" Then
    Cells.Select
    Range("E1").Activate
    Selection.EntireRow.Hidden = False
    F = 1
    For Rw = Rw To 3 Step -1
    If Cells(Rw, 3).Value <> F Then
    Cells(Rw, 3).EntireRow.Hidden = True
    Next Rw
    ElseIf text = "Arterial Facilities" Then
    Cells.Select
    Range("E1").Activate
    Selection.EntireRow.Hidden = False
    F = 3
    For Rw = Rw To 3 Step -1
    If Cells(Rw, 3).Value <> F Then
    Cells(Rw, 3).EntireRow.Hidden = True
    Next Rw
    End If


    End Sub
    [/vba]
    when I run it , I get an error Next with out For ,,
    Guess I cannot use a for loop statement in a if then statement
    any help or clue or any other way for me to to do it ??

    Thank you
    I am a Newbie, soon to be a Guru

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub FacilityType_Change(control As IRibbonControl, text As String)

    Dim Rw As Integer
    Dim F As Integer

    Worksheets("Report").Activate

    Rw = ActiveSheet.Cells(Rows.Count, "d").End(xlUp).Rows

    If text = "All Facilities" Then
    Cells.Select
    Range("E1").Activate
    Selection.EntireRow.Hidden = False
    If text = "SIS Facilities" Then
    Cells.Select
    Range("E1").Activate
    Selection.EntireRow.Hidden = False
    F = 1
    For Rw = Rw To 3 Step -1
    If Cells(Rw, 3).Value <> F Then
    Cells(Rw, 3).EntireRow.Hidden = True
    Next Rw
    ElseIf text = "Arterial Facilities" Then
    Cells.Select
    Range("E1").Activate
    Selection.EntireRow.Hidden = False
    F = 3
    For Rw = Rw To 3 Step -1
    If Cells(Rw, 3).Value <> F Then
    Cells(Rw, 3).EntireRow.Hidden = True
    '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    ' Missing an End If here
    '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    Next Rw
    End If


    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Mentor
    Joined
    Aug 2008
    Posts
    323
    Location
    hey xld .. thanks for pointing it out but it still gives me the same error , the error is popping up at the first next rw statement
    I am a Newbie, soon to be a Guru

  4. #4
    VBAX Mentor
    Joined
    Aug 2008
    Posts
    323
    Location
    My bad I was missing one more end if thankx XLD
    I am a Newbie, soon to be a Guru

Posting Permissions

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