我采用了现有代码并尝试将其调整为我想做的事情,但我在这个过程中遇到了麻烦。
在我要处理的每个 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/