VBA for循环无效

时间:2017-11-09 17:17:05

标签: vba excel-vba userform excel

我的循环遇到了麻烦。我想制作一个工作表,打印它(还没有构建,我知道它是如何工作的),然后删除它。之后继续下一个j做同样的事情。但它重新将j = 1转换为1,因此它试图创建一个名为" print"的第二个工作表。那是不可能的。

我有名字的复选框:CheckBox1,CheckBox2,CheckBox' j'。我想从CheckBox1开始,以CheckBox25结束。如果确实如此,则打印表格。

我想我需要摆脱第一个For:
对于每个ctrl In Me.Controls
但我不知道怎么做。因为我需要它来指定变量' 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

4 个答案:

答案 0 :(得分:3)

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

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

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

对于j = 1到1的行将设置j = 1并循环一次。

对于j = 2到4的行将设置j = 2并且每个循环增加j(运行三次)

你确定它是循环的吗?对于j = 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

当值超出数字顺序时可能更明显。

答案 1 :(得分:2)

看起来你在循环中有重复操作,你正在寻找类似开关的操作。我猜你的意思是将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循环。

答案 2 :(得分:0)

你有2个循环用于相同的J.如果你需要你的代码不同的东西用于不同的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

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

或者,你可以多添加一行:

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

这样您只能 x1

1

修改 注意,“CheckBox1”的长度为9.您可能需要从右侧2个符号中获取复选框编号:

j = RIGHT(ctrl.Name,2)

并且,摆脱“x”:

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

答案 3 :(得分:0)

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

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

使用精选案例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