Menerapkan format tanggal dan/atau waktu ke ekspresi tanggal dan mengembalikan hasilnya sebagai string.
This constant, function or object is enabled with the statement Option VBASupport 1 placed before the executable program code in a module.
Sintaksis:
FormatDateTime [DateExpression as Date [, NamedFormat as Integer]]
Nilai balikan:
String
Parameter:
DateExpression: Ekspresi tanggal yang akan diformat.
NamedFormat: Sebuah opsi vbDateTimeFormat enumerasi menentukan format yang akan diterapkan pada ekspresi tanggal dan waktu. Jika dihilangkan, nilainya vbGeneralDate digunakan.
Format Tanggal dan Waktu [enumerasi vbDateTimeFormat]
vbGeneralDate | 0 | Menampilkan tanggal dan/atau waktu sebagaimana ditentukan dalam pengaturan sistem Umum Tanggal Anda. Jika hanya tanggal, tidak ada waktu ditampilkan; Jika hanya waktu, tidak ada tanggal yang ditampilkan. |
vbLongDate | 1 | Menampilkan tanggal menggunakan format penanggalan panjang yang ditentukan dalam pengaturan regional komputer Anda. |
vbShortDate | 2 | Menampilkan tanggal menggunakan format penanggalan pendek yang ditentukan dalam pengaturan regional komputer Anda. |
vbLongTime | 3 | Menampilkan waktu sebagaimana ditentukan dalam pengaturan sistem Waktu Lama Anda. |
vbShortTime | 4 | Menampilkan waktu menggunakan format 24 jam [jj:mm]. |
Kode kesalahan:
13 Ketidakcocokan jenis
Contoh:
REM ***** BASIC *****
Option VBASupport 1
Sub DateFormat
Dim d as Date
d = ["1958-01-29 00:25"]
msgbox["Format tanggal umum : " & FormatDateTime[d]]
msgbox["Long date format : " & FormatDateTime[d,vbLongDate]]
msgbox["Short date format : " & FormatDateTime[d,vbShortDate]]
msgbox["Format waktu panjang : " & FormatDateTime[d,3]]
msgbox["Format waktu pendek : " & FormatDateTime[d,vbShortTime]]
End Sub
- Remove From My Forums
Question
dears
good greeting
how can format date by automatic way just when i'm write it inside textbox excel userform vba
regards.......
Answers
Hello TAREK SHARAF,
Textbox in useform does not have format property, you could format its text content in AfterUpated event.
Here is the simply code and demonstration.
Private Sub TextBox1_AfterUpdate[] If IsDate[Me.TextBox1.Text] Then Me.TextBox1.Text = Format[Me.TextBox1.Text, "dd/mm/yyyy"] End If End Sub
Best Regards,
Terry
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact .- Marked as answer by Tuesday, February 27, 2018 4:48 AM
Hi,
It is hard to change date format automatically by handling every single key-input in a TextBox.
And I'm afraid you forget what date format should be used.
So, I made a button [Change Format] that changed date format according to selected an option button [radio button].Here's my code:
' ---[Change Format] button Private Sub btn_ChangeFormat_Click[] If [Len[TextBox1.Text] < 8] _ Or [InStr[TextBox1.Text, "/"] < 2] Then MsgBox "invalid date value" Exit Sub End If ' --------------- If [rbt_YMD_S.Value = True] Then TextBox1.Text = Format[TextBox1.Text, "yyyy/M/d"] End If If [rbt_DMY_S.Value = True] Then TextBox1.Text = Format[TextBox1.Text, "d/M/yyyy"] End If If [rbt_MDY_S.Value = True] Then TextBox1.Text = Format[TextBox1.Text, "M/d/yyyy"] End If ' --------------- If [rbt_YMD_L.Value = True] Then TextBox1.Text = Format[TextBox1.Text, "yyyy/MMM/dd"] End If If [rbt_DMY_L.Value = True] Then TextBox1.Text = Format[TextBox1.Text, "dd/MMM/yyyy"] End If If [rbt_MDY_L.Value = True] Then TextBox1.Text = Format[TextBox1.Text, "MMM/dd/yyyy"] End If End Sub
Regards,
Ashidacchi
- Edited by Ashidacchi Tuesday, February 27, 2018 3:50 AM
- Marked as answer by TAREK SHARAF Tuesday, February 27, 2018 4:48 AM