繁体   English   中英

我的循环(for next)有问题,我想不通?

[英]There's a problem with my loop (for next) and I can't figure it out?

所以我有一个带有 54 个文本框的用户窗体,我都需要检查它们的值,这些值需要为空或“1”、“2”、“3”或“4”。 这就是我想出的:

dim CG(1 to 54) as string

CG(1) = NewStudent.TB1_1_1.Text
CG(2) = NewStudent.TB1_1_2.Text
CG(3) = NewStudent.TB1_1_3.Text
CG(4) = NewStudent.TB1_1_4.Text
CG(5) = NewStudent.TB1_1_5.Text
CG(6) = NewStudent.TB1_1_6.Text
CG(7) = NewStudent.TB1_2_1.Text
CG(8) = NewStudent.TB1_2_2.Text
CG(9) = NewStudent.TB1_2_3.Text

CG(10) = NewStudent.TB2_1_1.Text
CG(11) = NewStudent.TB2_1_2.Text
CG(12) = NewStudent.TB2_1_3.Text
CG(13) = NewStudent.TB2_1_4.Text
CG(14) = NewStudent.TB2_1_5.Text
CG(15) = NewStudent.TB2_1_6.Text
CG(16) = NewStudent.TB2_2_1.Text
CG(17) = NewStudent.TB2_2_2.Text
CG(18) = NewStudent.TB2_2_3.Text

CG(19) = NewStudent.TB3_1_1.Text
CG(20) = NewStudent.TB3_1_2.Text
CG(21) = NewStudent.TB3_1_3.Text
CG(22) = NewStudent.TB3_1_4.Text
CG(23) = NewStudent.TB3_1_5.Text
CG(24) = NewStudent.TB3_1_6.Text
CG(25) = NewStudent.TB3_2_1.Text
CG(26) = NewStudent.TB3_2_2.Text
CG(27) = NewStudent.TB3_2_3.Text

CG(28) = NewStudent.TB4_1_1.Text
CG(29) = NewStudent.TB4_1_2.Text
CG(30) = NewStudent.TB4_1_3.Text
CG(31) = NewStudent.TB4_1_4.Text
CG(32) = NewStudent.TB4_1_5.Text
CG(33) = NewStudent.TB4_1_6.Text
CG(34) = NewStudent.TB4_2_1.Text
CG(35) = NewStudent.TB4_2_2.Text
CG(36) = NewStudent.TB4_2_3.Text

CG(37) = NewStudent.TB5_1_1.Text
CG(38) = NewStudent.TB5_1_2.Text
CG(39) = NewStudent.TB5_1_3.Text
CG(40) = NewStudent.TB5_1_4.Text
CG(41) = NewStudent.TB5_1_5.Text
CG(42) = NewStudent.TB5_1_6.Text
CG(43) = NewStudent.TB5_2_1.Text
CG(44) = NewStudent.TB5_2_2.Text
CG(45) = NewStudent.TB5_2_3.Text

CG(46) = NewStudent.TB6_1_1.Text
CG(47) = NewStudent.TB6_1_2.Text
CG(48) = NewStudent.TB6_1_3.Text
CG(49) = NewStudent.TB6_1_4.Text
CG(50) = NewStudent.TB6_1_5.Text
CG(51) = NewStudent.TB6_1_6.Text
CG(52) = NewStudent.TB6_2_1.Text
CG(53) = NewStudent.TB6_2_2.Text
CG(54) = NewStudent.TB6_2_3.Text

x = True
For i = LBound(CG) To UBound(CG)

    If CG(i) = vbNullString Then
        Else
        If CG(i) = "1" Then
            Else
            If CG(i) = "2" Then
                Else
                If CG(i) = "3" Then
                    Else
                    If CG(i) = "4" Then
                        Else
                        x = False
                    End If
                End If
            End If
        End If
    End If
Exit For
Next i
    
'If i = UBound(CG) Then
If x = True Then

我知道可能有一种更简单的方法,但我对编码一无所知,我就是想不通。 现在这只是我代码的一部分,最后如果 x 保持为真,它应该将数据复制到工作表中。

这很好,但仅适用于第一个文本框。 之后它只是继续复制数据,忽略所有其他文本框。 因此我添加了“If i = UBound(CG) Then”,认为它会解决它。

然而,从那时起,它就不再做任何事情(没有反应,没有错误消息),就好像新添加的条件永远不会得到满足。

任何人请帮助我。

您的代码不起作用,因为您有Exit For所以在第一个循环它退出循环。

此外,您可能会受益于Select 案例

尝试更换:

x = True
For i = LBound(CG) To UBound(CG)

    If CG(i) = vbNullString Then
        Else
        If CG(i) = "1" Then
            Else
            If CG(i) = "2" Then
                Else
                If CG(i) = "3" Then
                    Else
                    If CG(i) = "4" Then
                        Else
                        x = False
                    End If
                End If
            End If
        End If
    End If
Exit For
Next i

For i = LBound(CG) To UBound(CG)
    x = True
    Select Case CG(i)
        Case 1 To 4, ""
            'WE DO NOTHING
        Case Else
            x = False
    End Select
    
    If x = True Then
        'code to copy if x=True
    End If
    
Next i

看看情况如何。 此外,复制部分肯定应该在循环内,因此您检查x并复制(或不复制)数组CG中的每个值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM