Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba MS Excel:在混合内容行中查找最早日期_Vba_Excel_Excel Formula - Fatal编程技术网

Vba MS Excel:在混合内容行中查找最早日期

Vba MS Excel:在混合内容行中查找最早日期,vba,excel,excel-formula,Vba,Excel,Excel Formula,有人知道如何在Microsoft Excel中获取行的最早日期吗。每行上没有可预测的列数,并且需要忽略日期以外的值。可以使用Excel公式或VBA完成 有人有什么建议吗 现在我正在使用这个快速而肮脏的VBA函数,但是当我加载新的输入数据(大约200行乘100列)时,出现了一个消息框,说Excel没有足够的资源来处理我的更改 ' returns smallest date on row Public Function getSmallestDateFromRow(r As Integer, she

有人知道如何在Microsoft Excel中获取行的最早日期吗。每行上没有可预测的列数,并且需要忽略日期以外的值。可以使用Excel公式或VBA完成

有人有什么建议吗

现在我正在使用这个快速而肮脏的VBA函数,但是当我加载新的输入数据(大约200行乘100列)时,出现了一个消息框,说Excel没有足够的资源来处理我的更改

' returns smallest date on row
Public Function getSmallestDateFromRow(r As Integer, sheetName As String) As Date
    Dim toReturn As Date
    Dim rng As Range
    Dim scanToColumn As Integer
    Dim c As Integer
    Dim tmp As Variant

    Set rng = Sheets(sheetName).Cells.Find("*", [a1], , , xlByColumns, xlPrevious) 'is this inefficient?
    scanToColumn = rng.Column
    toReturn = #12/12/2100#

    For c = 1 To scanToColumn
        tmp = Sheets(sheetName).Cells(r, c).Value
        If (IsDate(tmp)) Then
            If (toReturn = Null) Then
                toReturn = tmp
            ElseIf (toReturn > tmp) Then
                toReturn = tmp
            End If
        End If
    Next c

    If (toReturn = #12/12/2100#) Then
       toReturn = 0
    End If

    getSmallestDateFromRow = toReturn
End Function

您必须记住,Excel(以及许多其他Microsoft产品)将日期存储为浮点数:

  • 整数部分是自1900年1月1日起经过的天数(例如:1相当于1/1/1900)
  • 小数部分是一天过去的“分数”(例如:0.5相当于下午12:00)
关于如何找到最小或最大日期值的问题由于行中可能有许多其他数字而变得模糊。因此,您必须首先定义日期的“有效等级”。在这之后,一个简单的“数组公式”就可以做到:

示例。假设您的有效范围在2000年1月1日至2100年12月31日之间。那么您的有效“数字等级”是:

  • 2000年1月1日相当于36526
  • 2100年12月31日相当于73415
现在,您可以编写函数来跟踪此范围内的最小日期值:

function trackMinimum(rowRange as range) as date
    on error resume next
    dim j as integer, minValue as date
    dim t0 as double, t1 as double
    dim ans as date

    t0 = cdbl(dateserial(2000,1,1))
    t1 = cdbl(dateserial(2100,12,31))
    ans = 0
    for j = 1 to rowRange.columns.count
        if ans = 0 then ' You need to store the first valid value
            if rowRange.cells(1,j).value >= t0 and rowRange.cells(1,j) <= t1 then
                ans = rowRange.cells(1,j).value
            end if
        else
            if (rowRange.cells(1,j).value >= t0 and rowRange.cells(1,j) <= t1) _ 
               and rowRange.cells.value < ans then
                ans = rowRange.cells(1,j).value
            end if
        end if
    next j
    trackMinimum = ans
end function
函数跟踪最小值(行范围作为范围)作为日期
出错时继续下一步
dim j为整数,minValue为日期
尺寸t0为双精度,t1为双精度
日期
t0=cdbl(日期序列(2000,1,1))
t1=cdbl(日期序列(2100,12,31))
ans=0
对于j=1到rowRange.columns.count
如果ans=0,则需要存储第一个有效值

如果rowRange.cells(1,j).value>=t0和rowRange.cells(1,j)=t0和rowRange.cells(1,j),这是工作表函数/UDF吗?或者被另一个宏调用的函数?这不是数组公式。。。没必要那样。当然,此函数需要在每行上“复制”。或者其他函数或子函数可以对给定范围内的每一行计算此公式