VBA for 循环不工作

VBA for loop not working

我的循环有问题。我想做一个作品sheet,打印它(还没有内置,我知道它是如何工作的),然后删除它。之后继续下一个 j 做同样的事情。但它正在将 j = 1 重新循环到 1,因此它试图创建第二个名为 "print" 的作品sheet,但这是不可能的。

我有名称为 CheckBox1、CheckBox2、CheckBox'j' 的复选框。我想从 CheckBox1 开始,到 CheckBox25 结束。如果是,则打印 sheet.

我想我需要摆脱第一个原因:
对于 Me.Controls 中的每个 ctrl
但我不知道怎么做。因为我需要它来指定变量 'j'.

Private Sub PrintKnop_Click()

    Dim ctrl As MSForms.Control
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "CheckBox" And Left(ctrl.Name, 8) = "CheckBox" Then
            Dim j As Integer
            j = Mid(ctrl.Name, 9, 2)

            For j = 1 To 1
            'it should be possible to adjust the range.
                If ctrl.Value = True Then
                    Dim ws As Worksheet
                    With ThisWorkbook
                    Worksheets("Veiligheid").Copy _
                    before:=ActiveWorkbook.Sheets("Data")
                    Set ws = ActiveSheet
                    ws.Name = "print"
                    End With

                    'Application.DisplayAlerts = False
                    'Sheets("print").Delete
                    'Application.DisplayAlerts = True
                    'These shouldn't be comments, but if I uncomment it, it won't show the failures.

                End If
            Next

            For j = 2 To 4
                If ctrl.Value = True Then
                    With ThisWorkbook
                    Worksheets("Veiligheid").Copy _
                    before:=ActiveWorkbook.Sheets("Data")
                    Set ws = ActiveSheet
                    ws.Name = "printen"
                    End With

                    'Application.DisplayAlerts = False
                    'Sheets("printen").Delete
                    'Application.DisplayAlerts = True
                End If
            Next
        End If
    Next

End Sub

看起来你在循环中有重复的操作,你正在寻找类似开关的操作。我猜你的意思是将 CheckBox 的编号解析为变量 j。当你得到它时,循环的其余部分类似于:

    ... Prepare variables for this loop round ...

    If j = 1 Then
        ... do something ...
    Else
        ... do something else ...
    End If

    ... Put here the part that stays the same regardless the j value ...

并且本节不需要 For 循环。

我在这里看到的一个问题是您多次使用变量 j。

j = Mid(ctrl.Name, 9, 2)
...
For j = 1 to 1
...
For j = 2 to 4
...

行 j = Mid(ctrl.Name, 9, 2) 将为 j 分配一些值。

行 For j = 1 to 1 将设置 j = 1 并循环一次。

行 For j = 2 to 4 将设置 j = 2 并在每个循环中递增 j(运行三次)

您确定它是在 For j = to 1 循环上循环,而不只是进入第二个循环吗?

Sub test()

j = 2 + 3
Debug.Print j

For j = 99 to 99
 Debug.print j
Next

For j = 2 to 4
 Debug.print j
Next

End Sub

这输出值 5、99、2、3、4

当值的数字顺序乱序时,可能会更明显。

对于同一个 J,您有 2 个循环。如果您需要针对不同的 J 值编写不同的代码,我认为此解决方案可能会有所帮助:

ja = ja&"|"&Mid(ctrl.Name, 9, 2)
j = split(ja,"|")
for i = 0 to uBound(j)
if cInt(j(i))=1 then do something
if j(i)>1 AND j(i)<5 then do something 'j=2,3,4
if j(i)>4 AND j(i)<26 then do something 'j=5-25
next

BUT Mid(ctrl.Name, 9, 2) 表示您有 两个 符号,对于 CheckBox1,它是“x1 ”,不是1”。这意味着,在您的代码中 jx1。 您需要将复选框重命名为两位数索引,例如 "CheckBox01"

或者,您可以再添加一行:

j = Mid(ctrl.Name, 9, 2)
IF LEFT(j,1)="x" then j=RIGHT(j,1)
For j = 1 to 25
if j = 1 then....
if j >1 then...
next

这允许您只有 1 来自 x1

编辑 刚刚注意到,"CheckBox1" 的长度是 9。您可能需要从右边的 2 个符号中获取复选框编号:

j = RIGHT(ctrl.Name,2)

然后,去掉 "x":

IF LEFT(j,1)="x" then j=RIGHT(j,1)

您可能将 For j = 循环与 if j = 混淆了

for j = 会将您的变量设置为等于它后面的值

使用 select case j 语句

可能会更好
Private Sub PrintKnop_Click()
    Dim ctrl As MSForms.Control
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "CheckBox" And Left(ctrl.Name, 8) = "CheckBox" And ctrl.Value = True Then
            Dim j As Integer
            j = Mid(ctrl.Name, 9, 2)
            Select Case j
                Case 1
                    'it should be possible to adjust the range.
                    Dim ws As Worksheet
                    With ThisWorkbook
                      Worksheets("Veiligheid").Copy _
                        before:=ActiveWorkbook.Sheets("Data")
                      Set ws = ActiveSheet
                      ws.Name = "print"
                    End With
                    'Application.DisplayAlerts = False
                    'Sheets("print").Delete
                    'Application.DisplayAlerts = True
                    'These shouldn't be comments, but if I uncomment it, it won't show the failures.
                Case 2 To 4
                    With ThisWorkbook
                      Worksheets("Veiligheid").Copy _
                        before:=ActiveWorkbook.Sheets("Data")
                      Set ws = ActiveSheet
                      ws.Name = "printen"
                    End With
                    'Application.DisplayAlerts = False
                    'Sheets("printen").Delete
                    'Application.DisplayAlerts = True
            End Select
        End If
    Next
End Sub