Match Formula Returning Wrong Value

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,616
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi Folks,
Looking for someone that might be able to provide reason as to why I am getting the wrong result for 'cr' in my code below.
The line highlighted in purple calculates the value of 'cr' by referencing column A of worksheet ws_cd for the value of 'leag'.
When league = "WMSA", the value of CR should be 3. But it's being returned as 4.
In ws_cd, cell A3 = "WMSA" and cell A4 = "WMGSA"

I don't understand why my formula is reporting the wrong value.

Rich (BB code):
Sub customer()
    Dim cr as long
    Dim leag as string
    Dim c as double
    Stop
    mbevents = False
    leag = ws_form.Range("K6")
    c = Application.WorksheetFunction.CountIf(ws_cd.Columns(1), leag)
    ws_form.Unprotect
    
    If c > 1 Then
        MsgBox "Error: League  has too many entries"
        Stop
    
    ElseIf c = 1 Then 'customer info on file
        
        cr = Application.WorksheetFunction.Match(leag, ws_cd.Columns(1))
        With ws_form
            With .Range("K15, V15")
                .Interior.Color = RGB(198, 228, 202)
                '.Borders.Color = RGB(55, 86, 35)
                If .MergeCells = True Then
                    Set mergeRange = .MergeArea
                    mergeRange.Locked = False
                End If
            End With
            .Range("K15").Value = ws_cd.Cells(cr, 2)
            .Range("V15") = ws_cd.Cells(cr, 3)
        End With
    
    Else
        With ws_form.Range("K15")
            If .MergeCells = True Then
                Set mergeRange = .MergeArea
                mergeRange.Locked = False
            End If
            .Value = ""
            .Interior.Color = RGB(221, 235, 247)
            '.Borders.Color = RGB(48, 84, 150)
            .Select
        End With
        With ws_form.Range("V15")
            If .MergeCells = True Then
                Set mergeRange = .MergeArea
                mergeRange.Locked = False
            End If
            .Value = ""
            .Interior.Color = RGB(221, 235, 247)
            '.Borders.Color = RGB(48, 84, 150)
        End With
    End If
    chg = 0 'user change counter
    mbevents = True
    
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
@Ark68
I think you'll find that you need to include the Match TypeParameter

cr = Application.WorksheetFunction.Match(leag, ws_cd.Columns(1),0)

Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,224,836
Messages
6,181,248
Members
453,026
Latest member
cknader

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