"Indirect" 在循环中引用组合框

"Indirect" reference a combobox in a loop

我遇到这个问题,每当我尝试 运行 我的代码时,我的 excel 就会崩溃。

我相信我有一个解决方案,但我不知道如何执行它。

我有这个代码:

If (AnswerGame1A <> "") And (AnswerGame1B <> "") Then
    Score1A.Visible = False
    Score1B.Visible = False
    Resultlist1.Visible = False
    SubmitGame1.Visible = False
    Dash1.Visible = False
    GameLabel1.Visible = True
    GameLabel1.Left = 36
End If

If (AnswerGame2A <> "") And (AnswerGame2B <> "") Then
    Score2A.Visible = False
    Score2B.Visible = False
    Resultlist2.Visible = False
    SubmitGame2.Visible = False
    Dash2.Visible = False
    GameLabel2.Visible = True
    GameLabel2.Left = 36
End If

这样又持续了 51 次。

如果我删除这段代码,文件不会崩溃,我的想法是改为编写一个循环。

类似这样的东西,但这不起作用。

信息:所有这些名称都是多页面中的控件,即用户窗体中的控件。它是组合框、标签、命令按钮和文本框。用户窗体初始化时的代码运行。

For i = 1 to 51
If (Indirect("AnswerGame" & i & "A") <> "") And (Indirect("AnswerGame" & i & "B") <> "") Then
    Indirect("Score" & i & "A").Visible = False
    Indirect("Score" & i & "B").Visible = False
    Indirect("Resultlist" & i).Visible = False
    Indirect("SubmitGame" & i).Visible = False
    Indirect("Dash" & i).Visible = False
    Indirect("GameLabel" & i).Visible = True
    Indirect("GameLabel" & i).Left = 36
End If
Next i

您认为这可以帮助 excel 避免崩溃吗?以及如何修复代码才能正常工作?

假设您的组合框是sheetActiveX类型,请尝试下一段代码:

Sub testAvoitManyIterationsCombo()
   Dim sh As Worksheet, i As Long
   
   Set sh = ActiveSheet ' use here your necessary sheet

   For i = 1 To 51
        If sh.OLEObjects("AnswerGame" & i & "A").Object.Value <> "" And sh.OLEObjects("AnswerGame" & i & "B").Object.Value <> "" Then
            sh.Shapes("Score" & i & "A").Visible = False
            sh.Shapes("Score" & i & "B").Visible = False
            sh.Shapes("Resultlist" & i).Visible = False
            sh.Shapes("SubmitGame" & i).Visible = False
            sh.Shapes("Dash" & i).Visible = False
            sh.Shapes("GameLabel" & i).Visible = True
            sh.Shapes("GameLabel" & i).left = 36
        End If
    Next i
End Sub

如果它们不是 activeX,这应该会让您走上正轨:

    Option Explicit
    Private Sub UserForm_Click()
        Dim i As Long, str As String
        
        For i = 1 To 10
            str = "AnswerGame" & i & "A"
            If Me.Controls(str).Value = "" Then
                Score1A.Visible = False
            End If
        Next i
    End Sub

我的解决方案符合我的目的。该文件似乎不再崩溃。 感谢@ceci 展示了如何操作。

抱歉使用“x”而不是“i”,“i”已在别处使用。

Dim x As Long, str1 As String, str2 As String, SCO1 As String, SCO2 As String, Res As String
Dim SubmitG As String, Da As String, GameL As String

For x = 1 To 51
    str1 = "AnswerGame" & x & "A"
    str2 = "AnswerGame" & x & "B"
    If Me.Controls(str1) <> "" Then
        If Me.Controls(str2) <> "" Then
    
            SCO1 = "Score" & x & "A"
            SCO2 = "Score" & x & "B"
            Me.Controls(SCO1).Visible = False
            Me.Controls(SCO2).Visible = False
            
            Res = "Resultlist" & x
            Me.Controls(Res).Visible = False
            
            SubmitG = "SubmitGame" & x
            Me.Controls(SubmitG).Visible = False
            
            Da = "Dash" & x
            Me.Controls(Da).Visible = False
            
            GameL = "GameLabel" & x
            Me.Controls(GameL).Visible = True
            Me.Controls(GameL).Left = 36
            
            
        End If
    End If
Next x