【发布时间】:2019-02-27 11:44:24
【问题描述】:
我在第一行有一张带有项目名称的工作表。
我正在使用Forloop 来通过第 1 行中的单元格 - i。
我使用每个单元格的值内容从第 2 行中相应单元格下方的 .CSV 文件中导入一列,为此使用 j。
但是,我有一些 .CSV 文件丢失了,我需要移动到第 2 行中的下一个单元格,同时移动到第 1 行中的下一个单元格。基本上跳过一列。
到目前为止我所拥有的是:
Dim FSO As Object
Dim Folder As Object
Dim File As String
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Folder = FSO.GetFolder("C:\Users\Betty\AppData\Roaming\MetaQuotes\Terminal\B4D9BCD10BE9B5248AFCB2BE2411BA10\MQL4\Files")
For i = 2 To HCP.Cells(1, HCP.Columns.Count).End(xlToLeft).Column
Item = HCP.Cells(1, i).Value
FilePath = Folder & "\" & Item & "1440.CSV"
If Item = "" Or Dir(FilePath) = "" Then GoTo Continue
j = HCP.Cells(2, HCP.Columns.Count).End(xlToLeft).Column
With HCP.QueryTables.Add(Connection:="TEXT;" & FilePath, Destination:=HCP.Cells(2, j + 1))
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(9, 9, 9, 9, 9, 1, 9, 9, 9, 9)
.Refresh BackgroundQuery:=False
End With
Continue:
Next
我需要j 的列索引始终与i 的列索引相对应。
【问题讨论】:
标签: vba for-loop if-statement conditional-statements