从“查找”范围中查找最大日期

时间:2019-11-07 20:38:35

标签: excel vba

我有各种校准测试。我将所有不同类型及其日期保留在一个工作表“ wsCAL”中

我想用一种特定类型的测试的最新日期填充用户窗体,该日期存储在wsCAL的C列中。

从理论上讲,我希望VBA转到wsCAL,在C列中查找并找到一种测试类型的所有实例,在这些实例的B列中找到最新日期(或MAX),然后用该日期填充用户表单。

我尝试使用rangeCAL = .Find()函数在C列中查找测试类型的所有实例。这部分工作正常。但是,我尝试使用的application.worksheetfunction.Max(rangeCAL)失败。我猜是因为该应用程序功能仅适用于工作表范围,而不适用于Find()范围。我正在努力获取rangeCAL单元格,制作一个数组,然后找到这些单元格的最新日期(MAX)。

Private Sub UserForm_Initialize() 'Upon opening the userform

    Set wb = ThisWorkbook
    Set wsHOME = wb.Worksheets("Home")
    Set wsCAL = wb.Worksheets("Bottle Calibrations")
    Set wsC1T1 = wb.Worksheets("C1T1")

    'Last Calibration Date      
        Label27.Caption = vbNullString
        With wsCAL
        Dim Cell As Range
        Dim myArray As Date
        Dim i As Integer
        Dim rangeCAL As Range
        Dim rangeDateCAL As Date

        i = 0
        Set rangeCAL = Range("C:C").Find(What:=tank, LookAt:=xlWhole)
            If Not rangeCAL Is Nothing Then
                For Each Cell In rangeCAL
                    myArray(i) = .Range(rangeCAL.Row, "A").Value
                    i = i + 1
                Next
            Else
                MsgBox "Error: no previous Calibration dates loaded."
            End If
            rangeDateCAL = Application.WorksheetFunction.Max(myArray)
            rangeDateCAL = Format(rangeDateCAL, "yymmdd")

    End With
    Label27.Caption = rangeDateCAL

我不断收到错误消息

  

“期望数组”

我刚下线:

myArray(i) = .Range(rangeCAL.Row, "B").Value

更新:

Label27.Caption = vbNullString
With wsCAL
    Dim Cell As Range
    Dim myArray(1 To 5) As Date
    Dim i As Long
    Dim temp As Date
    Dim rangeCAL As Range
    Dim rangeDateCAL As Date

    i = 1
    Set rangeCAL = wsCAL.Range("C1", Range("C1").End(xlDown).Address)
        For Each Cell In rangeCAL
            If Cell <> "" Then
                If Cell.Value = tank Then
                    temp = wsCAL.Cells(Cell.Row, "B").Value
                    myArray(i) = temp
                    i = i + 1

                End If
            End If
        Next

    rangeDateCAL = Application.WorksheetFunction.Max(myArray)
    rangeDateCAL = Format(rangeDateCAL, "yymmdd")

End With

    Label27.Caption = rangeDateCAL

阅读您的评论后,我实施了此更改。该代码可以运行,但是它将11/22/4613填充为Label27.Caption,而不是预期的11/7/2019。

我假设日期值在MAX函数步骤中已更改,但是我不确定我还能更改什么。

2 个答案:

答案 0 :(得分:2)

For Each Cell In rangeCAL
    If Cell.Text <> vbNullString Then
        If Cell.Text = tank Then 'assuming tank is declared a string
            If tempDate < wsCAL.Cells(Cell.Row, "B").Value Then
                tempDate = wsCAL.Cells(Cell.Row, "B").Value
            End If
        End If
    End If
Next

Label27.Caption = Format(tempDate, "yymmdd")

答案 1 :(得分:0)

这是我根据SmileyFTW的建议实施的。比预期的要简单得多。虽然按预期工作。谢谢SmileyFTW,以及其他在帮助下发表评论的人。

Label27.Caption = vbNullString
With wsCAL
    Dim Cell As Range
    Dim i As Date
    Dim temp As Date
    Dim rangeCAL As Range

    temp = 0
    Set rangeCAL = wsCAL.Range("C1", Range("C1").End(xlDown).Address)
        For Each Cell In rangeCAL
            If Cell <> vbNullString Then
                If Cell.Value = tank Then
                    i = wsCAL.Cells(Cell.Row, "B").Text
                    If i > temp Then
                        temp = i
                    End If

                End If
            End If
        Next
End With
Label27.Caption = temp