简体   繁体   English

插入Excel单元格时的VBA日期值会更改其格式

[英]VBA Date values when inserted to Excel cells change their format

I have date variables formatted like: 25_December_2010 我的日期变量格式如下: 25_December_2010

Once i use 一旦我使用

Dim strDate As String
strDate = "25_December_2010"
strDate = Replace(strDate,"_"," ")
MsgBox strDate

Surely enough a MsgBox pops up and gives me: 25 December 2010. 果然弹出一个MsgBox并给我:2010年12月25日。

However once i try to put the value into a cell for example: 但是,一旦我尝试将值放入单元格 ,例如:

Sheets("Sheet1").Range("A1").Value = strdate

Instead of populating the cell with: 25 December 2010 ; 而不是向该cell填充: 25 December 2010 Excel acts on it's own accord and populates the cell with the vexing entry configuration: 25-Dec-2010 ! Excel自行执行操作,并用令人讨厌的条目配置填充单元格: 25-Dec-2010

How can I have my cell populated with no hyphen characters inbetween and not having the month name trimmed? 如何在我的单元格之间插入没有hyphen字符且没有修剪月份的名字?

This code puts the date into A1 in the format you write that you want: 此代码以您想要的格式将日期放入A1中:

Option Explicit
Sub WriteDate()
    Dim strDate As String
strDate = "25_December_2010"
strDate = Replace(strDate, "_", " ")
MsgBox strDate

With Sheets("Sheet1").Range("A1")
    .Value = strDate
    .NumberFormat = "dd mmmm yyyy"
End With

End Sub

I'm not sure if it is necessary, but for clarity, since strDate is a string data type, I would probably use 我不确定是否有必要,但是为了清楚起见,由于strDate是字符串数据类型,因此我可能会使用

.Value = CDate(strDate)

Explicitly converting it to the Date data type, before writing it to the worksheet, might be of value in non-English versions, but I've not checked that specifically. 在将其写入工作表之前,将其显式转换为Date数据类型在非英语版本中可能很有用,但我没有对此进行专门检查。

使用自定义日期格式:

Sheets("Sheet1").Range("A1").NumberFormat = "dd mmmm yy" //A1 = 25 December 10

The Excel sheet is not wrong, so stop saying it is. Excel工作表没有错,所以请不要再说错了。 A date is a count of the number of days since a start date. 日期是从开始日期算起的天数。 So a date is a NUMBER . 所以日期是NUMBER You can format it how you want. 您可以根据需要格式化它。

This is VBA, excel is similar though the starting dates are different. 这是VBA,尽管开始日期不同,但是excel相似。

Date variables are stored as IEEE 64-bit (8-byte) floating-point numbers that represent dates ranging from 1 January 100 to 31 December 9999 and times from 0:00:00 to 23:59:59. 日期变量以IEEE 64位(8字节)浮点数存储,代表日期范围为9999年1月1日至9999年12月31日,时间为0:00:00至23:59:59。 Any recognizable literal date values can be assigned to Date variables. 任何可识别的文字日期值都可以分配给Date变量。 Date literals must be enclosed within number signs (#), for example, #January 1, 1993# or #1 Jan 93#. 日期文字必须包含在数字符号(#)中,例如,#January 1,1993#或#1 Jan 93#。

Date variables display dates according to the short date format recognized by your computer. 日期变量根据计算机识别的短日期格式显示日期。 Times display according to the time format (either 12-hour or 24-hour) recognized by your computer. 时间根据计算机识别的时间格式(12小时或24小时)显示。

When other numeric types are converted to Date, values to the left of the decimal represent date information while values to the right of the decimal represent time. 当其他数字类型转换为日期时,小数点左边的值表示日期信息,而小数点右边的值表示时间。 Midnight is 0 and midday is 0.5. 午夜为0,午夜为0.5。 Negative whole numbers represent dates before 30 December 1899. 负整数表示1899年12月30日之前的日期。

Date 8 bytes January 1, 100 to December 31, 9999 日期8字节100年1月1日至9999年12月31日

This is what recording it in Excel shows. 这就是在Excel中记录所显示的内容。

    Selection.NumberFormat = "d mm yyyy"

This works here for me 这对我有用

Sub DateF()

Dim strDate As String
        strDate = "25_December_2010"
        strDate = Replace(strDate, "_", " ")
        MsgBox strDate
        Worksheets("Sheet1").Range("A1").NumberFormat = "dd mm yy"

        Worksheets("Sheet1").Range("A1").Value = strDate
End Sub

I also changed it from sheet. 我也从工作表中更改了它。 to worksheet. 到工作表。

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

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