How to use the XMATCH function correctly in VBA code?

Anonymní
2023-04-13T23:30:52+00:00

Hello,

I used the =XMATCH() function in the worksheet to solve the problem and it worked fine. When I wanted to use the function in the VBA code to automate the task, it keeps reporting an error at runtime, see image. What is wrong in the VBA code?
How I wrote the VBA code can be seen in this sample (the original is written in the project in Module1):

Function Zanes(ByVal Vstup As String, ByVal Lst As String, ByVal Slp as Long) As Boolean

Dim Rdmax As Long, RnZdroj As Range, Nález As Variant

Worksheets(Lst).Activate ' sheet for writing down phrases

 '  the last line already filled in the phrase list

Rdmax = Cells(Rows.Count, Slp).End(xlUp).Row
' definition of the range in which the XMATCH function will search
Set RnZdroj = ActiveSheet.Range("B1:B" & Rdmax)

Nález = WorksheetFunction.XMatch(Vstup, RnZdroj, 0) ' the program is interrupted here

If IsError(Nález) Then ' XMATCH success test
Cells(Rdmax + 1, Slp) = Vstup ' the first entry of the phrase in the list
Cells(Rdmax + 1, Slp - 1) = 1 ' default value of the counter
Else

' if a phrase is repeated in the list, the counter will be incremented
Cells(Nález, Slp - 1) = Cells(Nález, Slp - 1) + 1
End If

Zanes = True
Set RnZdroj = Nothing
Worksheets(LstZd).Activate ' return to the DataSheet (and LstZd is a global variable)

End Function

Note: Even after specifying function calls such as:

Nález= Application.WorksheetFunction.XMatch(Input, RnSource, 0)  

the error was not resolved.

I am using MS EXCEL 365, on operating system:

Processor Intel(R) Core(TM) i5-6500T CPU @ 2.50GHz
Installed RAM 16.0 GB (usable: 15.9 GB)
System type 64-bit operating system, x64 platform processor
Pen and Touch Control Pen input and touch input are not available for this display.

Windows 10 Pro edition
Version 22H2
Installation date 17.01.2021
Operating System Build 19045.2728
Performance Windows Feature Experience Pack 120.2212.4190.0

Thank you for your reply. Yours sincerely
Stanislav

Microsoft 365 a Office | Excel | Pro domácnosti | Jiné

Otázka je uzamčená. Tato otázka se migrovala z komunity podpory Microsoftu. Můžete hlasovat, jestli je užitečná, ale nemůžete k ní přidávat komentáře či odpovědi ani ji nemůžete sledovat.

Počet komentářů: 0 Žádné komentáře

Odpovědi: 3

Seřadit podle: Nejužitečnější
  1. Anonymní
    2023-04-15T12:55:22+00:00

    Ahoj,

    byla tam chyba v tom, že nebyla pořádně ošetřena možnost, kdy funkce XMATCH() nenalezla hledanou hodnotu. Původní sekvence, kde test bal až na dalším řádku:

    ...

    Nález = WorksheetFunction.XMatch(Vstup, RnZdroj, 0) ' the program is interrupted here

    If IsError(Nález) Then ... byl pozdě, nemohl zachytit chybu za běhu

    ...

    Takže jednoduchá oprava:

    ...

    On Error Resume Next

    Nález = WorksheetFunction.XMatch(Vstup, RnZdroj, 0)

    If Nález=0 then ' ... cesta při nenalezení hledané hodnoty,

    Else

    ' cesta pokračování při nalezení hledané hodnoty

    End If

    ...

    Zdravím

    Stanislav

    Byla tato odpověď užitečná?

    Počet komentářů: 0 Žádné komentáře
  2. Anonymní
    2023-04-14T10:09:16+00:00

    Děkuji za odpověď,

    obrázek posílám znovu, je až vespodu.

    Doplňuji, že hledaný výraz je typu text (viz "Vstup" v obr.), prohledávaná oblast tabulky (RnZdroj) má difaultní "obecný" formát. Zde v prvním obrázku je vidět zdrojový list a v něm funkční pokus s tabulkovou XMATCH.

    S pozdravem, Stanislav

    Byla tato odpověď užitečná?

    Počet komentářů: 0 Žádné komentáře
  3. Miroslav Pragl 78,941 Reputační body Dobrovolný moderátor
    2023-04-14T05:12:40+00:00

    Stanislave,

    pis cesky, nebo do anglickych for.

    Obrazek s chybou chybi.

    Podivej se napr. na excel - VBA Xmatch function returning Run-Time Error 1004 - Stack Overflow - tazatel tam dost bojoval s datovymi typy

    MP

    Byla tato odpověď užitečná?

    Počet komentářů: 0 Žádné komentáře