我想在If语句中使用Vlookup函数。我的问题是,如果K列中的任何行等于“Hatalı”,那么必须根据以下参数开始vlookup。
对于下面的代码,它给我编译错误说“Next witout For”,但我无法处理它。如果有人能帮我解决,我会很高兴。
Sub FINDSource()
On Error Resume Next
Dim Dept_Row As Long
Dim Dept_Clm As Long
Table1 = Worksheets("RegScenario").Range("B9:B10000")
Table2 = Worksheets("Source").Range("A1:C5000")
Dept_Row = Worksheets("RegScenario").Range("M9").Row
Dept_Clm = Worksheets("RegScenario").Range("M9").Column
For Each cl In Table1
If Worksheets("RegScenario").Range("K:K").Value = "Hatalı" Then
Worksheets("RegScenario").Cells(Dept_Row, Dept_Clm) = Application.WorksheetFunction.VLookup(cl, Table2, 2, False)
Dept_Row = Dept_Row + 1
Next cl
MsgBox "Done"
End If
End Sub
答案 0 :(得分:1)
这应该可以修复错误"下一步没有For"
Sub FINDSource()
On Error Resume Next
Dim Dept_Row As Long
Dim Dept_Clm As Long
Table1 = Worksheets("RegScenario").Range("B9:B10000")
Table2 = Worksheets("Source").Range("A1:C5000")
Dept_Row = Worksheets("RegScenario").Range("M9").Row
Dept_Clm = Worksheets("RegScenario").Range("M9").Column
For Each cl In Table1
If cl.Value = "Hatalı" Then
Worksheets("RegScenario").Cells(Dept_Row, Dept_Clm) = Application.WorksheetFunction.VLookup(cl, Table2, 2, False)
Dept_Row = Dept_Row + 1
End If
Next cl
MsgBox "Done"
End Sub
答案 1 :(得分:0)
我仍然不清楚你要对Worksheets("RegScenario").Range("K:K")
做些什么。
option explicit
sub FINDSource()
Dim Dept_Row As Long, Dept_Clm As Long
dim Table1 as range, table2 as range, cl as range
set Table1 = Worksheets("RegScenario").Range("B9:B10000")
set Table2 = Worksheets("Source").Range("A1:C5000")
'Dept_Row = Worksheets("RegScenario").Range("M9").Row
Dept_Clm = Worksheets("RegScenario").Range("M9").Column
On Error Resume Next
For Each cl In Table1
If cl.offset(0, 9).Value = "Hatalı" Then
Worksheets("RegScenario").Cells(cl.row, Dept_Clm) = _
Application.VLookup(cl.value, Table2, 2, False)
Dept_Row = Dept_Row + 1
else if
'code for non-Hatalı
End If
Next cl
MsgBox "Done"
End Sub
答案 2 :(得分:0)
首先:如果您正确缩进代码,您会发现If
缺少End If
。
第二次:避免使用On Error Resume Next
,而是处理您的错误。在您的情况下,您需要确保VLookup
能够在cl
范围内找到Table2
,否则会导致错误。您可以使用If Not IsError(Application.VLookup(cl.Value, Table2, 2, 0)) Then
来捕获此错误。
第三次:您需要Set
Table1
和Table2
Range
个对象。
以下代码中的更多解释:
代码
Option Explicit
Sub FINDSource()
Dim Table1 As Range, Table2 As Range, cl As Range
Dim Dept_Row As Long
Dim Dept_Clm As Long
' Set the Range Object
Set Table1 = Worksheets("RegScenario").Range("B9:B10000")
' Set the Range Object
Set Table2 = Worksheets("Source").Range("A1:C5000")
Dept_Row = Worksheets("RegScenario").Range("M9").Row '<-- this equals to 9, Anyway you have "M9" hard-coded
Dept_Clm = Worksheets("RegScenario").Range("M9").Column '<-- this equals to 13, Anyway you have "M9" hard-coded
With Worksheets("RegScenario")
For Each cl In Table1
If .Range("K" & cl.Row).Value2 = "Hatali" Then ' check if column K at the same row of Table1 Range = "Hatali"
' need to trap the error in case Vlookup unable to find a match in Table2 Range
If Not IsError(Application.VLookup(cl.Value, Table2, 2, 0)) Then
.Cells(Dept_Row, Dept_Clm) = Application.VLookup(cl.Value, Table2, 2, 0)
End If
Dept_Row = Dept_Row + 1
End If
Next cl
End With
MsgBox "Done"
End Sub