Jac*_*on5 4 excel vba loops if-statement vlookup
我的代码循环遍历表中的单元格值,并查看是否存在具有相同名称的文档.如果它确实存在,它将执行一个操作来打开这些文件并导入数据.
If bProcess Then
FileCounter = 0
For Each folderIDX In PrimaryMergeFiles
'If folderIDX.Name = Worksheets("Table").Range("A1:A13") Then
Dim vTest As Variant
vTest = Application.WorksheetFunction.VLookup(folderIDX.Name, Worksheets("Table").Range("A1:B13"), 2, False)
'Creating Merge File
If Not IsError(vTest) Then
FileCounter = FileCounter + 1
strStatus = "Creating file " & FileCounter & " of " & PrimaryMergeFiles.Count & ": " & folderIDX.Name
Application.StatusBar = strStatus
CreateMergedFile wdApp, sPrimaryMergeDirectory, folderIDX.Name, sSourceFile, ClientCount, _
sClientSubDirectory, bClearHighlightings(ClientCount), bHome
'ElseIf IsError(vTest) Then
Else
End Sub
End If
Next
End If
Run Code Online (Sandbox Code Playgroud)
当vTest出错时,如何跳过文件或结束循环/子?
use*_*820 14
你应该使用:
Else
Exit Sub '<-- Exit! :)
End If
Run Code Online (Sandbox Code Playgroud)
代替:
Else
End Sub '<-- Instead of End :3
End If
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
编辑:回答您的评论
Dim vTest As Variant
On Error Resume Next '<-- Add these since you are catching the error immediately after
vTest = Application.WorksheetFunction.VLookup(folderIDX.Name, Worksheets("Table").Range("A1:B13"), 2, False)
On Error Goto 0 '<-- You will handle your error on the next line
If Not IsError(vTest) Then '...
'You may also want to use/use instead: If Err.Number <> 0 Then ...
Run Code Online (Sandbox Code Playgroud)
以下是有关VBA中错误处理的更多信息:http://www.cpearson.com/excel/errorhandling.htm
| 归档时间: |
|
| 查看次数: |
54818 次 |
| 最近记录: |