簡體   English   中英

比較多個條件時,VBA中的評估/ MATCH功能不起作用

[英]Evaluate/MATCH function in VBA not working when comparing multiple criteria

我已經在互聯網上搜尋了此問題的解決方案。

我有應該匹配的列表,需要相互比較。 我需要比較每行中的5個左右不同的變量,然后使用MATCH函數來識別第一個匹配的行,然后將其刪除。 然后,我將遍歷該列表,直到還有剩余的未被刪除的條目。 我需要刪除的原因是,每個列表中可能有多個匹配項,但是如果一個列表中有3個匹配項,而另一個列表中有4個匹配項,則需要標識第4個(額外)條目。

請批評我下面的代碼,我還沒有創建循環,因為我認為一旦獲得了MATCH函數即可正常工作,這將很容易。 標准的CSE公式適用於工作表,但我需要VBA才能實現循環播放功能。 謝謝。

我嘗試使用msgbox驗證RowDelete的值,並返回運行時錯誤13:“類型不匹配”。 我還嘗試使用WATCH窗口查看傳遞的結果,但是實際的公式本身似乎不起作用。

編輯:此代碼返回運行時錯誤'13':類型不匹配。 我無法解決。 我只想知道如何將公式傳遞給可以使用的結果(在這種情況下,第一個結果是第62行)。 之后,我將能夠自己做所有事情。

Sub DeleteMatches2()

    Dim Ws As Worksheet
    Dim Direction As String
    Dim OrderType As String
    Dim Amount As String
    Dim CCY As String
    Dim Rate As String

    Dim RowCt As Long
    Dim Formula As Integer

    Dim iRow As Long

    Dim colNum As Integer

    Dim RowDelete As Long

    Set Ws = Sheets("KOOLTRA RAW")

    With Ws
        RowCt = .Cells(.Rows.Count, 11).End(xlUp).Row - 1

        For iRow = 2 To RowCt

            Direction = .Cells(iRow, "K").Value
            OrderType = .Cells(iRow, "L").Value
            Amount = .Cells(iRow, "M").Value
            CCY = .Cells(iRow, "N").Value
            Rate = .Cells(iRow, "P").Value

            Formula = Evaluate("MATCH(1,(""" & OrderType & """ = B:B)*(""" & Direction & """ = C:C)*(""" & Amount & """ = D:D)*(""" & CCY & """ = E:E)*(""" & Rate & """ = H:H),0)")
            MsgBox Formula

            Exit For

        Next iRow
    End With
End Sub

我已經對您的代碼進行了注釋,希望我的注釋可以幫助您改進它。

Sub DeleteMatches()

    Dim Ws As Worksheet
    Dim Direction As Variant
    Dim OrderType As Variant
    Dim Amount As Variant
    Dim CCY As Variant
    Dim Rate As Variant
    Dim RowCt As Long                       ' rows and columns should be of Long type
    Dim Formula As Variant
    Dim iRow As Long
    Dim colNum As Long
    Dim RowDelete As Long

    Set Ws = Sheets("Example")                      ' don't "select" anything

    With Ws
        ' creating variable for toral rows to cycle through:-
        ' you aren't "creating" a variable.
        ' RowCt is the variable and you are assigning a value to it.
        RowCt = .Cells(.Rows.Count, 11).End(xlUp).Row - 1

        For iRow = 2 To RowCt                       ' loop through all rows
            ' assigning a Range to a Variant (Direction etc) assigns the
            ' Range object to the variant. I have modified the code
            ' to assign the specified cell's value to the variant.
            ' A Variant can be anything. It would be better if you
            ' would declare your variables as String or Double or Integer or Long.
            Direction = .Cells(iRow, "K").Value     ' Range("K" & iRow)
            OrderType = .Cells(iRow, "L").Value     ' Range("L" & iRow)
            Amount = .Cells(iRow, "M").Value        ' Range("M" & iRow)
            CCY = .Cells(iRow, "N").Value           ' Range("N" & iRow)
            Rate = .Cells(iRow, "P").Value          ' Range("P" & iRow)

            ' Formula is a property of the Range object.
            ' use like .Cells(iRow, "X").Formula = "MATCH(1,((B:B="" & OrderType & "") ......
'            Formula = "MATCH(1,((B:B="" & OrderType & "")*(C:C="" & Direction & "")*(D:D="" & Amount & "")*(E:E="" & CCY& "")*(H:H="" & Rate & "")),0)"
            ' To set a formula, you need to enter the = sign, like
            ' .Cells(iRow, "X").Formula = " = MATCH(1 ...
            ' it is best that you test the formula on the worksheet
            ' before attempting to let VBA write it to a cell.
            ' Your above formula looks like nothing Excel would be able to execute.

            ' Please read up on how to use the Evaluate function.
            ' It can't do what you appear to expect it to do.
'            RowDelete = Evaluate(Formula)

            MsgBox RowDelete

            'colNum = Application.Match(1,((B1:B2=OrderType)*(C1:C2=Direction)*(D:D=Amount)*(E:E=CCY)*(H:H=Rate)),0)
            ' I think it is the better idea to let VBA execute the MATCH function
            ' rather than writing the formula to the worksheet.
            ' However, your "code" has no similarity with what MATCH can do.
            ' Read up on how to use the the MATCH function correctly.
            ' When executed by VBA it needs the same syntax as when called from the worksheet.


            'Formula = "MATCH(1,((B:B=OrderType)*(C:C=Direction)*(D:D=Amount)*(E:E=CCY)*(H:H=Rate)),0)"
            'Formula = "MATCH(1,((B:B=L2)*(C:C=K2)*(D:D=M2)*(E:E=N2)*(H:H=P2)),0)"
            'colNum = Worksheets("Example").Evaluate("MATCH(1,((B:B=OrderType)*(C:C=Direction)*(D:D=Amount)*(E:E=CCY)*(H:H=Rate)),0)")

            Exit For            ' stop the loop here during testing
                                ' remove this stop after your code works
        Next iRow
    End With
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM