2012-03-07 96 views
0

我在與編碼一些問題,不知道從哪裏解決這個在返回的最早日期值:VBA,EXCEL:此數組字符串

我想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 

編碼:

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 

回答

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