Match Formula Returning Wrong Value

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
@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
Wonderful Snakehips, thank you!
 
Upvote 0

Forum statistics

Threads
1,215,202
Messages
6,123,625
Members
449,109
Latest member
Sebas8956

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