0

当社の会計ソフトウェアは、日付を 07262013 としてテキストとしてエクスポートします。このテキスト文字列を日付形式に変換するには、通常、数式を入力します

=IF(A2>10000000,DATE(VALUE(RIGHT(A2,4)),VALUE(LEFT(A2,2)),VALUE(MID(A2,3,2))),
DATE(VALUE(RIGHT(A2,4)),VALUE(LEFT(A2,1)),VALUE(MID(A2,2,2)))) 

データをエクスポートするたびに。=convert_text(text)同じ関数を完成させるためにカスタム関数を書きたいです。

私が思いついた

Function Convert_Date(text)
 If text > 10000000 Then
    Convert_Date = Application.Date(Application.Value(Application.Right(text, 4)), Application.Value(Application.Left(text, 2)), Application.Value(Application.Mid(text, 3, 2)))
 Else
    Convert_Date = Application.Date(Application.Value(Application.Right(text, 4)), Application.Value(Application.Left(text, 1)), Application.Value(Application.Mid(text, 2, 2)))
End Function

よろしくお願いします!リー

4

3 に答える 3

1

あなたは次のものを探しています:

Function Convert_Date(text)
' assuming text is of the form mmddyyyy
' or mddyyyy
Dim year As Integer, month As Integer, day As Integer, L As Integer

L = Len(text)
year = Val(Right(text, 4))
day= Val(Mid(text, L - 5, 2))
If L = 7 Then month= Left(text, 1) Else month= Left(text, 2)

' >>>>> the next line is there for debugging; 
' >>>>> take it out once you are happy with the result
MsgBox "year: " & year & "; month: " & month & "; day: " & day

Convert_Date = DateSerial(year, month, day)

End Function

これは「日付シリアル番号」を返します。次に、必要な日付形式でセルをフォーマットすると、準備完了です。年、月、日の明示的な抽出を使用すると、コードがはるかに読みやすくなります。

注 - より一般的にしたい場合は、フォーマットをオプションの 2 番目の文字列として指定できます。たとえばddmmyyyy、これらの文字を検索し、それを使用して日付を適切に抽出できます。

Function Convert_Date(text, Optional formatString)
' assuming text is of the form mmddyyyy
' alternatively specify the format with the second parameter

Dim L As Integer, ii As Integer
Dim yearString As String, monthString As String, dayString As String

If IsMissing(formatString) Then formatString = "ddmmyyyy"

L = Len(text)

For ii = 1 To L
  c = Mid(formatString, ii, 1)
  t = Mid(text, ii, 1)
  If c = "d" Then dayString = dayString & t
  If c = "m" Then monthString = monthString & t
  If c = "y" Then yearString = yearString & t
Next ii

Convert_Date = DateSerial(Val(yearString), Val(monthString), Val(dayString))

End Function
于 2013-07-26T15:18:08.983 に答える
0
=DATE(YEAR(A1),MONTH(A1),DAY(A1))

必要なし!すでにそこにあります:) fxでこれを行う必要がある場合は、次のようになります

function convert_date(text As String) As Date
   convert_date = EVALUATE("DATE(YEAR(text),MONTH(text),DAY(text))")
end function
于 2013-07-26T15:18:01.507 に答える