Can't figure out match function

KasperC

New Member
Joined
May 11, 2023
Messages
49
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello,

I'm having troubles searching through an array.
I'm either getting a Mismatch or "unable to get the match property of..." error no matter what I try.
I'm I deeply misunderstanding something?

I'm trying to check if a value (string) appears further down the data-set, and if so print the adjacent values to the hit and the search value to a different sheet.
If the string does appear, and the adjacent value is the same as the search value - don't want to print.
If the string appears several times, I want the adjacent values of the next ones to print into the next column of the same "string".

This might not be the best way to comb through, If you have any suggestions I'm more than willing to try them.

VBA Code:
Sub MREXCEL()
    Dim ws As Worksheet
    Dim wso As Worksheet
    Dim wb As Workbook
    Dim r
    Dim a, b, c
    Dim i As Long, LRow As Long, LRowo As Long, y As Long
    Dim Søk As String, Søkr As String, Retur As String
    Dim Returnr As Long
    Dim Arr() As Variant
    Dim Match As String
   
    Set wb = Excel.Workbooks("test.xlsm")
    Set ws = wb.Worksheets("Sheet2")
    Set wso = wb.Worksheets("Sheet3")
    LRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
    LRowo = wso.Cells(Rows.Count, 1).End(xlUp).Row
   
    a = Range(ws.Cells(1, 1), ws.Cells(LRow, 1)).Resize(, 7)

    ReDim r(1 To UBound(a, 1))
   
    For y = 1 To UBound(a, 1)
        r(y) = a(y, 1)
    Next y
   
    For i = 2 To UBound(a)
        Søkr = a(i, 3)
        x = i + 1
       
        Do While x < LRow + 1
            r = Range(ws.Cells(x, 3), ws.Cells(LRow, 1))
            If Not IsError(Application.WorksheetFunction.Match(Søkr, r, 0)) Then        '//The error appears at this very first Match function.
                    Match = Application.WorksheetFunction.Match(Søkr, r, 0)
                If Not a(i, 4) = a(Match, 2) Then
                    If IsError(Application.WorksheetFunction.Match(Søkr, Range(wso.Cells(1, 1), wso.Cells(LRowo, 1)), 0)) Then
                        wso.Cells(LRowo, 1) = a(i, 1)
                        wso.Cells(LRowo, 2) = a(i, 3)
                        wso.Cells(LRowo, 3) = a(Match, 1)
                       
                        LRowo = wso.Cells(Rows.Count, 1).End(xlUp).Row
                    Else
                        Matcho = Application.WorksheetFunction.Match(Søkr, Range(wso.Cells(1, 1), wso.Cells(LRowo, 1)), 0)
                        wso.Cells(Matcho, 1).End(xlToLeft) = a(Match, 1)
                    End If
                Else
                    x = Match + 1
                End If
            End If
        Loop
    Next i

End Sub

Thank you for your time

Sincerely,
Kasper
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try changing this:
Rich (BB code):
If Not IsError(Application.WorksheetFunction.Match(Søkr, r, 0))
to
If Not IsError(Application.Match(Søkr, r, 0))
 
Upvote 1
Solution
Try changing this:
Rich (BB code):
If Not IsError(Application.WorksheetFunction.Match(Søkr, r, 0))
to
If Not IsError(Application.Match(Søkr, r, 0))
Worked like a charm, thank you so much!

Is there any reason as of why that you know of?
 
Upvote 0
Glad we could help.

Using WorksheetFunction is useful when initially building the formula in VBA because it supports intellisense. However once you are happy with the formula by dropping the WorksheetFunction wording makes errors easier to trap.
WorksheetFunction.Match if not found produces a VBA runtime error that you have to handle with an On Error statement and check for err <> 0
Using Application.Match if it doesn't find it, it doesn't produce a VBA error and you can test for it with IsError or even use IfError on it.

It is discussed here using Vlookup as an example under the heading "Dealing with Errors"

For a more technical discussion see here (also uses Vlookup)
 
Upvote 1

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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