简体   繁体   English

使用VBA Vlookup循环,具有动态单元格值

[英]Loop with VBA Vlookup, with dynamic cell value

Sub tabelvul()

Dim nr(2)

For i = 2 To 31
    nr(1) = Range("AA" & i).Value
    nr(2) = Range("AN" & i).Value
    If nr(2) = 0 Then
        ' do nothing
    Else
        Range(nr(1)).Select
        ActiveCell.FormulaR1C1 = _
            "=VLOOKUP(nr(2),Tabel80[[Kolom1]:[Inschrijver]],2)"
    End If
Next i

End Sub

The first value nr(1) has is B4, so i do get the formula of Vlookup in B4, however he is searching for nr(2) and not the corresponding value of nr(2) , which is 1, so I want the formula to look for 1 in the table. 第一个值nr(1)具有为B4,所以我得到VLOOKUP在B4的公式,但他正在寻找nr(2)而不是相应的值nr(2)这是1,所以我想在表格中寻找1的公式。

I'm pretty sure there are other things that could be improved in your code, but you should change: 我很确定您的代码中还有其他可以改进的地方,但是您应该更改:

"=VLOOKUP(nr(2),Tabel80[[Kolom1]:[Inschrijver]],2)"

To this: 对此:

"=VLOOKUP(" & nr(2) & ",Tabel80[[Kolom1]:[Inschrijver]],2)"

Well it looks like you are wanting to enter the Vlookup on a range until nr(2) = Range("AN" & i).value = 0, so I don't know why you would use an array at all, if I knew what you were trying with more detail, could probably make it even better, but with what I know, this could do: 好吧,好像您想在一个范围内输入Vlookup,直到nr(2)= Range(“ AN”&i).value = 0,所以我根本不知道为什么要使用数组。知道您正在尝试的更多细节,可能会使它变得更好,但是据我所知,这可以做到:

Sub tabelvul()
For i = 2 To 31
    If Range("AN" & i).Value = 0 Then
        ' do nothing
    Else
        Range("AA" & i).FormulaR1C1 = _
            "=VLOOKUP(" & Range("AN" & i).Value & ",Tabel80[[Kolom1]:[Inschrijver]],2)"
    End If
Next i
End Sub

也许您应该将"=VLOOKUP(nr(2),Tabel80[[Kolom1]:[Inschrijver]],2)""=VLOOKUP(" & nr(2) & ",Tabel80[[Kolom1]:[Inschrijver]],2)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM