VBA在文本框中更改日期格式

时间:2016-08-30 07:16:07

标签: vba excel-vba excel

我想在textbox1中插入一个像“feb-2016”这样的日期,并记住变量“data”,就像我插入它一样。

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

上面的代码msgbox data的输出是01.02.2016,我想显示输入“feb-2016”

2 个答案:

答案 0 :(得分:1)

您可以使用这样的两个函数MonthNameYear构建一个字符串

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

其中date是您的约会对象。如果MonthName中的第二个参数为TRUE,则返回缩写而不是完整的月份名称。

答案 1 :(得分:1)

使用以下代码保存:

data As Dateluna As Stringanul 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