Steffie
Steffie

Reputation: 11

VBA for loop not working

I'm having troubles with my loop. I want to make a worksheet, print it (not build in yet, I know how it works), then delete it. After that proceed to the next j to do the same. But it is relooping the j = 1 to 1, so it's trying to create a second worksheet named "print" and that's not possible.

I have checkboxes with name: CheckBox1, CheckBox2, CheckBox'j'. I want to start with CheckBox1 and end with CheckBox25. If it's true then print the sheet.

I think I need to get rid of the first For:
For Each ctrl In Me.Controls
But I don't know how. Because I need it to specify the variable '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

Upvotes: 0

Views: 2587

Answers (4)

Salek
Salek

Reputation: 303

You have 2 loops for same J. If you need your code different things for different J value, I think this solution might help:

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 the Mid(ctrl.Name, 9, 2) means you are having TWO symbols, and for CheckBox1 it is "x1", NOT "1". That means, in your code j is x1. You need to rename your checkboxes to two-digit index, like "CheckBox01"

OR, you might add one line more:

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

This allows you to have only 1 from x1

EDIT Just noticed, that length of "CheckBox1" is 9. You might need to get the checkbox number from right 2 symbols:

j = RIGHT(ctrl.Name,2)

And, get rid of "x":

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

Upvotes: 0

Clyde
Clyde

Reputation: 193

You may be confusing your For j = loop with if j =

for j = will set your variable equal to the value following it

you'd probably be better off with a select case j statement

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

Upvotes: 0

FirmwareRootkits
FirmwareRootkits

Reputation: 131

One problem I see here is that you are using the variable j multiple times.

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

The line j = Mid(ctrl.Name, 9, 2) will assign some value to j.

The line For j = 1 to 1 will set j = 1 and loop one time.

The line For j = 2 to 4 will set j = 2 and increment j each loop (runs three times)

Are you sure it is looping on For j = to 1 loop and not just moving on to the second loop?

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

This outputs values 5, 99, 2, 3, 4

It might be more obvious when the values are out of numerical order.

Upvotes: 3

Captain Stone
Captain Stone

Reputation: 446

It looks like you have repeating operations in the loop and you are looking for switch-like operation. I guess you mean to parse the number of the CheckBox as the variable j. When you get it, the rest of the loop is something like:

    ... 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 ...

And no For-loop is needed in this section.

Upvotes: 2

Related Questions