[英]Excel VBA split CSV files a certain order
此代码采用CSV文件,例如:
"Penn National Gaming, Inc.",16.28
"iShares 20 Year Treasury Bond E",118.88
"iShares MSCI Emerging Index Fun",42.40
第1步
第0行: ““ Penn National Gaming,Inc。”,16.28
第1行: “ iShares 20年期美国国债E”,118.88
第2行: “ iShares MSCI新兴指数指数”,42.40
第2步
它需要第0行 ,并进入:
值0:宾州国家游戏
值1 : , Inc.
价值2: 16.28
我的问题是:我怎么能做到:
值0: Penn National Gaming Inc.
价值1: 16.28
本质上,将全名(可能包含多个逗号)组合在值0中,并保持值1不变,但与此同时,仍然设法用逗号分隔CSV传递的数据。 我在想某种顺序(从行尾到行尾仅从第1行中删除1个逗号,但我找不到解决方法。
谢谢!
Dim Resp As String: Resp = Http.ResponseText
Dim Lines As Variant: Lines = Split(Resp, vbLf)
Dim sLine As String
Dim Values As Variant
For i = 0 To UBound(Lines)
sLine = Lines(i)
If InStr(sLine, ",") > 0 Then
Values = Split(sLine, ",")
这是一个有趣的问题。 我想出了一个通用函数,该函数可用于csv行中任意数量的未引用和引用的值,其中引用的值可能包含也可能不包含逗号。
测试行: "Penn National Gaming, Inc.",16.28
输出:
Value[0] = Penn National Gaming, Inc.
Value[1] = 16.28
测试行: a,b,c,"some, commas, here",16.28,"some,commas,there",17.123
输出:
Value[0] = a
Value[1] = b
Value[2] = c
Value[3] = some, commas, here
Value[4] = 16.28
Value[5] = some,commas,there
Value[6] = 17.123
我首先在行中搜索双引号“ ...”。
在每对引号中,我搜索了逗号,并用一个我认为永远不会出现的字符替换了它们, replacementCharacter = "¯"
(如果需要,可以选择其他字符)。
替换引号中的逗号后,我将使用Split()
函数按逗号将行分开。
然后,我遍历结果数组并将所有替换字符替换为逗号。
我使用给定的特定示例以及混合引号逗号值和值的更通用示例测试了我的代码:
码:
Function parseLine(sLine)
Dim Value As Variant
Dim i As Integer
quote = """"
delimiter = ","
replacementCharacter = "¯"
'get first pair of quotes
currentQuoteIndex = InStr(1, sLine, quote) 'get first quote
If (currentQuoteIndex = 0) Then
nextQuoteIndex = 0
Else
nextQuoteIndex = InStr(currentQuoteIndex + 1, sLine, quote) 'get next quote
End If
'get pairs of quotes and replace commas with replacementCharacter
Do While nextQuoteIndex <> 0 And currentQuoteIndex <> 0
subString = Mid(sLine, currentQuoteIndex + 1, nextQuoteIndex - currentQuoteIndex - 1)
subString = Replace(subString, comma, replacementCharacter)
sLine = Left(sLine, currentQuoteIndex - 1) + subString + Right(Mid(sLine, nextQuoteIndex + 1), Len(sLine))
'get next pair of quotes
currentQuoteIndex = InStr(nextQuoteIndex + 1, sLine, quote) 'get first quote
If (currentQuoteIndex = 0) Then
nextQuoteIndex = 0
Else
nextQuoteIndex = InStr(currentQuoteIndex + 1, sLine, quote) 'get next quote
End If
Loop
'split string by commas
Values = Split(sLine, delimiter)
'replace replacementCharacter with commas
For i = 0 To UBound(Values)
Values(i) = Replace(Values(i), replacementCharacter, delimiter)
Next
parseLine = Values
End Function
此函数可使用任意数量的逗号(包含引号字符串),并以任意顺序排列列。
下面的简单解决方案确定了最后一个逗号的位置。 此信息用于确定全名和价格在行中的位置。 最终结果是一个包含2个值的数组。
注意:由于不使用逗号“,”过程,所以全名中的其他逗号将被忽略
Dim Resp As String: Resp = Http.ResponseText
Dim Lines As Variant: Lines = Split(Resp, vbLf)
Dim sLine As String
Dim Values(1) As Variant
For i = 0 To UBound(Lines)
sLine = Lines(i)
'Reduced complexity by avoiding the need to split on commas ","
Values(0) = left(sLine,instrrev(sLine,",")-1) 'Full Name
Values(1) = mid(sLine,instrrev(sLine,",")+1) 'Price value
Next
使用功能
Dim Resp As String: Resp = Http.ResponseText
Dim Lines As Variant: Lines = Split(Resp, vbLf)
Dim sLine As String
Dim Values(1) As Variant
Function extractData(sLine as String)
Dim tmpArray(1) As Variant
'Reduced complexity by avoiding the need to split on commas ","
tmpArray(0) = left(sLine,instrrev(sLine,",")-1) 'Full Name
tmpArray(1) = mid(sLine,instrrev(sLine,",")+1) 'Price value
extractData = tmpArray
End Function
For i = 0 To UBound(Lines)
sLine = Lines(i)
Values = extractData(sLine)
Next
输出:
值0:Penn National Gaming,Inc.
价值1:16.28
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.