我只开始使用VBa,并且不断收到Expected:statement end错误。 我不确定是什么问题。我需要vba来循环带有vlookup和iferror的if语句。我无法设置范围,因为列表将是动态的。 我尝试自己修复代码的次数越多,则越混乱。我已经在网上搜寻了帮助。
我们将不胜感激。
该公式在MasterData选项卡上运行,并从DataDrop选项卡中提取。
Sub COAB()
Dim i As Range
Set i = Sheets("MasterData").Range("D2")
Dim i2 As Range
Dim i3 As Range
Set i2 = Sheets("MasterData").Range("W2")
Set i3 = Sheets("MasterData").Range("X2")
Dim MasterData As Worksheet
Dim DataDrop As Worksheet
Set MasterData = ThisWorkbook.Sheets("MasterData")
Set DataDrop = ThisWorkbook.Sheets("DataDrop")
Do Until IsEmpty(i)
i2 = "=IFERROR(IF(VLOOKUP(" & MasterData.Range("D2").Address(0, 0) & ","
& DataDrop.Range("A:C") & ",3,FALSE)" <> "NULL" & ", & ""Charged Off"" & ","
& "Active "),"& Active")"
i3 = "=IFERROR(VLOOKUP(RC[-20],'DataDrop'!A:C,2,FALSE),0)"
Set i = i.Offset(1)
Set i2 = i2.Offset(1, 0)
Set i3 = i3.Offset(1, 0)
Loop
End Sub
一栏中的公式应为= IFERROR(IF(VLOOKUP(D2,DataDrop!A:C,3,FALSE)<>“ NULL”,“ Charged Off”,“ Active”),“ Active”)< / p>
另一列应为= IFERROR(VLOOKUP(D2,DataDrop!A:C,2,FALSE),0)
答案 0 :(得分:0)
无需循环,只需获取基于列D的最后一行,然后一次性将公式写入所讨论的整个范围。另外,公式中的引号需要加倍。
Sub COAB()
With ThisWorkbook.Sheets("MasterData")
Dim lastRow As Long
lastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
.Range(.Range("W2"), .Range("W" & lastRow)).Formula = _
"=IFERROR(IF(VLOOKUP(D2,DataDrop!A:C,3,FALSE)<>""NULL"",""Charged Off"",""Active ""),"" Active"")"
.Range(.Range("X2"), .Range("X" & lastRow)).Formula = _
"=IFERROR(VLOOKUP(D2,DataDrop!A:C,2,FALSE),0)"
End With
End Sub