简体   繁体   English

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

[英]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. 我想在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

With the code above the output for msgbox data is 01.02.2016 and i want to display like input "feb-2016" 上面的代码为msgbox data的输出是01.02.2016,我想显示像输入“ feb-2016”

You can build up a string with the two functions MonthName and Year like this 您可以像这样用两个函数MonthNameYear建立一个字符串

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

where date is your date. date是您的日期。 If the second argument in MonthName is TRUE then the abbreviation and not the full month name is returned. 如果MonthName的第二个参数为TRUE则返回缩写而不是完整的月份名称。

Use the following code to save: 使用以下代码进行保存:

data As Date , luna As String and anul As Integer data As Dateanul As Integer luna 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM