Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: Looping Through String Using Excel VBA!

  1. #11
    Junior Member
    Join Date
    Sep 2014
    Posts
    7
    Rep Power
    0
    Quote Originally Posted by snb View Post
    Do you want to compare only A1 to B1 or A1 to every string in column B ?

    I would split the values in column A to colum A:E (texttocolumns)

    Then it's easy to compare the values in A1:E1 to the string in column F and put the result of the comparison in column G

    See the attachment.

    I would like to compare A1 to every string in column B.

  2. #12
    Senior Member
    Join Date
    Jun 2012
    Posts
    337
    Rep Power
    13
    Did you look at the attachment ?

  3. #13
    Junior Member
    Join Date
    Sep 2014
    Posts
    7
    Rep Power
    0
    Quote Originally Posted by snb View Post
    Did you look at the attachment ?
    Yes, and I am confused. It doesn't look like what I am looking for.
    Using text to columns is equivalent to splitting each string into an array with the delimiter "," using the split() function. I executed your code and it still confuses me.

    The task I am trying to solve is: I have two strings. I need a quick way to find how many tokens or numbers they have in common.
    Last edited by Jdean; 09-08-2014 at 06:02 AM.

  4. #14
    Member p45cal's Avatar
    Join Date
    Oct 2013
    Posts
    94
    Rep Power
    12
    If each list conatains say 1000 items, with a nested loop you could be making 1000 x 1000 = 1 million comparisons.
    Have you considered using an Exit For/Exit Do once a single match is found?
    Are both lists already sorted (it looks like they may be)? If so, instead of comparing all values in one list with all values in the other, you could walk down both lists at the same time:
    First compare both first items in the lists and see which one is smaller, then iterate down the list which had the smaller value until you get a match or the value is bigger, then stop running down that list and continue running down the other list until again, a match or bigger value is encountered etc. That way only 2000 comparisons get made.
    Eg. using your example:
    list A: "5, 45, 100, 113, 160"
    list B: "2, 4, 45, 160, 189"

    compare 5 and 2. 2 is smaller so run down list B:
    compare 5 and 4
    compare 5 and 45
    stop at the 45 and start running down list A:
    compare 45 and 45 (match, so add to the count)
    compare 45 and 100
    stop at 100 to flip over:
    compare 100 and 160
    stop at 160 to flip over:
    compare 160 and 113
    compare 160 and 160 (match, so add to the count)
    compare 160 and 189
    stop at 189 to flip over (or since you've come to the end of one of the lists there's no need to continue).

    Using your example we need only make 8 comparisons instead of 25.

    Perhaps post the code you already have?
    Perhaps attach a workbook?

    edit post posting:
    something along these lines (neither debugged nor streamlined):
    Code:
    Sub blah()
    s1 = "5, 45, 100, 113, 160"
    s2 = "2, 4, 45, 160, 189"
    x = Split(s1, ",")
    y = Split(s2, ",")
    ix = 0: iy = 0
    Do Until ix > UBound(x) Or iy > UBound(y) 'put ubounds into variables so that they don't need to be calculated in each iteration.
      'Debug.Print x(ix), y(iy), IIf(CLng(x(ix)) = CLng(y(iy)), "Match!", "")
      If CLng(x(ix)) = CLng(y(iy)) Then Count = Count + 1 'I've assumed whole numbers; requires a tweak for singles or doubles.
      If CLng(x(ix)) >= CLng(y(iy)) Then iy = iy + 1 Else ix = ix + 1
    Loop
    MsgBox Count & " item(s) in common"
    End Sub
    Last edited by p45cal; 09-10-2014 at 09:32 PM.

  5. #15
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    14
    What about doing it with no loops? Here is a one-liner (albeit a long one) UDF (user defined function) that you can use directly in a formula on a worksheet...
    Code:
    Function CountDupes(R1 As Range, R2 As Range) As Long
      CountDupes = Evaluate("SUM(0+ISNUMBER(TRANSPOSE(FIND("", ""&TRIM(MID(SUBSTITUTE(" & _
                   R1.Address & ","", "",REPT("" "",99)),ROW(A1:A" & UBound(Split(R1.Value, _
                   ",")) + 1 & ")*99-98,99))&"", "","", ""&" & R2.Address & "&"", ""))))")
    End Function
    HOW TO INSTALL UDFs
    ------------------------------------
    If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use CountDupes just like it was a built-in Excel function. For example,

    ="Number of matching numbers = " & CountDupes(A1,B1)

    If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
    Last edited by Rick Rothstein; 09-12-2014 at 08:30 AM.

  6. #16
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    14
    Quote Originally Posted by Rick Rothstein View Post
    What about doing it with no loops? Here is a one-liner (albeit a long one) UDF (user defined function) that you can use directly in a formula on a worksheet...
    Code:
    Function CountDupes(R1 As Range, R2 As Range) As Long
      CountDupes = Evaluate("SUM(0+ISNUMBER(TRANSPOSE(FIND("", ""&TRIM(MID(SUBSTITUTE(" & _
                   R1.Address & ","", "",REPT("" "",99)),ROW(A1:A" & UBound(Split(R1.Value, _
                   ",")) + 1 & ")*99-98,99))&"", "","", ""&" & R2.Address & "&"", ""))))")
    End Function
    As a follow up to my one-liner above, I should mention two things. First, although I presented the above function as a UDF (in Message #15), it can be called from other VBA procedures as well, just make sure to pass both arguments as Ranges, not text Strings. Second, there is a worksheet formula equivalent to the one-liner function above... instead of installing the code and calling the CountDupes formula, you can use this array-entered** formula directly on the worksheet

    =SUM(0+ISNUMBER(TRANSPOSE(FIND(", "&TRIM(MID(SUBSTITUTE(A1,", ",REPT(" ",99)),ROW(INDIRECT("A1:A"&LEN(A1)-LEN(SUBSTITUTE(A1,",",""))+1))*99-98,99))&", ",", "&B1&", "))))

    ** Commit this formula using CTRL+SHIFT+ENTER and not just Enter by itself.

Similar Threads

  1. Replies: 15
    Last Post: 08-23-2013, 12:03 PM
  2. VBA Looping Input Range and Output Range
    By Whitley in forum Excel Help
    Replies: 7
    Last Post: 04-25-2013, 09:02 PM
  3. Looping through Each Files and Folders
    By Rajan_Verma in forum Rajan Verma's Corner
    Replies: 0
    Last Post: 04-18-2012, 12:12 AM
  4. Replies: 10
    Last Post: 04-07-2012, 05:33 AM
  5. Looping Through Each Internet Explorer
    By Rajan_Verma in forum Rajan Verma's Corner
    Replies: 3
    Last Post: 03-27-2012, 07:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •