for next loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Neruda
    New Member
    • Mar 2017
    • 72

    #1

    for next loop

    Hi, just a quick question, I wonder if there a better way to write the lines below, just trying to simplify and learn more elegant ways of writing codes,
    Thanks all, hope u all well


    [CODE]
    Dim i As Integer
    Dim textTab As Variant
    Dim ctl As Control

    Code:
    For i = 0 To 9
            Set ctl = Me.Controls("cmd" & i + 1)
            If i <= UBound(textTab) Then ctl.Caption = textTab(i)
            If i > UBound(textTab) Then ctl.Visible = False
      Next i
  • cactusdata
    Recognized Expert New Member
    • Aug 2007
    • 223

    #2
    I would do like this:

    Code:
    For i = 0 To 9
        Set ctl = Me.Controls("cmd" & i + 1)
        If i <= UBound(textTab) Then 
            ctl.Caption = textTab(i)
            ' ???
            ' ctl.Visible = True
        else
            ctl.Visible = False
        End If
    Next

    Comment

    • twinnyfo
      Recognized Expert Moderator Specialist
      • Nov 2011
      • 3665

      #3
      And even perhaps:

      Code:
      For i = 0 To 9
          ctl.Visible = (i <= UBound(textTab))
          ctl.Caption = IIf(i <= UBound(textTab), _
                            textTab(i), _
                            "")
      Next

      Comment

      • Neruda
        New Member
        • Mar 2017
        • 72

        #4
        Uhh interesting, Thanks!

        Comment

        Working...