我正在尝试执行vlookup循环来搜索列表中的值并将它们返回到4个特定的单元格中,但是我的公式不起作用,它只返回单元格中变量的名称。
Private Sub copydata() 'copy fornecedor data if match
Dim rng As Range
Dim rang As Integer
Dim rang2 As Integer
Dim cell As Variant
Dim rang3 As Range
rang = Worksheets("Plan1").Range("A2").End(xlDown).Row
Set rng = ActiveSheet.Range("A2:A" & rang)
Set rang3 = Range("Plan1!$G1:$K1000")
rang2 = 1
For Each cell In rng
rang2 = rang2 + 1
If cell.Interior.Color = 65535 Then
cell.Offset(0, 1).Formula = "=vlookup(cell,rang3,2,0)"
cell.Offset(0, 2).Formula = "=vlookup(cell,rang3,2,0)"
cell.Offset(0, 3).Formula = "=vlookup(cell,rang3,2,0)"
cell.Offset(0, 4).Formula = "=vlookup(cell,rang3,2,0)"
End If
Next cell
End Sub
答案 0 :(得分:1)
语法有点偏。变量范围埋在VBA语法内的引号中。由于你想要返回变量元素,你需要将它们连接在一起,使代码能够为你读取地址。
这是一种方法。
cell.Offset(0, 1).Formula = "=vlookup(" & cell.address & "," & rang3.Parent & "!" & rang3.address & ",2,0)"
答案 1 :(得分:1)
当您正在使用公式时,您无法使用变量,您需要编写"=vlookup(" & cell.value & "," & rang3.address & ",2,0)"
如果你增加2?
,那么论坛也会查找相同的值