从字符串转换为日期

时间:2014-08-18 07:06:26

标签: excel vba date excel-vba text-files

短篇小说:

我的问题有两个:

  1. 如何从文本文件中复制此文字05-Jun-2014,以便文字05-Jun-2014不会显示为#NAME?或被5切断}? (我需要复制整件事。)

  2. 如何将该文字作为日期?

  3. 长篇故事

    在此文本文件中,我的日期格式为day-month-year,例如05-Jun-2014。 如你所见," e"六月被切断了。如果我在excel中键入05-Jun-2014,它会识别它。我使用CDate将字符串转换为日期。

    我的问题是多方面的。基本上我希望excel vba将05-Jun-2014识别为日期。

    如果我尝试从文本文档中复制短语05-Jun-2014。我只会复制5。我将字符串复制到锯齿状数组jaggArray,然后从那里调用它。

    下面你可以看到我的代码如何复制文本:

    Private Function createGCStruct(ByRef tempString() As String, ByVal aNum As Integer, ByVal oNum As Integer, _
    ByRef oList() As String, ByVal index As Integer, ByVal gcSoiDate As Date, ByVal gcFName As String, _
    ByVal typeName As String) As gcBStruct
    
    '...code...
    
    ' JaggedArray:
    Dim jaggArray() As Variant
    
    '...code...
    
    '2) Capture structure information from textfile array
    
        'A) remove unnecessary spaces from existing Array and Place into jaggedArray
        jaggArray = splitStringArrayElements(tempString)
    
    '...code...
    
    End Function
    

    splitStringArrayElements Function():

    Private Function splitStringArrayElements(tempString() As String) As Variant()
    ' 1) Variables:
        Dim j As Long
        Dim trimmedString As String
        Dim jaggArray() As Variant
        ReDim jaggArray(LBound(tempString()) To UBound(tempString()))
        Dim emptyStringArrayPlaceholder() As String
        ReDim emptyStringArrayPlaceholder(0 To 0)
    ' 2) Remove unwanted spaces from tempString() and create a JaggedArray
        For j = LBound(tempString()) To UBound(tempString())
            ' Remove spaces inbetween
            trimmedString = Trim(tempString(j))
            ' If empty line, array is empty
            If trimmedString = "" Then
                jaggArray(j) = emptyStringArrayPlaceholder
            ' Else add array without spaces
            Else
                jaggArray(j) = SplitAdv(trimmedString, " ")
            End If
        Next j
    
        splitStringArrayElements = jaggArray
    End Function
    

    splitAdv()函数:

    Public Function SplitAdv(ByVal InputText As String, Optional ByVal Delimiter As String) As Variant
    
    ' This function splits the sentence in InputText into
    ' words and returns a string array of the words. Each
    ' element of the array contains one word.
    
        ' This constant contains punctuation and characters
        ' that should be filtered from the input string.
        Const CHARS = """-" 'Potential options are: "!?,.;:""'()[]{}"
        Dim strReplacedText As String
        Dim intIndex As Integer
    
        ' Replace tab characters with space characters.
        strReplacedText = Trim(Replace(InputText, _
             vbTab, " "))
    
        ' Filter all specified characters from the string.
        For intIndex = 1 To Len(CHARS)
            strReplacedText = Trim(Replace(strReplacedText, _
                Mid(CHARS, intIndex, 1), " "))
        Next intIndex
    
        ' Loop until all consecutive space characters are
        ' replaced by a single space character.
        Do While InStr(strReplacedText, "  ")
            strReplacedText = Replace(strReplacedText, _
                "  ", " ")
        Loop
    
        ' Split the sentence into an array of words and return
        ' the array. If a delimiter is specified, use it.
        'MsgBox "String:" & strReplacedText
        If Len(Delimiter) = 0 Then
            SplitAdv = VBA.Split(strReplacedText)
        Else
            SplitAdv = VBA.Split(strReplacedText, Delimiter)
        End If
    End Function
    

    如果我在复制时删除-,则这三部分将成为:5-Jan-00 #NAME? 14:42:58

    这是我将日期复制到结构的位置(假设我删除了" - ")。我的目标是复制日期的部分内容" 05"," Jun"和" 2014& #34;,然后以这种格式重新组合它们:05 Jun,2014,然后尝试将其转换为日期:

    Dim rDStart As Integer      ' row of first gcDate
    Dim cDStart As Integer      ' col of first gcDate
    Dim tempD1 As Date
    Dim tempD2 As Date
    rDStart = 6                                            ' the row of the first gcDate
    cDStart = 2                                            ' the column of the first gcDate
    'Collect gcDate
    '***I get my errors below***
    tempD1 = CDate(jaggArray(rDStart - 1)(cDStart - 1) & " " & jaggArray(rDStart - 1)(cDStart) & ", " & jaggArray(rDStart - 1)(cDStart + 1))
    tempD2 = CDate(jaggArray(rDStart - 1)(cDStart + 2))
    createGCStruct.gcDate = tempD1 + tempD2
    

2 个答案:

答案 0 :(得分:2)

您可以在不分割字符串的情况下使用CDateCDate会识别许多日期格式,只要它们不含糊不清("05-Jun-2014"不是'),转换应该可以正常工作。这对我有用:

Dim d As Date
d = CDate("05-Jun-2014")
With Range("A1")
    .Value = d
    .NumberFormat = "dd-mmm-yyyy"
End With

答案 1 :(得分:2)

创建“#NAME?”的方法VBA的消息是执行以下操作:

Range("A1") = "=05-Jun-2014"

请注意,“=”运算符会使Excel将以下内容视为一个表达式,在这种情况下为5减去Jun减去2014.因为您没有使用NAME'Jun'定义的函数,所以您会得到“#NAME? “消息。

以下内容不会产生错误,Excel也会正确地将该值识别为日期。

Range("A1") = "05-Jun-2014"

我建议您查看将值放入工作表的位置,我认为该工作表可能是错误的来源。