Excel 如何使用;工作表function.Match“;作为具有可变范围的数组

Excel 如何使用;工作表function.Match“;作为具有可变范围的数组,excel,vba,worksheet-function,Excel,Vba,Worksheet Function,我试图实现一个.Match函数,作为VBA中具有可变范围的数组,以查找特定范围内的第一个非零单元格 但是,目前我只收到运行时错误1004 Set myRange = Worksheets("Portf_Mod").Range("AB368:CY368") With Application.WorksheetFunction Date_col = .Match(True, [myRange <> 0], 0) End With Set myRange=工作表(“Portf_

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

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

Set myRange = Worksheets("Portf_Mod").Range("AB368:CY368") 
With Application.WorksheetFunction
    Date_col = .Match(True, [myRange <> 0], 0)
End With
Set myRange=工作表(“Portf_Mod”).范围(“AB368:CY368”)
使用Application.WorksheetFunction
日期列=.Match(True,[myRange 0],0)
以

认为数组元素抛弃了您的方法,因此这里有一个替代方法

不清楚您是想要第一个非零值还是它的位置,所以这两个都包括在内

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
Date\u col=Evaluate(“MATCH(TRUE,&myRange.Address&“0,0)”)返回位置
Date\u col=Evaluate(“索引(&myRange.Address&”,MATCH(TRUE,&myRange.Address&“0,0)))返回值

当您使用VBA时,您可以使用API来生成更明确和可维护的代码,而不是使用
Match

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

我不会为此使用匹配函数

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

这是我第一次在VBA中遇到“数组公式”。这没有错,但理解/故障排除不太明显。在我的机器上[该区域有所有空单元格,它给出一个错误
[myRange 0]=error 2092
,然后在匹配函数中,得到另一个错误

数组公式仅在公式工作表中很好,但我觉得您应该避免在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

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

看一看,您可能会调整此答案以满足您的需要。我认为问题来自您的=匹配利用率。您在多个列上使用它。如果您在公式上尝试相同的方法,您将出现“#NA”错误。
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
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