使用Excel VBA进行字符串到日期的转换

问题描述 投票:0回答:2

具有一个字符串datetime,其中包含一个待处理的'AEST',需要将其删除,以便可以进行日期计算

要使用空白工作查找并替换'AEST'来手动完成此操作。但是要在不起作用的VBA中执行此操作。-例如,当有4个样本日期时,就会出现不一致,并且其中2个日期会转换为美国时间而不是AUS时间。-不知道这种不一致的根本原因

Sub KPIM_RptMth()

Range("H:H").Select
Selection.Replace What:="AEST", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

End Sub
e.g. sample dates:

[REQ] WOC Date and Time
12-04-2019 01:03:32 PM AEST
22-06-2019 08:33:04 AM AEST
11-06-2019 03:30:00 PM AEST
17-06-2019 02:50:00 PM AEST

the correct output is:

[REQ] WOC Date and Time
12/04/2019 13:03
22/06/2019 8:33
11/06/2019 15:30
17/06/2019 14:50

the actual output after the VBA script above runs is:
[REQ] WOC Date and Time
4/12/2019 13:03
22-06-2019 08:33:04 AM 
6/11/2019 15:30
17-06-2019 02:50:00 PM 


excel vba date date-conversion excel-dates
2个回答
1
投票

问题是计算机无法知道此字符串日期具有哪种格式

12-04-2019 01:03:32 PM AEST

可能是以下两个

dd-mm-yyyy
mm-dd-yyyy

因此,您所做的就是让计算机进行猜测,然后它就会失败。即使是人类也无法说出两种格式中的哪一种是正确的。因此,作为字符串的日期和没有实际日期的日期几乎没有用(除非您具有有关格式的其他信息,而该信息不属于日期字符串本身)。

因此,[[唯一有效的解决方案将字符串转换为日期是如果您知道两种格式中的哪一种是正确的,请使用此信息将日期分割成几部分并使用DateSerial function用它创建一个真实的日期。

您将需要对每个单元格执行以下类似操作,作为字符串日期转换。

示例

Public Sub ConvertTimeStampToRealDateTime() Dim TimeStampString As String TimeStampString = "12-04-2019 01:03:32 PM AEST" 'split by spaces Dim SplitTimeStamp As Variant SplitTimeStamp = Split(TimeStampString, " ") 'SplitTimeStamp(0) = "12-04-2019" 'SplitTimeStamp(1) = "01:03:32" 'SplitTimeStamp(2) = "PM" 'SplitTimeStamp(3) = "AEST" 'split date by dash Dim SplitDate As Variant SplitDate = Split(SplitTimeStamp(0), "-") 'SplitDate(0) = "12" 'SplitDate(1) = "04" 'SplitDate(2) = "2019" 'now we add the information which of the 3 split parts is day, month and year 'and put it together to a real date Dim RealDate As Date RealDate = DateSerial(SplitDate(2), SplitDate(1), SplitDate(0)) 'cast time string into a date Dim RealTime As Date RealTime = SplitTimeStamp(1) & " " & SplitTimeStamp(2) 'casting from string to date works good for times but not for dates 'so we can do that with the time 'Add date and time to make it a datetime Dim RealDateTime As Date RealDateTime = RealDate + RealTime 'note that we need to ADD them mathematically (+) this is not a string concatenation (&)!!! 'AEST: This information about how many hours you need to add or subtract ' from this datetime to convert it into your desired time zone needs ' needs to be done manually Dim RealDateTimeInMyZone As Date RealDateTimeInMyZone = DateAdd("h", -3, RealDateTime) 'for example subtract 3 hours End Sub
最后,您可以将实际的日期时间写入一个单元格,并用Range("A1").NumberFormat将其格式化为所需的格式。

enter image description here

图像1:字符串到日期的转换期间的变量值。

请注意,上面的代码仅是该概念的示例。如果需要使其牢固,则可能需要进行一些检查和错误处理,因为在意外的输入字符串上它将失败。

此外,最好使用其中的一个函数来重新使用它,例如:

Public Function ConvertDDMMYYYYTimeStampToRealDateTime(ByVal TimeStampString As String) As Date End Function


0
投票
您也可以使用Excel 2010中的Power QueryGet & Transform来完成此操作

所有步骤都可以通过GUI完成

  • 获取来自表/范围
  • 的数据space分隔符分隔列(仅最右边的实例)
  • 更改格式-
  • 按语言环境设置
  • 选择DateTime作为格式,选择English(Australia)作为语言环境
完成一次后,如果刷新数据源表,则可以更新查询。

现在,工作表将包含“真实的” Excel日期/时间,如果未按照您希望的格式设置,则只需更改单元格格式。

原始

enter image description here

结果

enter image description here

M代码

您并不是真的需要它,但是如果将其粘贴到高级编辑器中,如果遇到问题,它将重新创建GUI步骤。

如果使用此方法,则可能需要更改Table名称。

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], #"Split Column by Delimiter" = Table.SplitColumn(Source, "[REQ] WOC Date and Time", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, true), {"[REQ] WOC Date and Time.1", "[REQ] WOC Date and Time.2"}), #"Removed Columns" = Table.RemoveColumns(#"Split Column by Delimiter",{"[REQ] WOC Date and Time.2"}), #"Changed Type with Locale" = Table.TransformColumnTypes(#"Removed Columns", {{"[REQ] WOC Date and Time.1", type datetime}}, "en-AU") in #"Changed Type with Locale"
© www.soinside.com 2019 - 2024. All rights reserved.