Home > Mobile >  Loop running 6 times instead of 1
Loop running 6 times instead of 1

Time:08-16

I am having issued with this code, the loop is supposed to only run once but its running 6 times and passing all of the data even though the toggle buttons are not pressed

the way this is supposed to work is that only the data that has the toggle button attached to it will be transferred to column C and E. its working but, its passing all the data and more even if the toggle buttons are not pressed

    Private Sub SubmitCSR_Click()
    Dim LastRow As Long
    Dim ws As Worksheet: Set ws = Sheets("GP count")
    
    Dim StockCaption(1 To 6) As String
   'populate the array
   StockCaption(1) = ToggleButton6.Caption
   StockCaption(2) = ToggleButton2.Caption
   StockCaption(3) = ToggleButton4.Caption
   StockCaption(4) = ToggleButton5.Caption
   StockCaption(5) = ToggleButton1.Caption
   StockCaption(6) = ToggleButton3.Caption
   'declare a variant to hold the array element
   Dim StockCC As Variant
   
   Dim ReceivedV(1 To 6) As String
   'populate the array
   ReceivedV(1) = R1.Value
   ReceivedV(2) = R2.Value
   ReceivedV(3) = R3.Value
   ReceivedV(4) = R4.Value
   ReceivedV(5) = R5.Value
   ReceivedV(6) = R6.Value
   'declare a variant to hold the array element
   Dim ReceivingN As Variant
   'loop through the entire array
   Dim strNames(1 To 6) As Variant
   'populate the array
   strNames(1) = ToggleButton6.Value
   strNames(2) = ToggleButton2.Value
   strNames(3) = ToggleButton4.Value
   strNames(4) = ToggleButton5.Value
   strNames(5) = ToggleButton1.Value
   strNames(6) = ToggleButton3.Value
   'declare a variant to hold the array element
   Dim StockValue As Variant
   'loop through the entire array
   For Each StockValue In strNames
   For Each ReceivingN In ReceivedV
   For Each StockCC In StockCaption
    If StockValue = True Then
    LastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
    ws.Range("C" & LastRow   1).Value = StockCC
    LastRow = ws.Cells(ws.Rows.Count, "E").End(xlUp).Row
    ws.Range("E" & LastRow   1).Value = ReceivingN
   End If
Next StockCC
Next ReceivingN
Next StockValue
End Sub

CodePudding user response:

You probably only want 1 standard for loop:

Private Sub SubmitCSR_Click()
    Dim LastRow As Long
    Dim ws As Worksheet: Set ws = Sheets("GP count")
    
    Dim StockCaption(1 To 6) As String
    'populate the array
    StockCaption(1) = ToggleButton6.Caption
    StockCaption(2) = ToggleButton2.Caption
    StockCaption(3) = ToggleButton4.Caption
    StockCaption(4) = ToggleButton5.Caption
    StockCaption(5) = ToggleButton1.Caption
    StockCaption(6) = ToggleButton3.Caption
    
    Dim ReceivedV(1 To 6) As String
    'populate the array
    ReceivedV(1) = R1.Value
    ReceivedV(2) = R2.Value
    ReceivedV(3) = R3.Value
    ReceivedV(4) = R4.Value
    ReceivedV(5) = R5.Value
    ReceivedV(6) = R6.Value
    
    'loop through the entire array
    Dim strNames(1 To 6) As Variant
    'populate the array
    strNames(1) = ToggleButton6.Value
    strNames(2) = ToggleButton2.Value
    strNames(3) = ToggleButton4.Value
    strNames(4) = ToggleButton5.Value
    strNames(5) = ToggleButton1.Value
    strNames(6) = ToggleButton3.Value
    'declare a variant to hold the array element
    
    Dim i As Long
    For i = 1 To 6
        If StockCaption(i) = True Then
            LastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
            ws.Range("C" & LastRow   1).Value = strNames(i)
            LastRow = ws.Cells(ws.Rows.Count, "E").End(xlUp).Row
            ws.Range("E" & LastRow   1).Value = ReceivedV(i)
        End If
    Next i
End Sub
  • Related