获取最小日期值 VBA

huangapple go评论105阅读模式
英文:

Get Minimum Date value VBA

问题

I am trying to get the minimum value date if the column next to it has the word "before". I tried the following formula, but I am getting a value of 0. Am I missing something here?

  1. Sub test()
  2. Dim i As Long
  3. Dim j As Long
  4. Dim LRow As Long
  5. Dim b As Long
  6. Dim a As String
  7. a = "before"
  8. j = 0
  9. LRow = Range("A" & Rows.Count).End(xlUp).Row
  10. Dim MyArr() As Variant
  11. ReDim MyArr(1 To LRow) As Variant
  12. For i = 1 To LRow
  13. If VBA.InStr(Range("B" & i).Value, a) > 0 Then
  14. j = j + 1
  15. MyArr(j) = Range("A" & i).Value
  16. End If
  17. Next i
  18. ReDim Preserve MyArr(1 To j)
  19. Range("D1").Value = Application.WorksheetFunction.Min(MyArr())
  20. End Sub
英文:

获取最小日期值 VBAI am trying to get the minimum value date if the column next to it has the word "before". I tried the following formula, but I am getting a value of 0. Am I missing something here?

  1. Sub test()
  2. Dim i As Long
  3. Dim j As Long
  4. Dim LRow As Long
  5. Dim b As Long
  6. Dim a As String
  7. a = "before"
  8. j = 0
  9. LRow = Range("A" & Rows.Count).End(xlUp).Row
  10. Dim MyArr() As Variant
  11. ReDim MyArr(1 To LRow) As Variant
  12. For i = 1 To LRow
  13. If VBA.InStr(Range("B" & i).Value, a) > 0 Then
  14. j = j + 1
  15. MyArr(j) = Range("A" & i).Value
  16. End If
  17. Next i
  18. ReDim Preserve MyArr(1 To j)
  19. Range("D1").Value = Application.WorksheetFunction.Min(MyArr())
  20. End Sub

答案1

得分: 3

Use MINIFS:

  1. =MINIFS(A:A, B:B, "*before*")

With VBA:

  1. Sub test()
  2. Range("D1").Value = WorksheetFunction.MinIfs(Range("A:A"), Range("B:B"), "*before*")
  3. Range("D1").NumberFormat = "m/d/yyyy"
  4. End Sub
英文:

Use MINIFS:

  1. =MINIFS(A:A, B:B, "*before*")

With VBA:

  1. Sub test()
  2. Range("D1").Value = WorksheetFunction.MinIfs(Range("A:A"), Range("B:B"), "*before*")
  3. Range("D1").NumberFormat = "m/d/yyyy"
  4. End Sub

huangapple
  • 本文由 发表于 2023年4月19日 21:41:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定