vba,excel:返回此字符串数组中的最早日期值

时间:2012-03-07 15:34:42

标签: excel vba

我遇到了编码方面的一些问题,不知道在哪里解决这个问题:

我希望BronzeDate返回我的数组中找到的最早日期。

这是打印出来的例子:

   Possible Bronze Date : 6/6/2011
   Possible Bronze Date : 10/5/2011
   Possible Bronze Date : 10/5/2011
   Possible Bronze Date : 12/5/2011
   Possible Bronze Date : 10/5/2011
   Possible Bronze Date : 6/6/2011
   Possible Bronze Date : 11/5/2011
   Possible Bronze Date : 1/16/2012
   Possible Bronze Date : 6/6/2011
   Possible Bronze Date : 
   Possible Bronze Date : 
   Possible Bronze Date : 
   Possible Bronze Date : 
   Possible Bronze Date : 
   Possible Bronze Date : 
   Possible Bronze Date : 
   Possible Bronze Date : 
   Possible Bronze Date : 
   Possible Bronze Date : 
  [BRONZE] 0

CODING:

    Dim Bronze(1 To 100) As String
...
        Dim c As Integer
        c = 1
        Do Until c = 20
            Debug.Print "       Possible Bronze Date : " & Bronze(c)
            c = c + 1
        Loop

        '   EXTRACT THE MIN VALUES
        Dim BronzeDate As String
        BronzeDate = Application.WorksheetFunction.Min(Bronze)

        '   PRINT OUT VALUE FOR EACH DATES
        Debug.Print "      [BRONZE] " & BronzeDate

2 个答案:

答案 0 :(得分:2)

我相信你别无选择,只能单独转换字符串日期。

我已测试此代码以输出正确的日期。注意我已将数组中的中间日期日期切换为小端,因此我的Excel版本将正确处理它们。

Sub ExtractMinDate()

  Dim Bronze() As Variant
  Dim DateCrnt As Date
  Dim DateMin As Date
  Dim InxBronze As Long

  Bronze = Array("6/6/2011", "5/10/2011", "5/10/2011", "1/12/2011", "5/10/2011", _
                 "6/6/2011", "5/11/2011", "16/1/2012", "5/6/2011")

  DateMin = 0

  For InxBronze = LBound(Bronze) To UBound(Bronze)
    If IsDate(Bronze(InxBronze)) Then
      DateCrnt = Bronze(InxBronze)
      If DateMin = 0 Or DateMin > DateCrnt Then
        DateMin = DateCrnt
      End If
    End If
  Next

  Debug.Print DateMin

End Sub

答案 1 :(得分:0)

我认为问题是你的Bronze数组是一个字符串数组。

如果您将青铜阵列更改为Variant类型,则应解决您的问题。