BOB
BOB

Reputation: 700

VBA Change date format in textbox

I want to insert into textbox1 a date like "feb-2016" and to memorize in a variable "data" like i insert it.

Private Sub importa_buton_Click()
Dim data As Date
Dim luna As String
Dim anul As Integer


data = Analiza_Date.TextBox1.Value
TextBox1.Value = Format(data, "mmm-yyyy")

luna = month(data)
anul = year(data)

msgbox data
msgbox luna
msgbox anul

With the code above the output for msgbox data is 01.02.2016 and i want to display like input "feb-2016"

Upvotes: 0

Views: 490

Answers (2)

Shai Rado
Shai Rado

Reputation: 33692

Use the following code to save:

data As Date , luna As String and anul As Integer

Private Sub importa_buton_Click()

Dim data As Date
Dim luna As String
Dim anul As Integer

data = Analiza_Date.TextBox1.value
TextBox1.value = Format(data, "mmm-yyyy")

luna = Format(data, "mmm")
anul = Year(data)

MsgBox luna & "-" & anul
MsgBox luna
MsgBox anul

End Sub

Upvotes: 1

raemaerne
raemaerne

Reputation: 112

You can build up a string with the two functions MonthName and Year like this

Dim str As String
str = MonthName(date, TRUE) & "-" & Year(date)

where date is your date. If the second argument in MonthName is TRUE then the abbreviation and not the full month name is returned.

Upvotes: 1

Related Questions