For loop not exiting with Exit For

shakeeb92

New Member
Joined
Nov 18, 2013
Messages
15
Hello people,

I have this code that loops through the cells of a single row. For some reason, the "Exit for" is not working making vba copy the string to the 33 cells. Ideally, this code should check the looped cell if it is equal to the string. If not, just paste it on the next available cell in the row. the string value changes through another loop in my full code.

Here is the code:


Code:
Sub check()
'
Dim uniquevendor As String
uniquevendor = "shakeeb92"
Set Krange = Sheets("sheet3").Range("B1:B33") '33 is a random number just to make sure that it checks all the cells in the row
                     For Each Kcell In Krange
                     If Kcell.Value = uniquevendor Then
                     Exit For
                     Else
                     If Range("b1").Value = "" Then
                     Range("b1").Value = uniquevendor
                     Else
                     Sheets("sheet3").Range("A1").End(xlToRight).Select
                     ActiveCell.Offset(0, 1) = uniquevendor
                     End If
                     End If
                     Next Kcell
'
End Sub


Thanks for the help
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
The way you have sequenced your For loop, if a cell containing uniquevendor is found, you exit the loop and go to the End Sub line, so if the first cell checked contains uniquevendor the sub will end. Why do you think the Exit For is not working?
 
Upvote 0
Hey JoeMo,

Yes, I was hoping that the code returns the same result as you said. I have this code inside an another loop which loops to assign the uniquename to different values(strings) . Now, when i do the 1st run with an empty sheet, the code adds the uniquename for 4 times (assigned by the larger loop) which is right. when i re-run again to validate, the code works well for the first uniquename (it does not add it again) but for the other 3 uniquename it does add them which is wrong. hope my explanation was clear



The way you have sequenced your For loop, if a cell containing uniquevendor is found, you exit the loop and go to the End Sub line, so if the first cell checked contains uniquevendor the sub will end. Why do you think the Exit For is not working?
 
Upvote 0
Hey JoeMo,

Yes, I was hoping that the code returns the same result as you said. I have this code inside an another loop which loops to assign the uniquename to different values(strings) . Now, when i do the 1st run with an empty sheet, the code adds the uniquename for 4 times (assigned by the larger loop) which is right. when i re-run again to validate, the code works well for the first uniquename (it does not add it again) but for the other 3 uniquename it does add them which is wrong. hope my explanation was clear
Sorry, I don't understand what you want the code to accomplish. Perhaps you could post the entire code and an explanation of the expected output.
 
Upvote 0

Forum statistics

Threads
1,215,013
Messages
6,122,694
Members
449,092
Latest member
snoom82

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