excel - 如何从列中的日期列表中找到最早的日期?

标签 excel vba date

我采用了现有代码并尝试将其调整为我想做的事情,但我在这个过程中遇到了麻烦。

在我要处理的每个 Excel 表中,列大小(日期数)会有所不同。每个单元格肯定会包含格式为 dd/mm/yyyy 的日期。

运行时错误“13”
类型不匹配

知道如何解决这个问题吗?

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 = 3 To 7
    If ans = 0 Then ' You need to store the first valid value
        If Sheets(1).Cells(j, 9).Value >= t0 And Sheets(1).Cells(j, 9) <= t1 Then
            ans = Sheets(1).Cells(j, 9).Value
        End If
    Else
        If (Sheets(1).Cells(j, 9).Value >= t0 And Sheets(1).Cells(j, 9) <= t1) _
           And Sheets(1).Cells.Value < ans Then
            ans = Sheets(1).Cells(j, 9).Value
        End If
    End If
Next j

最佳答案

我能够使用 Sam 的评论来回答我自己的问题。这是一个可以解决我的问题的工作宏:

Range("I3:I7").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("I3"), _
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
    .SetRange Range("I3:I7")
    .Header = xlNo
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
Range("I3").Select
Selection.Copy
Sheets("Sheet2").Select
Range("AA1").Select
ActiveSheet.Paste

关于excel - 如何从列中的日期列表中找到最早的日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29987791/

相关文章:

sql-server - 可能会丢失从 double 到 int 的转换(Excel 到 SQL Server 导入)

python - 在 python 中将 excel 文件中选定的工作表打印为 pdf

Excel Office 加载项 API 工作表保护密码

excel - 如何在excel中获取所有过滤的数据行(带有隐藏列)?

java - SimpleDateFormat 宽容导致意外行为

ruby-on-rails - 如何验证(和处理)传递给.to_date的值是否有效或返回错误?

Excel 求解器忽略 VBA 中的约束

excel - 如何禁用另存为提示?

java - 如何在 Java 中对自定义 ArrayList 进行排序?

excel - 选择大小写错误输出和不匹配错误