0

5 つの日付のうち最も古いものを見つけるために、次のコードを作成しました。期待どおりに動作しますが、5 つの日付を比較するよりエレガントな方法があるかどうか知りたいです。誰にもアイデアはありますか?

Dim sTemp
sTemp = ""

If IsDate(dtOne) Then
    If IsDate(dtTwo) Then
        If CDate(dtOne) < CDate(dtTwo) Then
            sTemp = dtOne
        Else
            sTemp = dtTwo
        End If
    Else
        sTemp = dtOne
    End If
ElseIf IsDate(dtTwo) Then
    sTemp = dtTwo
End If

If IsDate(dtThree) Then
    If IsDate(sTemp) Then
        If CDate(dtThree) < CDate(sTemp) Then
            sTemp = dtThree
        End If
    Else
        sTemp = dtThree
    End If
End If

If IsDate(dtFour) Then
    If IsDate(sTemp) Then
        If CDate(dtFour) < CDate(sTemp) Then
            sTemp = dtFour
        End If
    Else
        sTemp = dtFour
    End If
End If

If IsDate(dtFive) Then
    If IsDate(sTemp) Then
        If CDate(dtFive) < CDate(sTemp) Then
            sTemp = dtFive
        End If
    Else
        sTemp = dtFive
    End If
End If
4

3 に答える 3

1

このようなものはどうですか、各比較を行い、渡された変数がどちらも日付でない場合は、sTemp を "" に復元します。

Dim sTemp
sTemp = ""
sTemp = OldestDate(dtOne, dtTwo)
sTemp = OldestDate(dtThree, sTemp)
sTemp = OldestDate(dtfour, sTemp)
sTemp = OldestDate(dtfive, sTemp)

Function OldestDate(dtOne, dtTwo)
    If IsDate(dtOne) Then
        If IsDate(dtTwo) Then
            If CDate(dtOne) < CDate(dtTwo) Then
                OldestDate = dtOne
            Else
                OldestDate = dtTwo
            End If
        Else
            OldestDate = dtOne
        End If
    ElseIf IsDate(dtTwo) Then
        OldestDate = dtTwo
    Else
         OldestDate = ""
    End If
End Function
于 2012-10-26T23:41:53.873 に答える
0

あなたは洗練されたアプローチを求め、次に ArrayList を使用し、それを埋めて並べ替えました。概念実証コードの下では、有効な日付がないなどの例外は処理されません。

' Create a new arraylist
Set arrayList = createobject("System.Collections.ArrayList")

' Loop through all dates
For each d in array("23-5-2007", "28-6-2010", "16-9-2001", "32-12-2000")
     ' See if the date is valid. If true, convert it to a date and add it to the list
    If isDate(d) Then arrayList.Add cDate(d)
Next

' Sort the list from oldest to newest date
arrayList.Sort

' Get the first item, it will be the oldest date (16-9-2001)
OldestDate = arrayList.Item(0)
于 2012-10-29T15:33:58.037 に答える
0

私は次のようなものを使用します:

sTemp = GetOldestOf(sTemp, dtOne)   ' this instruction makes "dtOne" the current
                                    ' current oldest date, b/c "sTemp" is auto-
                                    ' initialized as Empty
sTemp = GetOldestOf(sTemp, dtTwo)
sTemp = GetOldestOf(sTemp, dtThree)
sTemp = GetOldestOf(sTemp, dtFour)
sTemp = GetOldestOf(sTemp, dtFive)

If isEmpty(sTemp) Then
  WScript.Echo "No valid date found!"
Else
  WScript.Echo "Oldest date is: " & sTemp
End If

' pre-condition: d1 is either Empty or the current oldest date
Function GetOldestOf(ByVal d1, ByVal d2)
  GetOldestOf = d1          ' make d1 the default return value
  If IsDate(d2) Then        ' d2 is a valid date
    d2 = CDate(d2)
    If IsEmpty(d1) Then     ' if d1 is empty, d2 is automatically oldest
      GetOldestOf = d2
    ElseIf d1 > d2 Then     ' otherwise check if d2 is older
      GetOldestOf = d2
    End If
  End If
End Function

GetOldestOf()引数が有効な日付で、最初の引数がEmpty2 番目の引数またはそれよりも新しい場合、2 番目の引数を (日付に変換して) 返します。それ以外の場合、関数は (変更されていない) 最初の引数を返します。これは、定義により、Emptyまたは現在の最も古い日付です。

于 2012-10-27T12:21:36.083 に答える