jetbenny
jetbenny

Reputation: 1

VBA Excel Function - Invalid Qualifier

Building a VBA excel function that compares each position of 2 string arguments to see if they are equal then return the distance between them.

Ex. Given "the" and "dog" should return 3. Given "cat" and "hat" should return 1.

Currently using the below code the function fails compile with Invalid Qualifier - on the line "str1.Chars(i)", the help page explains that this would mean that str1 is out of scope for this code block, and I was unsure how that was possible as other examples I have searched are able to use their arguments in the function.

Function hamming_dist(str1 As String, str2 As String) As Integer

hamming_dist = 0
    
For i = 0 To Len(str1)
    If str1.Chars(i) = str2.Chars(i) Then
        hamming_dist = hamming_dist + 1
    End If
Next
    
End Function

UPDATED code:

Function hamming_dist(str1 As String, str2 As String) As Integer

hamming_dist = 0
    
For i = 1 To Len(str1)
    If Mid(str1, i, 1) <> Mid(str2, i, 1) Then
        hamming_dist = hamming_dist + 1
    End If
Next
    
End Function

Upvotes: 0

Views: 419

Answers (2)

Apafey
Apafey

Reputation: 66

you are looking for mid() function, that cuts a char from the string.

actualy, your code calculates the same chars and not the different ones.

Upvotes: 1

D&#225;vid Laczk&#243;
D&#225;vid Laczk&#243;

Reputation: 1101

As Warcupine's comment also says, you were looking at Visual Basic .NET documentation. In VBA, String is not of type object, it does not have any methods/properties. When you Google, always add "VBA" and check if the page is really about that.

Upvotes: 1

Related Questions