IF函数中的Excel VBA索引/匹配

时间:2016-10-04 01:24:44

标签: excel vba excel-vba

我试图设置循环遍历M列中每个单元格的VBA代码,如果单元格包含数字,则返回Column L的值,如果Column L没有&#39,则返回索引/匹配函数; t包含一个数字。然后,如果索引/匹配没有找到它正在寻找的内容,它将通过另一个vlookup。我遇到了第三部分语法(最后的vlookup)的问题。我不确定它是否应该是另一个Else声明或if声明或ISerror或完全不同的东西。现在我把它设置为第二个if / else。我还想知道我是否会遇到问题,因为索引/匹配函数有文本作为输入,应该返回一个数字。对此有任何建议/意见,非常感谢。以下是我到目前为止的情况。

Sub Ranking_2()

Dim cell As Range, rng As Range
Set rng = Range("L2:L120")

For Each cell In rng
    If WorksheetFunction.IsNumber(cell.Value) Then
        cell.Offset(0, 1).Value = cell.Value
    Else: cell.Offset(0, 1).Value = WorksheetFunction.Index(ThisWorkbook.Sheets(1).Range("K:K"), WorksheetFunction.Match(cell.Offset(0, 1) & cell.Offset(0, 5), ThisWorkbook.Sheets(1).Range("A:A") & ThisWorkbook.Sheets(1).Range("H:H"), 0))
        If:cell.Offset(0,1).Value= WorksheetFunction.IsError(
        Else: cell.Offset(0, 1).Value = WorksheetFunction.VLookup(cell.Offset(0, -11), ThisWorkbook.Sheets(2).Range("A1:D136"), 3, 0)
End If
Next
End Sub

1 个答案:

答案 0 :(得分:0)

您可能希望对代码采用这些更改

Option Explicit

Sub Ranking_2()
    Dim cell As Range
    Dim lookUp1Sht As Worksheet
    Dim lookUp2Rng As Range
    Dim val1 As Variant

    Set lookUp1Sht = ThisWorkbook.Worksheets("LookUp1Sht") '<--| set the worksheet you're making the first lookup
    Set lookUp2Rng = ThisWorkbook.Worksheets("LookUp2Sht").Range("A1:C136") '<--| since you're this range returning column "C" value it suffices limiting it to column "C"

    For Each cell In Range("L2:L120").SpecialCells(xlCellTypeConstants) '<--| limit looping through wanted range not blank cells only
        With cell '<--| reference current cell
            Select Case True
                Case IsNumeric(.Value) '<--| if current cell value can be evaluated as "number"... 
                    .Offset(0, 1).Value = CDbl(.Value)
                Case Not IsError(LookUp1(lookUp1Sht, .Offset(0, 1).Value, .Offset(0, 5).Value, val1)) '<-- if "first" lookup doesn't return an "error"...
                    .Offset(0, 1).Value = val1 '<--| then write the 3rd argument passed from LookUp1() function
                Case Else '<-- if all preceeding "cases" failed...
                    .Offset(0, 1).Value = Application.VLookup(.Offset(0, -11), lookUp2Rng, 3, 0) '<-- write "second" lookup return value
            End Select
        End With
    Next
End Sub

Function LookUp1(sht As Worksheet, val1 As Variant, val2 As Variant, val As Variant) As Variant
    Dim f As Range
    Dim firstAddress As String

    With sht '<--| reference passed worksheet
        Set f = .Range("A:A").Find(what:=val1, LookIn:=xlValues, lookat:=xlWhole) '<-- look for first passed value in its column "A"
        If Not f Is Nothing Then '<--| if found...
            firstAddress = f.Address '<--| store found cell address to stop subsequent FindNext() loop upon wrapping back to it
            Do '<--| loop
                If f.Offset(, 7).Value = val2 Then '<--| if corresponding value in column "H" matches val2...
                    val = .Cells(f.row, "K") '<-- set 3rd argument to value in column "K" corresponding to the "double" match
                    Exit Function '<--| exit function
                End If
                Set f = .Range("A:A").FindNext(f) '<-- go on looking for val1 in column "A"
            Loop While f.Address <> firstAddress '<-- stop looping upon wrapping back on first cell found
        End If
    End With
    LookUp1 = CVErr(xlErrValue) '<-- if no "double" match occurred then return "#VALUE!" error
End Function

请注意:

  • 改变&#34; LookUp1Sht&#34;和&#34; LookUp2Sht&#34;到您的实际工作表名称

  • MatchLookUp Application函数可以处理可能的错误,而无需暂停宏并只返回错误值

    这个我只用在.Offset(0, 1).Value = Application.VLookup(.Offset(0, -11)...中,所以如果&#34;最后一次机会查找&#34;曾经回复过你在.Offset(0,1)单元格

  • 中写的错误
  • 使用SpecialCells()方法返回您调用它的范围的过滤组:例如,使用xlCellTypeConstants作为其Type参数,您不会回来仅限空单元

  • 使用IsNumeric()函数代替[WorksheetFunction.IsNumber() [(https://msdn.microsoft.com/en-us/library/office/ff840818(v=office.15).aspx),因为前者会识别字符串&#34; 5&#34;作为一个数字,而后者不会

相关问题