Loop and If - If condition is met, but loop jumps over it

makiwara

Board Regular
Joined
Mar 8, 2018
Messages
171
Hi! I can't solve a problem, and I would be really gladful if somebody could help me with my code. While debugging, it jumps a huge. What can be the problem?

Cells(j,g).Value
= "anxious"
temp = "anxiety"

Code:
If Left(temp, 3) = Left([B]Cells(j, g).Value[/B], 3) Then [U][COLOR=#008000]'It's true, code goes on (anx = anx)[/COLOR][/U]                
                    For k = Len(temp) To 2   [U][COLOR=#008000]'here code jumps a huge...[/COLOR][/U]
                        

                       testVariable= Mid([COLOR=#ff0000]temp[/COLOR],1,k-1) 'IT STAYS EMPTY
                        If InStr(1, [B]Cells(j, g).Value,[/B] Mid([COLOR=#ff0000]temp[/COLOR], 1, k - 1)) >= 1 Then 
              
'I want to loop from the last character of temp (anxiety), and find the most similar word. So 
' InStr(1,"anxious", mid("anxiety",1,k-1) -->
'can it find "anxiet" in "anxious"? NO so goes on.
'can it find "anxie" in "anxious"? NO so goes on.
'can it find "anxi" in "anxious"? YES so stores "anxiety" in Cells(i,f).Value


                            [I]Cells(i, f).Value[/I] = [B]Cells(j, g).Value [/B]
                            GoTo Skip
                        End If
                    Next
                        
                        
                End If  [U][COLOR=#008000]'.. and code lands here[/COLOR][/U]
                

Skip: ...
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Is Len(temp) is > 2? I.e For k = Len(temp) and k > 2 so it's "jumping" out of the loop as the condition is not satisfied - would be my guess.

Try
Rich (BB code):
For k = Len(temp) to 2 Step - 1
 
Last edited:
Upvote 0
What a mistake, I spent 1 hour thinking about it!

Thank you very much, Jack!!!

Is Len(temp) is > 2? I.e For k = Len(temp) and k > 2 so it's "jumping" out of the loop as the condition is not satisfied - would be my guess.

Try
Rich (BB code):
For k = Len(temp) to 2 Step - 1
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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