我已经在互联网上搜寻了此问题的解决方案。
我有一些应该匹配的列表,需要进行相互比较。我需要比较每行中的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
答案 0 :(得分:1)
我已经注释了您的代码,希望我的注释可以帮助您改进它。
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