简体   繁体   English

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

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

So I've got a UserForm with 54 Textboxes which I all need to be checked for their values which need to be either empty or "1", "2", "3" or "4".所以我有一个带有 54 个文本框的用户窗体,我都需要检查它们的值,这些值需要为空或“1”、“2”、“3”或“4”。 This is what I came up with:这就是我想出的:

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

I know there's probably an easier way but I don't know anything about coding and I just couldn't figure it out.我知道可能有一种更简单的方法,但我对编码一无所知,我就是想不通。 Now this is only a part of my code, in the end if x stays true it should copy the data into the worksheet.现在这只是我代码的一部分,最后如果 x 保持为真,它应该将数据复制到工作表中。

This worked out fine but only for the first textbox.这很好,但仅适用于第一个文本框。 After that it just moved on to copy the data, ignoring all the other textboxes.之后它只是继续复制数据,忽略所有其他文本框。 Therefore I added "If i = UBound(CG) Then" thinking it would fix it.因此我添加了“If i = UBound(CG) Then”,认为它会解决它。

However since then it just doesn't do anything anymore (no reaction, no error messages) as if that newly added condition never gets fullfilled.然而,从那时起,它就不再做任何事情(没有反应,没有错误消息),就好像新添加的条件永远不会得到满足。

Anyone pls help me out.任何人请帮助我。

Your code does not work because you got Exit For so at first loop it quits the loop.您的代码不起作用,因为您有Exit For所以在第一个循环它退出循环。

Also, you may benefit from Select Case此外,您可能会受益于Select 案例

Try replacing:尝试更换:

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

with

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

and see how it goes.看看情况如何。 Also, the copy part should be inside the loop for sure, so you check x and copy (or not) for each value in your array CG此外,复制部分肯定应该在循环内,因此您检查x并复制(或不复制)数组CG中的每个值

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

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