gpt4 book ai didi

vba - vlookup 可变索引引用

转载 作者:行者123 更新时间:2023-12-04 21:37:52 34 4
gpt4 key购买 nike

我对以下带有索引引用的 vlookup 方程有疑问。

我正在尝试使索引引用(对于我的案例 testdiff)从 2 开始增加一个值。根据我的测试,即使使用 testdiff = testdiff+1 的语句,它也没有增加

Dim colnum As Integer, testdiff As Integer

For colnum = 2 To 14

testdiff = 2

Sheets("Sheets1").Cells(8, colnum ).Formula = "=iferror(vlookup(" & Sheets("Sheets1").Cells(8, 2).Address(False, True) & ",'Sheet2'!" & Range("A:E").Address & "," & testdiff & ",False), ""NA"")"

testdiff = testdiff + 1

Next colnum

最佳答案

对于公式,Testdiff 始终为 2,因为它在循环内。如果你把它放在循环之外(在 for 语句之前)它会一直工作到 colnum = 14 然后 testdiff 将是 6 并且你的公式只占 5 列(A:E)所以它会在这里出错如果你想要所有列 A: E 那么你需要改变

testdiff = 2


testdiff = 1

并且在 For 循环之前仍然拥有它

修改后的代码:
Dim colnum As long, testdiff As long
testdiff = 2
For colnum = 2 To 14
Sheets("Sheets1").Cells(8, colnum).Formula = "=iferror(vlookup(" & Sheets("Sheets1").Cells(8, 2).Address(False, True) & ",'Sheet2'!" & Range("A:N").Address & "," & testdiff & ",False), ""NA"")"
testdiff = testdiff + 1
Next colnum

注意,不要在 VBA 中使用 Integer,使用 long。

关于vba - vlookup 可变索引引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29980521/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com