• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How to run a loop based on the len value in the cell [SOLVED]

ThrottleWorks

Excel Ninja
Hi,


I want to run a loop.

The loop has to run for x times.


This x will be present in cell C1.


I have 2 values, cell a1 & cell b1.

I have following formula in cell c1.


=IF(LEN(A1)>LEN(B1),LEN(A1),LEN(B1))


this will give me a number, for example 5.


I want to run my loop 5 times, cause my len value is 5.


Can anyone help me in this please.


P.S. - I think I am able to solve, as I started writing the problem I realized how to do it :)


Thanks.
 
Hi Indian,


thanks a lot for the hlp.

I did the same, only change is, I defined r as worksheetname.range("C1").value.


So code is For i = 1 to r


Have a nice day ahead.
 
Throttleworks


A basic loop for your problem


For i = 1 To [C1]


Next i


Can you explain the broader problem? Loops are typically slow and there is more than likely another way without them.


Take care


Smallman


EDIT: There was 1 minute between the post above and this post.
 
Hi Smallman, thanks for the help, sorry for late reply.


I am trying to achieve http://chandoo.org/forums/topic/how-to-compare-to-text-values


As I had no idea how to proceed, I tried loop, after trying loop, I thouth this is not required & will take time (as advised by you) so I am using vLookup now.


For the problem mentioned in the link.

I am trying Len, Mid, True to get the results.


I have just started, I will post the code once it is done.

So that I will be able to get the guidence from the forum.


Have a nice day ahead.
 
Hi Smallman, I have posted my code at the below link.


I am still testing it & trying to impprove it.


http://chandoo.org/forums/topic/how-to-compare-to-text-values#post-135218


Thanks for the help.
 
Hi Throttleworks


I had a look at your code and it is Deffo in line for some tweeking. I would prefer to go at it when I can access your file which will be much later tonight. I will be able to reduce your code and there are no loops required to replicate your vb.


It is all good!


Smallman
 
Try:

[pre]
Code:
Function TextCompare(Str1 As Variant, Str2 As Variant, Optional StrNo As Integer = 1) As Variant

Dim i As Integer
Dim Longs

Longs = Len(Str1)
If Len(Str2) > Len(Str1) Then Longs = Len(Str2)

For i = 1 To Longs
If Mid(Str1, i, 1) <> Mid(Str2, i, 1) Then Exit For
Next i

If StrNo = 1 Then
TextCompare = Mid(Str1, i, 1)
Else
TextCompare = Mid(Str2, i, 1)
End If

End Function
[/pre]

To use


=TextCompare(A2,B2)

Will show the first difference from the first string A2


=TextCompare(A2,B2,2)

Will show the first difference from the second string B2
 
Hui Sir, sorry for late reply, I was on leave for 2 days due to sickness hence could not reply.


I am trying this & will share the results, thanks a lot for the help & your valuable time.


Have a nice day ahead.
 
Hui Sir, I will use the code given by you, it is really fantastic.


Thanks a lot for the help.


@Smallman, thanks a lot for the help, sorry for late reply, have a nice day ahead.
 
Good evening bobhc Sir, sorry if this is a cross post.

I was not trying to cross post.


My original query was

http://chandoo.org/forums/topic/how-to-compare-to-text-values


While working on this I faced this loop issue & posted it separately.


In Future I will take utmost care to avoid such posts.
 
Back
Top