从Cell到VBA提取日期和时间数据

时间:2017-10-11 18:48:02

标签: excel vba

我有一个excel文件,其中的列包含以下格式的日期:yyyy-mm-dd h:mm

我想将日期和时间存储在两个单独的VBA字符串变量中。我该怎么做呢?

2 个答案:

答案 0 :(得分:1)

将值设为字符串

Sub ytrewq()
    ary = Split(Range("A1").Text, " ")
    DateAsString = ary(0)
    TimeAsString = ary(1)
End Sub

答案 1 :(得分:1)

您可以使用拆分功能以两个不同的变量提取日期和时间。

Dim aDate() As String
Dim separatedDate As String
Dim separatedTime As String

'Put the value before the " " (space character) in the first case of the array 
'and the value after the " " (space character) in the second case
aDate = Split(Range("Cell where you date is").Value, " ", -1)

'You can now access these values and store them in variables
separatedDate = aDate(0)
separatedTime = aDate(1)