Results 1 to 6 of 6

Compare Strings

Threaded 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.

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. 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