VBA: No match string comparison issue

dm321321

New Member
Joined
Mar 8, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have two arrays of strings, called InputAlias and OutputAlias. I compare them starting from the first element in InputAlias and, when there is a match, I extract the location in OutputAlias. For example
InputAlias = (a,b,c,d,f,e)
OutputAlias = (a,d,e)

I loop starting with "a" in InputAlias and I get the location 1.

I have been using this code:
Code:
For i = 0 To 10
        
    For jj = LBound(InputAlias) To UBound(InputAlias)
        If StrComp(OutputAlias(i), InputAlias(jj, 1), vbTextCompare) = 0 Then
            LocationArray = jj
            Exit For
        End If
    Next

    OutputValue(i) = InputValue(LocationArray)

Next

And it has been working great except when there is no match. I didn't notice this issue earlier because all the elements in OutputAlias are in InputAlias (the reverse is not true). I noticed it when I wrongly added an element in Input Alias that was not in OutputAlias.

It seems when there is no match, LocationArray takes the last value (or the value with the closest match ... not sure) and wrongly replaces OutputValue. Is it possible to modify the code to skip the element when there is no matches? Maybe add an error warning about the problem?

Thanks!
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi, as a starting point see what the variable X contains using this VBA demonstration :​
VBA Code:
    Dim V, W, X
        V = [{"a","b","c","d","f","e"}]
        W = [{"a","d","e"}]
        X = Application.Match(W, V, 0)
 
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,446
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top