Vba 如果满足条件,则转至下一个空单元格
我有一张表,第一行有物品名称 我正在使用Vba 如果满足条件,则转至下一个空单元格,vba,for-loop,if-statement,conditional-statements,Vba,For Loop,If Statement,Conditional Statements,我有一张表,第一行有物品名称 我正在使用For循环通过第1行的单元格-I。 我使用每个单元格的值内容从第2行相应单元格下的.CSV文件导入一列,方法是使用j。 但是,我有一些.CSV文件丢失,我需要移动到第2行的下一个单元格,同时移动到第1行的下一个单元格。基本上跳过一列 到目前为止,我得到的是: Dim FSO As Object Dim Folder As Object Dim File As String Set FSO = CreateObject("Scripting.FileSy
For
循环通过第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
的列索引相对应。我计算出来了。这就是我现在使用的
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
If j <> i Then j = i
With HCP.QueryTables.Add(Connection:="TEXT;" & FilePath, Destination:=HCP.Cells(2, j))
.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
对于i=2到HCP.Cells(1,HCP.Columns.Count).End(xlToLeft.Column)
项目=HCP.单元格(1,i).值
FilePath=文件夹&“\”项&“1440.CSV”
如果项“”或目录(文件路径)=“”,则转到继续
j=HCP.Cells(2,HCP.Columns.Count).End(xlToLeft).Column
如果j i那么j=i
使用HCP.QueryTables.Add(连接:=“TEXT;”&FilePath,目标:=HCP.Cells(2,j))
.TextFileParseType=xlDelimited
.textfilecommadelimitor=True
.TextFileSpaceDelimiter=True
.TextFileColumnDataTypes=数组(9,9,9,9,9,1,9,9,9,9)
.Refresh BackgroundQuery:=False
以
继续:
下一个
这就是结果:
请随意提出任何其他建议。我将避免使用
继续
。在进入循环之前,只需检查语句的负数。您的问题和解决方案中也缺少一些End If
语句
如果Item
或Dir
为空,我留下了一些注释,显示代码将跳到哪里。同样的结果,只是更干净的代码
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 'Test Here
j = HCP.Cells(2, HCP.Columns.Count).End(xlToLeft).Column
If j <> i Then j = i
With HCP.QueryTables.Add(Connection:="TEXT;" & FilePath, Destination:=HCP.Cells(2, j))
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(9, 9, 9, 9, 9, 1, 9, 9, 9, 9)
.Refresh BackgroundQuery:=False
End With
End If 'Skips to here if either are blank.
Next i
对于i=2到HCP.Cells(1,HCP.Columns.Count).End(xlToLeft.Column)
项目=HCP.单元格(1,i).值
FilePath=文件夹&“\”项&“1440.CSV”
如果项目“”或目录(文件路径)”,则“在此处测试”
j=HCP.Cells(2,HCP.Columns.Count).End(xlToLeft).Column
如果j i那么j=i
使用HCP.QueryTables.Add(连接:=“TEXT;”&FilePath,目标:=HCP.Cells(2,j))
.TextFileParseType=xlDelimited
.textfilecommadelimitor=True
.TextFileSpaceDelimiter=True
.TextFileColumnDataTypes=数组(9,9,9,9,9,1,9,9,9,9)
.Refresh BackgroundQuery:=False
以
如果其中一个为空,则“如果结束”跳到此处。
接下来我
解决方案2:嵌套Do
循环
避免使用“Continue”命令,因为它不是VBA命令
For i = 2 To BS.Cells(1, BS.Columns.Count).End(xlToLeft).Column: Do
Item = BS.Cells(1, i).Value
FilePath = Folder & "\" & Item & "1440.CSV"
If Item = "" Or Dir(FilePath) = "" Then Exit Do
j = BS.Cells(2, BS.Columns.Count).End(xlToLeft).Column
If j <> i Then j = i
With BS.QueryTables.Add(Connection:="TEXT;" & FilePath, Destination:=BS.Cells(2, j))
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(9, 9, 9, 9, 9, 1, 9, 9, 9, 9)
.Refresh BackgroundQuery:=False
End With
Loop While False: Next i
如果Item=”“或Dir(FilePath)=“”,则必须在中设置或条件,然后退出Do
,因为i
可能是值“”
,但文件的文件路径可能不存在,即Dir(FilePath)=“”
,这将像以前的我一样抛出一个错误
在这种情况下,如果j i那么j=i
是强制性的,因为i
的表示为=2到
,这意味着循环从第2列开始
这可以通过将i的循环声明为i=1到
的来避免。然而,这是完成工作的初始循环
此外,j
可以表示为j=BS.Cells(2,i)
,获得列索引的i
值
但是,出于进一步的保证目的,建议使用如果j i那么j=i
语句
在进一步的研究中,出现了更多的解决方案
参见解决方案3:Nested,了解带有嵌套If
语句的循环解决方案3:Nested了解带有嵌套If
语句的循环
For i = 1 To BS.Cells(1, BS.Columns.Count).End(xlToLeft).Column
For j = 1 To BS.Cells(2, BS.Columns.Count - 1).End(xlToLeft).Column
Item = BS.Cells(1, i).Value
FilePath = Folder & "\" & Item & "1440.CSV"
If ((Item <> "") Or (Dir(FilePath) <> "") And (i = j)) Then
With BS.QueryTables.Add(Connection:="TEXT;" & FilePath, Destination:=BS.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
End If
Next j
Next i
i=1到BS.Cells(1,BS.Columns.Count).End(xlToLeft.Column)的
对于j=1到BS.Cells(2,BS.Columns.Count-1).End(xlToLeft).Column
项目=BS.单元格(1,i).值
FilePath=文件夹&“\”项&“1440.CSV”
如果((项目“”)或(目录(文件路径)”)和(i=j)),则
使用BS.QueryTables.Add(连接:=“TEXT;”和文件路径,目标:=BS.Cells(2,j+1))
.TextFileParseType=xlDelimited
.textfilecommadelimitor=True
.TextFileSpaceDelimiter=True
.TextFileColumnDataTypes=数组(9,9,9,9,9,1,9,9,9,9)
.Refresh BackgroundQuery:=False
以
如果结束
下一个j
接下来我
感谢@urderboy对我的代码进行更正和改进+1但是,我在第二个End If上得到了一个“编译错误:End If不带块If”@urdearboyUpdated@I.Я.Newb很抱歉,如果条件是如果项“”和Dir(FilePath)”,那么“在这里测试,使用和,而不是或。请随意接受您的解决方案之一作为答案!”在上面的示例中,Continue
命令可以替换为Next Iteration
,因为Continue
命令是非VBA命令。
For i = 1 To BS.Cells(1, BS.Columns.Count).End(xlToLeft).Column
For j = 1 To BS.Cells(2, BS.Columns.Count - 1).End(xlToLeft).Column
Item = BS.Cells(1, i).Value
FilePath = Folder & "\" & Item & "1440.CSV"
If ((Item <> "") Or (Dir(FilePath) <> "") And (i = j)) Then
With BS.QueryTables.Add(Connection:="TEXT;" & FilePath, Destination:=BS.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
End If
Next j
Next i