日付を dd-mm-yyyy 形式から dd/mm/yyyy 形式に変換するのに苦労しています。
たとえば、2012 年 2 月 25 日 (dd/mm/yyyy) として Excel に日付を入力すると、次の行に日付を入力すると、日付が 2012 年 2 月 25 日 (dd-mm- yyyy) 形式。
私がやりたいことは、Excel で (dd/mm/yyyy) 形式で日付を入力すると、そのまま保持し、次に移動するときに (dd-mm-yyyy) 形式に戻してはならないということです。細胞。
日付を現在のシステム日付として入力すると、コードでエラーが表示されます。日付の検証に問題があります。つまり、入力した日付が有効な日付であるかどうかです。
Sub valid_date()
' ---------------------------------------------------------------------
' Final Code - Trial
' ---------------------------------------------------------------------
Dim d1 As Variant
Dim IssueDate As Variant
Dim str As Variant
d1 = Worksheets("Sheet1").Cells(6, 1).value
MsgBox " The Issue Date format is " & d1
sysdate = Date
MsgBox "System Date is " & sysdate
If IsDateValid(d1) Then ' if date is in dd/mm/yyyy format then print this
If (d1 > sysdate) Then
MsgBox "Invalid date"
End If
End If
End Sub
Function IsDateValid(pdate) As Boolean
IsDateValid = False
Set RegExp = CreateObject("VBScript.RegExp")
' it only matches whether date is in dd/mm/yyyy format or not
'
' [1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1] ---> it allows the DATE from 01 to 31
' [1-9]|0[1-9]|1[0-2] ---> it allows the MONTH from 01 to 12
' 1[9][0-9][0-9]|2[0][0-9][0-9] ---> it allows the YEAR from 1900 to 2099
'
' below is the regular expression for checking the date in dd/mm/yyyy format
RegExp.Pattern = "^([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])[/]([1-9]|0[1-9]|1[0-2])[/](1[9][0-9][0-9]|2[0][0-9][0-9])$"
' check whether the date is in dd/mm/yyyy format or not....
tempdate = RegExp.Test(pdate)
If tempdate Then ' if tempdate is in dd/mm/yyyy format than proceed further
'If isdate(tempdate) Then ' if date is a valid date then proceed further
If isdate(pdate) Then
IsDateValid = True
Else
IsDateValid = False
End If
Else
IsDateValid = False
End If
End Function
正規表現を使用して上記のコードを使用して、日付が dd/mm/yyyy 形式であるかどうかを確認しています
しかし、私が直面している問題は、dd/mm/yyyy 形式で日付を入力するたびに、Excel の日付が dd-mm-yyyy 形式になることです。
コードを少し更新しました。現在のシステム日付として日付を入力するとエラーが発生するため、もう 1 つヘルプが必要です
たとえば、日付を 09/09/2012 (現在のシステム日付と仮定) として入力し、IsDate メソッドを使用してこの日付を確認すると、エラーが発生します。
私は再び自分のコードを編集しました、
誰でもこれについて私を助けてくれますか