如何使用“ WorksheetFunction.Match”作为具有可变范围的数组

问题描述 投票:1回答:4

我正在尝试将.Match函数实现为VBA中具有可变范围的数组,以查找特定范围内的第一个非零单元格。

但是,目前我只收到运行时错误1004。

Set myRange = Worksheets("Portf_Mod").Range("AB368:CY368") 
With Application.WorksheetFunction
    Date_col = .Match(True, [myRange <> 0], 0)
End With
excel vba excel-vba worksheet-function
4个回答
2
投票

因为您正在使用VBA,而不是使用Match,您可以使用API​​编写更明确和可维护的代码:

Function FirstNonZeroCell(rng As Range) As Range
    Dim cell As Range
    For Each cell In rng.Cells
        If cell.Value <> 0 Then
            Set FirstNonZeroCell = cell
            Exit Function
        End If
    Next
End Function

或者如果您想要一个更紧凑的版本(但我认为可读性较差:

Function FirstNonZeroCell(rng As Range) As Range
    For Each FirstNonZeroCell In rng.Cells
        If FirstNonZeroCell.Value <> 0 Then Exit Function
    Next
    Set FirstNonZeroCell = Nothing
End Function

这里是使用示例:

Sub test()
    Dim res As Range
    Set res = FirstNonZeroCell(Range("A1:Z100"))
    If Not res Is Nothing Then
        MsgBox "Found value " & res.Value & " in cell " & res.Address & "."
    Else
        MsgBox "No match has been found!"
    End If
End Sub

1
投票

认为数组元素会抛出您的方法,所以这里是替代方法。

尚不清楚您是否想要第一个非零值或其位置,因此可以同时覆盖这两个值。

Date_col = Evaluate("MATCH(TRUE," & myRange.Address & "<>0,0)") 'returns position
Date_col = Evaluate("INDEX(" & myRange.Address & ",MATCH(TRUE," & myRange.Address & "<>0,0))") 'returns value

0
投票

这是我第一次在VBA中遇到“数组公式”。这没错,但了解/解决问题不是很明显。在我的机器上[该区域的所有单元格都为空,它给出了错误[myRange <> 0] = Error 2092,然后在Match函数中给出了另一个错误...

数组公式仅在公式工作表中很棒,但是我认为您应该在VBA中避免使用它们。您可以在VBA中使用循环,因此,请不要犹豫使用它们!编写软件时,关键是要尽可能地明确(因此您稍后将理解!)。

我的建议是:

Option Explicit

Function FindDateColumnInRange(ByVal RangeToLookIn As Range) As Long

If RangeToLookIn.Rows.Count <> 1 Then
    'The range you're looking in has more than one row
    'what should you do in this case? Look only in the first row?

Else

    Dim i As Long
    'The range has only one row
    For i = 0 To RangeToLookIn.Columns.Count - 1
        If RangeToLookIn.Item(1, i).Value <> 0 Then
            'should you verifiy that the value is a date value?
            FindDateColumnInRange = RangeToLookIn.Item(1, i).Column
            Exit Function
        End If
    Next
End If

'the range didn't have a value different from 0
FindDateColumnInRange = 0

End Function

实际上,您会得到:

Sub Test()

Dim MyRange As Range
Set MyRange = Worksheets("Portf_Mod").Range("AB368:CY368")
Dim date_col As Integer

date_col = FindDateColumnInRange(MyRange)

If date_col = 0 Then
    'There was no date in your range
End If

End Sub

是的,它比SJR建议要长得多,但是,它涵盖了所有异常,并且您可以控制如何传递多维数组(首先遍历行,然后遍历列,或者反过来遍历)。


0
投票

我不会为此使用Match函数...

Set myRange = Worksheets("Portf_Mod").Range("AB368:CY368") 
On Error Resume Next
Date_col = myRange.Find("*", LookIn:=xlValues, SearchOrder:=xlByColumns).Column
On Error GoTo 0
© www.soinside.com 2019 - 2024. All rights reserved.