简化我的代码

时间:2015-05-23 20:50:01

标签: arrays vba loops

    If bar1.Visible = False Or bar1.Value > 0 Then
        npc1.Visible = False
    End If
    If bar2.Visible = False Or bar2.Value >= 0 Then
        npc2.Visible = False
    End If
    If bar3.Visible = False Or bar3.Value >= 0 Then
        npc3.Visible = False
    End If
    If bar4.Visible = False Or bar4.Value >= 0 Then
        npc4.Visible = False
    End If
    If bar5.Visible = False Or bar5.Value >= 0 Then
        npc5.Visible = False
    End If

这可以达到bar10和npc10 我不知道如何应用循环或数组。我不是最好的编程,但我的项目很快就会到期而且我很害怕,因为它看起来不错。如果可以,请帮助并分解!

2 个答案:

答案 0 :(得分:1)

For n = 1 to 10
    Set c = Me.Controls("bar" & n)
    If c.Visible = False Or c.Value > 0 Then
        Me.Controls("npc" & n).Visible = False
    End If
Next n

答案 1 :(得分:1)

试试这个

Dim i As Long

For i = 1 To 10 Step 1
    If bar(i).Visible = False Or bar(i).Value >= 0 Then
        npc(i).Visible = False
    End If
Next i