我使用 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"
您可以提供一个数组作为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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)