从string转换为date
短篇故事:
我的问题有两个:
-
如何从文本文件复制
05-Jun-2014
的文本,以便05-Jun-2014
的文本不会显示为#NAME?
或在5
截断? (我需要复制整个东西。) -
我如何把这个文本做成约会呢?
很长的故事
在这个文本文件中,我使用这种格式的day-month-year
,例如05-Jun-2014
。 如你所见,六月份的“e”被切断了。 如果我在excel中键入05-Jun-2014
年05-Jun-2014
,它会识别它。 我使用CDate
将string转换为date。
我的问题是多方面的。 本质上,我希望excel vba将05-Jun-2014
识别为date。
如果我试图从我的文本文件复制短语05-Jun-2014
。 我只会得到5
复制。 我将string复制到锯齿arraysjaggArray
然后从那里我称之为。
下面你可以看到我的代码,我如何复制文本:
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函数():
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()function:
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
5-Jan-00 #NAME? 14:42:58
这里是我把date复制到一个结构的地方(假设我删除了“ – ”)。 我的目标是复制date“ 05
”,“ Jun
”和“ 2014
”的部分,然后按以下格式重新组合: 05 Jun,2014
,然后尝试将其转换为date:
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
你可以直接使用CDate
而不用拆分string。 CDate
可以识别多种date格式,只要它们不含糊( "05-Jun-2014"
不是),转换就可以正常工作。 这适用于我:
Dim d As Date d = CDate("05-Jun-2014") With Range("A1") .Value = d .NumberFormat = "dd-mmm-yyyy" End With
创build“#NAME?”的方法 与VBA消息是做以下几点:
Range("A1") = "=05-Jun-2014"
请注意,“=”运算符会导致Excel将以下expression式视为一个expression式,在这种情况下,该expression式将是5减去Jun减去2014。因为没有使用名称“Jun”定义的函数,您将获得“#NAME?”。 信息。
以下不会产生错误,并且Excel也将该值正确识别为date。
Range("A1") = "05-Jun-2014"
我build议你回顾一下你把价值放在工作表中,我相信这是错误的可能来源。