使用VBA我将一个csv文件从Yahoo Finance导入excel,每行包含7个逗号分隔值。我想只导入每行的第一个和第五个逗号分隔值。目前我导入整个csv,将其解压缩到列,然后删除不需要的列。但是,这不足以用于此导入的未来用途。如何选择要导入的列?
当前代码:
qURL = "http://ichart.finance.yahoo.com/table.csv?s=" & Symbol & "&a=" & Month(Startdate) - 1 & "&b=" & Day(Startdate) & _
"&c=" & Year(Startdate) & "&d=" & Month(Enddate) - 1 & "&e=" & Day(Enddate) & "&f=" & Year(Enddate) & "&g=" & QuoteInterval & "&ignore=.csv"
With Sheets(2).QueryTables.Add(Connection:="URL;" & qURL, Destination:=ActiveCell.Offset(1, 0))
.BackgroundQuery = True
.TablesOnlyFromHTML = False
.Refresh BackgroundQuery:=False
.SaveData = True
End With
Sheets(2).Range("A2:A" & Lastrow).TextToColumns Destination:=Sheets(2).Range("A2"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False, other:=False
Columns(2).EntireColumn.Delete
Columns(2).EntireColumn.Delete
Columns(2).EntireColumn.Delete
Columns(3).EntireColumn.Delete
Columns(3).EntireColumn.Delete
Range("A3:A" & Lastrow).NumberFormat = "dd mmm yy"
Range("B3:B" & Lastrow).NumberFormat = "0.00"
答案 0 :(得分:1)
您可以将数组作为FieldInfo
参数提供给TextToColumns
这将隐藏2,3,4,6,7列
.TextToColumns FieldInfo := Array(Array(1, 1), Array(2, 9), Array(3, 9), Array(4, 9), Array(5, 1), Array(6, 9), Array(7, 9))
1 = xlGeneralFormat
9 = xlSkipColumn