【问题标题】:Finding Max Date from "Find" Range从“查找”范围查找最大日期
【发布时间】:2019-11-08 05:39:01
【问题描述】:

我有各种校准测试。我将所有不同的类型及其日期保存在一个工作表“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

我在阅读了您的 cmets 后实施了此更改。此代码运行,但它使用 11/22/4613 而不是预期的 11/7/2019 填充 Label27.Caption。

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

【问题讨论】:

  • 重要提示,Find 只返回一个单元格。 rangeCAL 不是您想要的所有单元格。
  • myArray 声明中没有括号(),这可能是错误的来源。
  • 每次循环都需要Redim PreservemyArray。此外,我认为您在值分配中需要Cell.Row 而不是rangeCal.Row。一旦你填充了数组,你就可以使用它了。可能更容易将整个数据集填充到数组中,然后循环数组以首先检查测试类型,然后如果日期晚于您将存储在 currentMaxDate 变量中的那个日期。在循环范围结束时,您的最大日期保存在 currentMaxDate 变量中。其他信息可能在其他变量中,或者在一维数组中。
  • 如果日期已按照您在工作表上的格式设置,则使用单元格的 .Text 属性。如果当前单元格的值更大,您需要做的就是循环范围并替换变量的值。根本不需要数组。
  • 还建议您完全限定空字符串的测试并实际使用 vbNullString 而不是 "" If Cell.Text&lt;&gt;vbNullString

标签: excel vba


【解决方案1】:
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")

【讨论】:

    【解决方案2】:

    这是我根据 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
    

    【讨论】:

      猜你喜欢
      • 2015-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-22
      • 2016-10-08
      • 1970-01-01
      相关资源
      最近更新 更多