How to set up Excel VBA Loop for vlookup










-1














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.



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.



Image



[Edit]
Consider the Dasboard table as a discrete table where manually entries are posted.
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. so for example if i want to add the 4th row of dashboard to be sync with the log ill have to add




+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




= 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. so the function should return me the sum of all i rather than just the last iteration.










share|improve this question



















  • 2




    Welcome to Stack Overflow. Please note that because this is no free code writing service it is necessary to show either what you have tried so far and where you got stuck or errors (by showing your code) or at least to show what you have researched and the effort you made. Otherwise it is just asking us to do all the work for you. Reading How to Ask might help you to improve your question. see stackoverflow.com/help/how-to-ask
    – Solar Mike
    Nov 12 at 9:04










  • Potentially useful references: WorksheetFunction.Vlookup or WorksheetFunction.Match
    – Chronocidal
    Nov 12 at 10:05















-1














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.



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.



Image



[Edit]
Consider the Dasboard table as a discrete table where manually entries are posted.
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. so for example if i want to add the 4th row of dashboard to be sync with the log ill have to add




+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




= 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. so the function should return me the sum of all i rather than just the last iteration.










share|improve this question



















  • 2




    Welcome to Stack Overflow. Please note that because this is no free code writing service it is necessary to show either what you have tried so far and where you got stuck or errors (by showing your code) or at least to show what you have researched and the effort you made. Otherwise it is just asking us to do all the work for you. Reading How to Ask might help you to improve your question. see stackoverflow.com/help/how-to-ask
    – Solar Mike
    Nov 12 at 9:04










  • Potentially useful references: WorksheetFunction.Vlookup or WorksheetFunction.Match
    – Chronocidal
    Nov 12 at 10:05













-1












-1








-1







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.



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.



Image



[Edit]
Consider the Dasboard table as a discrete table where manually entries are posted.
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. so for example if i want to add the 4th row of dashboard to be sync with the log ill have to add




+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




= 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. so the function should return me the sum of all i rather than just the last iteration.










share|improve this question















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.



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.



Image



[Edit]
Consider the Dasboard table as a discrete table where manually entries are posted.
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. so for example if i want to add the 4th row of dashboard to be sync with the log ill have to add




+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




= 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. so the function should return me the sum of all i rather than just the last iteration.







excel vba loops






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 10:01

























asked Nov 12 at 8:52









Osama Malik

135




135







  • 2




    Welcome to Stack Overflow. Please note that because this is no free code writing service it is necessary to show either what you have tried so far and where you got stuck or errors (by showing your code) or at least to show what you have researched and the effort you made. Otherwise it is just asking us to do all the work for you. Reading How to Ask might help you to improve your question. see stackoverflow.com/help/how-to-ask
    – Solar Mike
    Nov 12 at 9:04










  • Potentially useful references: WorksheetFunction.Vlookup or WorksheetFunction.Match
    – Chronocidal
    Nov 12 at 10:05












  • 2




    Welcome to Stack Overflow. Please note that because this is no free code writing service it is necessary to show either what you have tried so far and where you got stuck or errors (by showing your code) or at least to show what you have researched and the effort you made. Otherwise it is just asking us to do all the work for you. Reading How to Ask might help you to improve your question. see stackoverflow.com/help/how-to-ask
    – Solar Mike
    Nov 12 at 9:04










  • Potentially useful references: WorksheetFunction.Vlookup or WorksheetFunction.Match
    – Chronocidal
    Nov 12 at 10:05







2




2




Welcome to Stack Overflow. Please note that because this is no free code writing service it is necessary to show either what you have tried so far and where you got stuck or errors (by showing your code) or at least to show what you have researched and the effort you made. Otherwise it is just asking us to do all the work for you. Reading How to Ask might help you to improve your question. see stackoverflow.com/help/how-to-ask
– Solar Mike
Nov 12 at 9:04




Welcome to Stack Overflow. Please note that because this is no free code writing service it is necessary to show either what you have tried so far and where you got stuck or errors (by showing your code) or at least to show what you have researched and the effort you made. Otherwise it is just asking us to do all the work for you. Reading How to Ask might help you to improve your question. see stackoverflow.com/help/how-to-ask
– Solar Mike
Nov 12 at 9:04












Potentially useful references: WorksheetFunction.Vlookup or WorksheetFunction.Match
– Chronocidal
Nov 12 at 10:05




Potentially useful references: WorksheetFunction.Vlookup or WorksheetFunction.Match
– Chronocidal
Nov 12 at 10:05












1 Answer
1






active

oldest

votes


















0














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



----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


  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)


  3. and (I3>=...,J3>=...)now we got both start and end time, we use I and J to compare


  4. if 3. is true, lookup the output column, else 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)







share|improve this answer






















  • What should i define X for?
    – Osama Malik
    Nov 12 at 10:59










  • i am using this formula =IF(AND(H3=$B$3,I3>=$C$3,J3<=$D$3),VLOOKUP(H3,$B$3:$E$8,4,FALSE)) in output column in the log. If i drag it down manually the formula wont check the every entry in the dashboard. To check every entry i have to manually write the formula for every entry. This is ok in 4 entries or less but if there are 10 or more entries it is not possible to write that long formula.
    – Osama Malik
    Nov 12 at 11:01











  • ok, got your issue, you will need to find the correct row to reference in your first part, this make it more complicate, will amend the code
    – Osman Wong
    Nov 13 at 1:46











  • Thanks Osman i got you. But there is one problem with this. What if there are more entries for one date? For example for date 22-11-18 there are two entries with different times. The formula you suggested will only take into account the first entry.
    – Osama Malik
    Nov 13 at 5:21










  • how do you decide which entries to take in that case?
    – Osman Wong
    Nov 13 at 8:11










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258620%2fhow-to-set-up-excel-vba-loop-for-vlookup%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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



----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


  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)


  3. and (I3>=...,J3>=...)now we got both start and end time, we use I and J to compare


  4. if 3. is true, lookup the output column, else 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)







share|improve this answer






















  • What should i define X for?
    – Osama Malik
    Nov 12 at 10:59










  • i am using this formula =IF(AND(H3=$B$3,I3>=$C$3,J3<=$D$3),VLOOKUP(H3,$B$3:$E$8,4,FALSE)) in output column in the log. If i drag it down manually the formula wont check the every entry in the dashboard. To check every entry i have to manually write the formula for every entry. This is ok in 4 entries or less but if there are 10 or more entries it is not possible to write that long formula.
    – Osama Malik
    Nov 12 at 11:01











  • ok, got your issue, you will need to find the correct row to reference in your first part, this make it more complicate, will amend the code
    – Osman Wong
    Nov 13 at 1:46











  • Thanks Osman i got you. But there is one problem with this. What if there are more entries for one date? For example for date 22-11-18 there are two entries with different times. The formula you suggested will only take into account the first entry.
    – Osama Malik
    Nov 13 at 5:21










  • how do you decide which entries to take in that case?
    – Osman Wong
    Nov 13 at 8:11















0














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



----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


  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)


  3. and (I3>=...,J3>=...)now we got both start and end time, we use I and J to compare


  4. if 3. is true, lookup the output column, else 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)







share|improve this answer






















  • What should i define X for?
    – Osama Malik
    Nov 12 at 10:59










  • i am using this formula =IF(AND(H3=$B$3,I3>=$C$3,J3<=$D$3),VLOOKUP(H3,$B$3:$E$8,4,FALSE)) in output column in the log. If i drag it down manually the formula wont check the every entry in the dashboard. To check every entry i have to manually write the formula for every entry. This is ok in 4 entries or less but if there are 10 or more entries it is not possible to write that long formula.
    – Osama Malik
    Nov 12 at 11:01











  • ok, got your issue, you will need to find the correct row to reference in your first part, this make it more complicate, will amend the code
    – Osman Wong
    Nov 13 at 1:46











  • Thanks Osman i got you. But there is one problem with this. What if there are more entries for one date? For example for date 22-11-18 there are two entries with different times. The formula you suggested will only take into account the first entry.
    – Osama Malik
    Nov 13 at 5:21










  • how do you decide which entries to take in that case?
    – Osman Wong
    Nov 13 at 8:11













0












0








0






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



----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


  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)


  3. and (I3>=...,J3>=...)now we got both start and end time, we use I and J to compare


  4. if 3. is true, lookup the output column, else 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)







share|improve this answer














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



----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


  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)


  3. and (I3>=...,J3>=...)now we got both start and end time, we use I and J to compare


  4. if 3. is true, lookup the output column, else 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)








share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 at 1:58

























answered Nov 12 at 9:53









Osman Wong

1037




1037











  • What should i define X for?
    – Osama Malik
    Nov 12 at 10:59










  • i am using this formula =IF(AND(H3=$B$3,I3>=$C$3,J3<=$D$3),VLOOKUP(H3,$B$3:$E$8,4,FALSE)) in output column in the log. If i drag it down manually the formula wont check the every entry in the dashboard. To check every entry i have to manually write the formula for every entry. This is ok in 4 entries or less but if there are 10 or more entries it is not possible to write that long formula.
    – Osama Malik
    Nov 12 at 11:01











  • ok, got your issue, you will need to find the correct row to reference in your first part, this make it more complicate, will amend the code
    – Osman Wong
    Nov 13 at 1:46











  • Thanks Osman i got you. But there is one problem with this. What if there are more entries for one date? For example for date 22-11-18 there are two entries with different times. The formula you suggested will only take into account the first entry.
    – Osama Malik
    Nov 13 at 5:21










  • how do you decide which entries to take in that case?
    – Osman Wong
    Nov 13 at 8:11
















  • What should i define X for?
    – Osama Malik
    Nov 12 at 10:59










  • i am using this formula =IF(AND(H3=$B$3,I3>=$C$3,J3<=$D$3),VLOOKUP(H3,$B$3:$E$8,4,FALSE)) in output column in the log. If i drag it down manually the formula wont check the every entry in the dashboard. To check every entry i have to manually write the formula for every entry. This is ok in 4 entries or less but if there are 10 or more entries it is not possible to write that long formula.
    – Osama Malik
    Nov 12 at 11:01











  • ok, got your issue, you will need to find the correct row to reference in your first part, this make it more complicate, will amend the code
    – Osman Wong
    Nov 13 at 1:46











  • Thanks Osman i got you. But there is one problem with this. What if there are more entries for one date? For example for date 22-11-18 there are two entries with different times. The formula you suggested will only take into account the first entry.
    – Osama Malik
    Nov 13 at 5:21










  • how do you decide which entries to take in that case?
    – Osman Wong
    Nov 13 at 8:11















What should i define X for?
– Osama Malik
Nov 12 at 10:59




What should i define X for?
– Osama Malik
Nov 12 at 10:59












i am using this formula =IF(AND(H3=$B$3,I3>=$C$3,J3<=$D$3),VLOOKUP(H3,$B$3:$E$8,4,FALSE)) in output column in the log. If i drag it down manually the formula wont check the every entry in the dashboard. To check every entry i have to manually write the formula for every entry. This is ok in 4 entries or less but if there are 10 or more entries it is not possible to write that long formula.
– Osama Malik
Nov 12 at 11:01





i am using this formula =IF(AND(H3=$B$3,I3>=$C$3,J3<=$D$3),VLOOKUP(H3,$B$3:$E$8,4,FALSE)) in output column in the log. If i drag it down manually the formula wont check the every entry in the dashboard. To check every entry i have to manually write the formula for every entry. This is ok in 4 entries or less but if there are 10 or more entries it is not possible to write that long formula.
– Osama Malik
Nov 12 at 11:01













ok, got your issue, you will need to find the correct row to reference in your first part, this make it more complicate, will amend the code
– Osman Wong
Nov 13 at 1:46





ok, got your issue, you will need to find the correct row to reference in your first part, this make it more complicate, will amend the code
– Osman Wong
Nov 13 at 1:46













Thanks Osman i got you. But there is one problem with this. What if there are more entries for one date? For example for date 22-11-18 there are two entries with different times. The formula you suggested will only take into account the first entry.
– Osama Malik
Nov 13 at 5:21




Thanks Osman i got you. But there is one problem with this. What if there are more entries for one date? For example for date 22-11-18 there are two entries with different times. The formula you suggested will only take into account the first entry.
– Osama Malik
Nov 13 at 5:21












how do you decide which entries to take in that case?
– Osman Wong
Nov 13 at 8:11




how do you decide which entries to take in that case?
– Osman Wong
Nov 13 at 8:11

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258620%2fhow-to-set-up-excel-vba-loop-for-vlookup%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto