簡體   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