Results 1 to 10 of 16

Thread: Looping Through String Using Excel VBA!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    Welcome to board.

    You could try these methods.

    Code:
    Option Explicit
    
    Sub kTest()
        
        Dim x, y, i As Long, c As Long
        Dim s1 As String, s2 As String
        
        s1 = "5, 45, 100, 113, 160"
        s2 = "2, 4, 45, 160, 189"
        
        x = Split(s1, ",")
        y = Split(s2, ",")
        
        With CreateObject("scripting.dictionary")
            .comparemode = 1
            For i = 0 To UBound(x)
                If Len(Trim(x(i))) Then
                    .Item(Trim(x(i))) = 1
                End If
            Next
            
            For i = 0 To UBound(y)
                If .exists(Trim(y(i))) Then
                    c = c + 1
                End If
            Next
            MsgBox "There are " & c & " item(s) in common."
        End With
        c = 0
        For i = 0 To UBound(y)
            If InStr(1, ", " & s1 & ", ", ", " & Trim(y(i)) & ", ", 1) Then 'careful about the delimiter
                c = c + 1
            End If
        Next
        MsgBox "There are " & c & " item(s) in common."
        
    End Sub
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  2. #2
    Junior Member
    Join Date
    Sep 2014
    Posts
    7
    Rep Power
    0
    Thanks, Admin. I created a function using the Dictionary data structure method you provided. It worked but when i tested it for large datasets (in the hundreds of thousands) it didn't run any faster than the good ole fashioned two For-loops array method I was using.

    Your second method using the Instr function looks interesting, but before I try it I would like for you to slowly explain the arguments for me. I really want to understand what is in the Instr() parentheses. I know it has the structure InStr( [start], string, substring, [compare] ) but I couldn't easily sync it up with your coded arguments.
    Also, will the number of delimiters stay the same regardless of the length of S1 and S2 for the Instr() method? (Of course, I assume each input string will always contain more than one token).

    Jdean

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
  •