vba Excel 日期从 yyyymmdd 到 mm/dd/yyyy 的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13056051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:17:57  来源:igfitidea点击:

Excel Date Conversion from yyyymmdd to mm/dd/yyyy

excelexcel-vbaexcel-2007vba

提问by Malachi

I have been searching for about an hour on how to do this in Excel.

我一直在寻找关于如何在 Excel 中执行此操作的大约一个小时。

I have an Excel file that was created from an old system and I am pulling information from a SQL Server Database, I will be inputting the information back into the SQL Server Database and would like the Dates to match.

我有一个从旧系统创建的 Excel 文件,我正在从 SQL Server 数据库中提取信息,我会将信息输入回 SQL Server 数据库并希望日期匹配。

I have tried Creating a Custom Format, but I am unsure if I even did it Correctly. I found several places where they want to go the other way mm/dd/yyyyto yyyymmddbut they have not been helpful.

我曾尝试创建自定义格式,但我不确定我是否正确地做到了。我发现了几个地方,他们希望走另一条路mm/dd/yyyyyyyymmdd,但他们都没有帮助。

I am unfamiliar with using VBA in any Microsoft Products otherwise I am sure that this would be a simple Task.

我不熟悉在任何 Microsoft 产品中使用 VBA,否则我确信这将是一个简单的任务。

I have two separate columns that need to be changed.

我有两个单独的列需要更改。

How do I Format the entire column from (float)yyyymmddto a (Date)mm/dd/yyyy

如何将整列从(float)yyyymmdd格式化 为(Date)mm/dd/yyyy

回答by Daniel

You can convert the value to a date using a formula like this, next to the cell:

您可以使用像这样的公式将值转换为日期,在单元格旁边:

=DATE(LEFT(A1,4),MID(A1,5,2),RIGHT(A1,2))

Where A1 is the field you need to convert.

其中 A1 是您需要转换的字段。

Alternatively, you could use this code in VBA:

或者,您可以在 VBA 中使用此代码:

Sub ConvertYYYYMMDDToDate()
   Dim c As Range
   For Each c In Selection.Cells
       c.Value = DateSerial(Left(c.Value, 4), Mid(c.Value, 5, 2), Right(c.Value, 2))
       'Following line added only to enforce the format.
       c.NumberFormat = "mm/dd/yyyy"
   Next
End Sub

Just highlight any cells you want fixed and run the code.

只需突出显示您想要修复的任何单元格并运行代码。

Note as RJohnsonmentioned in the comments, this code will error if one of your selected cells is empty. You can add a condition on c.value to skip the update if it is blank.

请注意,正如评论中提到的RJohnson,如果您选择的单元格之一为空,则此代码将出错。如果 c.value 为空,您可以在 c.value 上添加条件以跳过更新。

回答by barry houdini

Do you have ROWS of data (horizontal) as you stated or COLUMNS (vertical)?

您是否有您所说的数据行(水平)或 COLUMNS(垂直)?

If it's the latter you can use "Text to columns" functionality to convert a whole column "in situ" - to do that:

如果是后者,您可以使用“文本到列”功能来“原位”转换整列 - 这样做:

Select column > Data > Text to columns > Next > Next > Choose "Date" under "column data format" and "YMD" from dropdown > Finish

选择列>数据>文本到列>下一步>下一步>选择“列数据格式”下的“日期”和下拉菜单中的“YMD”>完成

....otherwise you can convert with a formula by using

....否则您可以使用公式进行转换

=TEXT(A1,"0000-00-00")+0

=TEXT(A1,"0000-00-00")+0

and format in required date format

并以所需的日期格式格式化

回答by user2027618

for converting dd/mm/yyyyto mm/dd/yyyy

用于转换dd/mm/yyyymm/dd/yyyy

=DATE(RIGHT(a1,4),MID(a1,4,2),LEFT(a1,2))

回答by Leo

Here is a bare bones version:

这是一个裸骨版本:

Let's say that you have a date in Cell A1 in the format you described. For example: 19760210.

假设您在单元格 A1 中有您描述的格式的日期。例如:19760210

Then this formula will give you the date you want:

然后这个公式会给你你想要的日期:

=DATE(LEFT(A1,4),MID(A1,5,2),RIGHT(A1,2)).

On my system (Excel 2010) it works with strings or floats.

在我的系统 (Excel 2010) 上,它适用于字符串或浮点数。

回答by rwb

Found another (manual) answerwhich worked well for me

找到了另一个(手动)答案,对我来说效果很好

  1. Select the column.
  2. Choose Data tab
  3. Text to Columns - opens new box
  4. (choose Delimited), Next
  5. (uncheck all boxes, use "none" for text qualifier), Next
  6. use the ymd option from the Date dropdown.
  7. Click Finish
  1. 选择列。
  2. 选择数据选项卡
  3. 文本到列 - 打开新框
  4. (选择分隔),下一步
  5. (取消选中所有框,文本限定符使用“无”),下一步
  6. 使用日期下拉菜单中的 ymd 选项。
  7. 单击完成