简体   繁体   English

如何为 vlookup 设置 Excel VBA 循环

[英]How to set up Excel VBA Loop for vlookup

I have a dashboard (image below) where I manually add entries.我有一个仪表板(下图),我可以在其中手动添加条目。 Then there is a log (image below) where all entries are recorded with the help of IF and Vlookup functions.然后有一个日志(下图),其中所有条目都在IFVlookup函数的帮助下记录。

I need a code so so that every output cell in the log finds through all the entries in the dashboard and gives the answer.我需要一个代码,以便日志中的每个输出单元格都能在仪表板中的所有条目中找到并给出答案。 I think loop for vlookup will be used.我认为将使用vlookup循环。

图片

[Edit] Consider the Dasboard table as a discrete table where manually entries are posted. [编辑] 将 Dasboard 表视为一个离散表,其中手动发布条目。 Consider log table as a continues table where record of every hour for each date is kept.将日志表视为一个连续表,其中保留每个日期的每小时记录。 The entries from Dashboard table get posted to the log table.仪表板表中的条目将发布到日志表中。 New Image attached New Image附加新图像 新图像

I have entered this function in output column in the log table:我在日志表的输出列中输入了这个函数:

 =IF( AND(H3=$B$3,I3>= $C$3,I3<$D$3) ,$E$3,0) + IF(AND(H3=$B$4,I3>= $C$4,I3<$D$4) ,$E$4,0) + IF (AND(H3=$B$5,I3>= $C$5,I3<$D$5), $E$5,0)

This works fine for me for plotting the entries but the problem is for every row in the dashboard i have to add a new IF-And function in the above.这对我绘制条目很有效,但问题是仪表板中的每一行我都必须在上面添加一个新的 IF-And 函数。 so for example if i want to add the 4th row of dashboard to be sync with the log ill have to add因此,例如,如果我想添加仪表板的第 4 行以与日志同步,则必须添加

+If(AND(H3=$B$6,I3>=$C$6,I3<$D$6),$E$6,0)

I want every row in the dashboard to add automatically somehow with a loop like: i = variable我希望仪表板中的每一行都以某种方式自动添加一个循环,例如:i = variable

 = If (AND(H3=$B$i,I3>= $C$i,I3<$D$i), $E$i,0)

Only one i will be greater than 0 while the rest will be zero.只有一个 i 大于 0 而其余的将为零。 so the function should return me the sum of all i rather than just the last iteration.所以函数应该返回所有 i 的总和,而不仅仅是最后一次迭代。

Maybe just filldown the formula manually?也许只是手动填写公式? if insist with macro, something like如果坚持使用宏,就像

Sub test()
Range("K3").Value = "X"
Range("K3:K10").FillDown
End Sub

replace "X" with your formula, keep the " " replace K10 with how far down you want用你的公式替换“X”,保留“”用你想要的多远替换 K10

----edit---- - - 编辑 - -

let me break down below for you,下面让我为你分解,

  1. match(H3,B:B,0), will find the correct row in B that = H, in H3 case it finds B3 match(H3,B:B,0), 将在 B 中找到正确的行 = H,在 H3 的情况下它找到 B3

  2. INDEX(B:E, MATCH(H3,B:B,0) ,2) -> now it find B3, index let you find C3 (notice the 2,3,4 in later codes, it means the column from B3) INDEX(B:E, MATCH(H3,B:B,0) ,2) -> 现在它找到 B3,index 让你找到 C3(注意后面代码中的 2,3,4,它表示来自 B3 的列)

  3. and (I3>=...,J3>=...)now we got both start and end time, we use I and J to compare和 (I3>=...,J3>=...) 现在我们得到了开始和结束时间,我们用 I 和 J 来比较

  4. if 3. is true, lookup the output column, else 0如果 3. 为真,则查找输出列,否则为 0

=IF(AND(I3>=INDEX(B:E,MATCH(H3,B:B,0),2),J3>=INDEX(B:E,MATCH(H3,B:B,0),3)),INDEX(B:E,MATCH(H3,B:B,0),4),0) =IF(AND(I3>=INDEX(B:E,MATCH(H3,B:B,0),2),J3>=INDEX(B:E,MATCH(H3,B:B,0),3) ),索引(B:E,MATCH(H3,B:B,0),4),0)

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

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