+ Reply to Thread
Results 1 to 6 of 6

Compare Strings

Hybrid View

  1. #1
    Registered User
    Join Date
    05-23-2017
    Location
    São Paulo, Brazil
    MS-Off Ver
    MS Office 2013
    Posts
    12

    Compare Strings

    Hi Guys,

    I`m having a problem comparing two strings in VBA, I`m used to compare using languages like C, where i can iterate trough each string as string[i].
    But in VBA, it cannot be done like that, so I tried using Mid function, but it doenst allow me to use a variable as parameter, so... how can I achieve my goal?

    Sub compareSrings(ByVal str1 As String, ByVal str2 As String)
        Dim sizeStr1 As Integer
        Dim sizeStr2 As Integer
        Dim counter As Integer
        Dim matchCnt As Integer
        Dim i As Integer
        Dim a1 As Characters
        Dim a2 As Characters
        
        sizeStr1 = Len(str1)
        sizeStr2 = Len(str2)
        spcCharCnt = 0
        matchCnt = 0
        
        For i = 0 To sizeStr1
            If Mid(str1, i, 1) <> "*" Then
                counter = counter + 1
            End If
        Next i
    
        If sizeStr1 > sizeStr2 Then
            For i = 0 To sizeStr2
                a1 = Mid(str1, i, 1)
                a2 = Mid(str2, i, 1)
                If a1 = a2 Then
                    matchCnt = matchCnt + 1
                End If
            Next i
        Else
           For i = 0 To sizeStr1
                a1 = Mid(str1, i, 1)
                a2 = Mid(str2, i, 1)
                If a1 = a2 Then
                    matchCnt = matchCnt + 1
                End If
            Next i
        End If
        
        
        If matchCnt = spcCharCnt Then
            Debug.Print "Match"
        End If
    End Sub
    Last edited by Keytnknd; 05-26-2017 at 09:57 AM.

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Compare Strings

    What is the issue you have, your code looks nice
    Perhaps send a sample of you data
    - Battle without fear gives no glory - Just try

  3. #3
    Registered User
    Join Date
    05-23-2017
    Location
    São Paulo, Brazil
    MS-Off Ver
    MS Office 2013
    Posts
    12

    Re: Compare Strings

    the problem is :
    Run time error 5
    invalid procedure call or argument

    In the line
    If Mid(str1, i, 1) <> "*" Then

    I`m trying to compare for example app*e and apple , it should give me that they are equal, cause i m not counting the character *
    Last edited by Keytnknd; 05-26-2017 at 11:09 AM.

  4. #4
    Registered User
    Join Date
    05-23-2017
    Location
    São Paulo, Brazil
    MS-Off Ver
    MS Office 2013
    Posts
    12

    Re: Compare Strings

    I found the error, the For loop was starting from Index 0, but Mid needs to start from 1

  5. #5
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Compare Strings

    Easy, just ask ... (;-))

  6. #6
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Compare Strings

    Just for info - if you're happier to work with arrays like in C, you can stuff a string into an array in 1 call... (OK - so this is a whole sub, but the 'Split(strConv(...' line is the heart of it).
    Sub x()
    
       Dim y As String
       Dim z As Variant
       Dim iTemp As Integer
       
       y = "This is a test string"
       
       z = Split(StrConv(y, vbUnicode), Chr(0))
       
       For iTemp = LBound(z) To UBound(z)
          Debug.Print "Element " & iTemp & ": " & z(iTemp)
       Next
       
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 05-30-2015, 11:45 AM
  2. Compare two strings
    By voicetec in forum Excel General
    Replies: 19
    Last Post: 06-17-2014, 08:58 AM
  3. Compare Differences between 2 strings
    By wadafruck in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 12-11-2012, 07:09 PM
  4. Compare two strings
    By matrex in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 10-15-2012, 06:28 PM
  5. [SOLVED] Compare Strings Question
    By Sabo, Eric in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-23-2006, 11:20 PM
  6. [SOLVED] Compare data strings?
    By Ed in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-14-2005, 09:40 AM
  7. Compare the strings
    By yangyh in forum Excel General
    Replies: 3
    Last Post: 09-08-2005, 12:05 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1