我有一个带有空格分隔的固定宽度字符串的文本文件。文件中的每一行代表一行数据,每行数据由5列组成。 例如:
16/08/2016 16/08/2016 6,52 ACCREDITO
09/08/2016 09/08/2016 206,73 ACCREDITO
03/08/2016 03/08/2016 1,00 COMMISS. BOLLETTINO
03/08/2016 03/08/2016 248,89 ADDEBITO BOLLETTINO
我试图这样做
Open "d:\Users\Desktop\q.txt" For Input As #1
Dim i As Integer
i = 0
Do Until EOF(1)
Line Input #1, NewTextLine
WrdArray() = Split(NewTextLine, " ")
Loop
Close #1
问题是split函数中的分隔符是固定的“”但是分隔字符串的空格不是固定的,所以数组WrdArray也包含空格,我不需要spases,我想我的数组包含
First Line:
WrdArray(0)---->09/05/2016
WrdArray(1)---->11/05/2016
WrdArray(2)---->21,45
WrdArray(3)---->0,00
WrdArray(4)---->descrizione
Second Line
WrdArray(0)---->09/05/2016
WrdArray(1)---->11/05/2016
WrdArray(2)---->0,00
WrdArray(3)---->39,87
WrdArray(4)---->descrizione
答案 0 :(得分:1)
由于您的数据包含5列,因此我用于替换空格的原始代码(当我认为它是4列时)将无效。试试这个:
Public Function GetArray2(pstrExpression As Variant)
Dim data(4) As String
data(0) = Trim(Mid(pstrExpression, 1, 14))
data(1) = Trim(Mid(pstrExpression, 13, 20))
data(2) = Trim(Mid(pstrExpression, 33, 13))
data(3) = Trim(Mid(pstrExpression, 46, 13))
data(4) = Trim(Mid(pstrExpression, 59, 20))
GetArray2 = data
End Function
测试方法:
Public Sub TestGetArray2()
Dim item As Integer
Dim data As Variant
Dim dataLines As Variant
Dim lineItem As Variant
dataLines = Array("16/08/2016 16/08/2016 6,52 ACCREDITO", _
"09/08/2016 09/08/2016 206,73 ACCREDITO", _
"03/08/2016 03/08/2016 1,00 COMMISS. BOLLETTINO", _
"03/08/2016 03/08/2016 248,89 ADDEBITO BOLLETTINO")
For Each lineItem In dataLines
Debug.Print
data = GetArray2(CStr(lineItem))
For item = 0 To UBound(data)
Debug.Print "Item" & item & ": " & data(item)
Next item
Next lineItem
End Sub
输出:
Item0: 16/08/2016
Item1: 16/08/2016
Item2:
Item3: 6,52
Item4: ACCREDITO
Item0: 09/08/2016
Item1: 09/08/2016
Item2:
Item3: 206,73
Item4: ACCREDITO
Item0: 03/08/2016
Item1: 03/08/2016
Item2: 1,00
Item3:
Item4: COMMISS. BOLLETTINO
Item0: 03/08/2016
Item1: 03/08/2016
Item2: 248,89
Item3:
Item4: ADDEBITO BOLLETTINO