从文本文件中读取日期时订单错误

时间:2013-02-24 10:03:45

标签: excel date excel-vba vba

我有一个包含这样的文本文件:

03/02/2013 09:47:29 AM||User Name||Open||Normal||Path
03/02/2013 09:47:45 AM||User Name||Close||Normal||Path
03/02/2013 09:47:52 AM||User Name||Open||Normal||Path
03/02/2013 09:47:58 AM||User Name||Save||Normal||Path
03/02/2013 09:47:59 AM||User Name||Close||Normal||Path
03/02/2013 09:48:05 AM||User Name||Open||Normal||Path
03/02/2013 09:48:06 AM||User Name||Close||Normal||Path
03/02/2013 09:48:12 AM||User Name||Open||Normal||Path
03/02/2013 09:55:33 AM||User Name||Save||Normal||Path
03/02/2013 09:55:34 AM||User Name||Close||Normal||Path
03/02/2013 09:55:40 AM||User Name||Open||Normal||Path

当我在Excel中使用VBA读取此行并使用分隔符“||”将其拆分后的日期GetDay 那天是“2”,但实际上是2个月。

如何在3天和月份为2的情况下获得此日期?

1 个答案:

答案 0 :(得分:2)

以下示例之类的内容可以帮助您找到正确的方向。

Dim row_string as String    'this is a row of text to be manipulated.

Dim sDay as String, sMonth as String, sYear as String
Dim sDateTime as String, sDate as String
Dim rowArr as Variant, elements as Variant, dateElements as Variant

'split the initial row into elements
rowArr = Split(row_string,"||")

'first element is the datetime string
sDateTime = rowArr(0)

'split the datetime string into individual parts
elements = Split(sDateTime," ")

'grab the date element
sDate = elements(0)

'split the date element up into parts
dateElements = Split(sDate,"/")


'Assign date elements to individual variables
sDay = dateElements(0)
sMonth = dateElements(1)
sYear = dateElements(2)


If sDay = "03" And sMonth = "02" Then

'do something here............

End If

或者我想你可以选择真正简单的方法来做..

Dim row_string as String

'check the left 5 characters of the entire string
If Left(row_string,5) = "03/02" Then

'do something here...........

End If