Excel VBA按照一定顺序分割CSV文件

此代码需要一个CSV文件,如:

"Penn National Gaming, Inc.",16.28 "iShares 20 Year Treasury Bond E",118.88 "iShares MSCI Emerging Index Fun",42.40 

步骤1

0号线: “”宾夕法尼亚州国家博彩公司“,16.28

第一行: “iShares20年国债E”,118.88

第二行: “iShares MSCI新兴指数乐趣”,42.40

第2步

它需要线0,并使其在:

值0:佩恩国家博彩

价值1: ,Inc.

价值2: 16.28

我的问题是:我怎样才能使它成为:

价值0:佩恩国家游戏公司

价值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行中的任何数量的非引号和引号值,其中引用的值可以包含或不包含逗号。

testing线: "Penn National Gaming, Inc.",16.28
输出:

  Value[0] = Penn National Gaming, Inc. Value[1] = 16.28 

testing线: 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 
  1. 我首先在行中search引号“…”。

  2. 在每一对引号中,我search逗号,并用一个我认为永远不会出现的字符replace它们, replacementCharacter = "¯" (如果需要,可以select不同的字符)。

  3. 一旦引用的逗号被replace,我使用Split()函数以逗号分隔行。

  4. 然后我遍历结果数组,并用逗号replace所有replacementCharacters。

我使用给定的具体示例testing了我的代码,以及混合引用的逗号值和值的更一般示例:

码:

 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 

这个函数可以处理任何包含带引号的string的逗号,列的顺序可以是任意的。

以下简单的解决scheme确定最后一个逗号的位置。 这些信息用于确定行内全名价格的位置。 最终结果是包含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:佩恩国家博彩公司

价值1:16.28