+ Reply to Thread
Results 1 to 7 of 7

Issue with For...next loop

Hybrid View

  1. #1
    Registered User
    Join Date
    11-22-2012
    Location
    San Luis Potosi
    MS-Off Ver
    Excel 2010
    Posts
    14

    Issue with For...next loop

    Hi,

    I am having a small issue in a code. I have the following loop:

    Dim I As Integer
    If ComboBox.Value = "Paid" Then
    For I = 1 To 3
    Label& I.Visible = False
    Next I
    End if
    I have three labels (Label1, Label2 and Label3).

    My issue is in joining Label with the I of the loop !

    Thank you for your help,

    Charles

  2. #2
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Issue with For...next loop

    Hi,

    I'm not sure if you can do that, but here's a workaround.
    Sub test()
    
    If ComboBox.Value = "Paid" Then
        Label1.Visible = False
        Label2.Visible = False
        Label3.Visible = False
    End If
    
    End Sub

  3. #3
    Registered User
    Join Date
    11-22-2012
    Location
    San Luis Potosi
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Issue with For...next loop

    Thank you for your fast answer. I wanted to make the code shorter using the "for... next" loop but it's fine like this.

    Bye,

    Charles

  4. #4
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Issue with For...next loop

    Anther way
    Private Sub ComboBox1_Change()
        With ComboBox1
            Label1.Visible = (.Value <> "Paid")
            Label2.Visible = (.Value <> "Paid")
            Label3.Visible = (.Value <> "Paid")
        End With
    End Sub
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Issue with For...next loop

    Charles

    You can do it without a loop but the syntax would be slightly different.

    Where are the labels located? Woksheet? Userform?
    If posting code please use code tags, see here.

  6. #6
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Issue with For...next loop

    Well, it has the same amount of rows now... :P
    (Of course that wouldn't be the case if you had four or more labels.)

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Issue with For...next loop

    Here's an example for labels on a userform.
    Dim I As Integer
    
    For I = 1 To 3
         Me.Controls("Label" &I).Visible = ComboBox.Value<>"Paid"
    Next I

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1