Code loop

rickblunt

Well-known Member
Joined
Feb 18, 2008
Messages
609
Office Version
  1. 2019
Platform
  1. Windows
Hello, ever have one of those codes that just keep making you hit your head on the keyboard? lol As soon as I get one quirk figured out another one pops up. I am using the code below to pass information from a userform to two different WS's. Initially I was having issues with a listbox values but I got that sorted out. Now I have an issue with the data being sent to the worksheets. In the code, separately each both work perfect but when I put them together the entries on worksheet "Repair Log" gets repeated (as in entered) 26 times! It doesn't do it when I disable the code for the "Device List" section so it appears to be coming from that section. I am at a loss as to why it is happening 26 times, why 26?

Does anyone have any thoughts on why it is doing this and how I can stop it? I appreciate all of the assistance - thanks,

VBA Code:
Private Sub CommentButton_Click()

    Dim i As Long
    Dim Lastrow As Long
    Dim Found As Range
          
    Worksheets("Device List").Activate
    
    Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    
       For i = 1 To Lastrow
            If Cells(i, 8).Value = "FAIL" And Cells(i, 7).Value = "" Then
            Cells(i, 7).Value = RepairComments.Value
    Exit For
    End If
    
    '''''''''''''''''''''''
    With Sheets("Repair Log")
 
     nextrow = .Range("A" & Rows.Count).End(xlUp).Row + 1
 
     .Range("A" & nextrow) = DeviceId.Value
     .Range("B" & nextrow) = Now()
     .Range("C" & nextrow) = RepairComments.Value
     
    End With
    ''''''''''''''''''''''''
    
    Next
    
   Unload DataEntry
   DataEntry.Show
   
   Unload RepairForm
   RepairForm.Show
      
   If Sheets("Coding").Range("H8").Value > 0 Then
                                           
     RepairForm.Show
            
   Else
        
     Unload RepairForm
     MsgBox "No further repair comments are needed", , "Repair Comments"
           
    End If
   
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Looks like the section that is repeating is within the loop but not within the condition that ends the loop, meaning that it is processed every time regardless of whether or not the condition is met.

The lines
VBA Code:
Exit For
End If
need to be moved down so that they are immediately above Next then it should work as intended.
 
Upvote 0
Solution
It's probably because that section of code is inside the loop for the Device List
 
Upvote 0
It's probably because that section of code is inside the loop for the Device List
that makes sense and that is what I was thinking I was probably going to have to tweak - but why 26? :)
 
Upvote 0
but why 26?
Number of iterations of the loop until the If - And criteria are met and the loop ends. With the way the code is laid out, it might be 1 either way, my tiny mind is not processing it correctly.
See post 2 for the fix.
 
Upvote 0
Just move the Next to before the With, as Jason says the 26 is just the number of iterations through the loop, best guess is the If condition is matched on row 27.
 
Upvote 0
Just move the Next to before the With, as Jason says the 26 is just the number of iterations through the loop, best guess is the If condition is matched on row 27.
That's cool - that was what I did and everything is great. I am going to go look at row 27 to see if I can find the trigger. I appreciate the knowledge.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,999
Members
452,373
Latest member
TimReeks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top