Macro for formula need to change based on cell value

maheshrk81

Board Regular
Joined
Jan 5, 2010
Messages
153
Hi All,

I need a macro where the formula should get updated based on cell value.
I have a refresh button, when i select the refresh button, my Cell T1, U1, V1 and W1 should get updated as below.

If my Cell "A1" is "All" then Cell T1 "All" else "Vlookup(formula)".
If my Cell "B1" is "All" then Cell U1 "All" else "Vlookup(formula)".
If my Cell "C1" is "All" then Cell V1 "All" else "Vlookup(formula)".
If my Cell "D1" is "All" then Cell W1 "All" else "Vlookup(formula)".

Thanks in Advance.
Maheshrk
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Re: Need Macro for formula need to change based on cell value

I don't understand why you need a macro. Why don't you just put this directly in T1 with similar formulas in U1:W1?

=IF(A1="All",A1,vlookup_formula)

If you do need a macro, please ..
a) explain why, and
b) clarify exactly what the VLOOKUP formula should be in each column T:W
 
Upvote 0
Re: Need Macro for formula need to change based on cell value

Hi Peter,

Thanks for your reply.
I need this in Macro as i will callout this macro in another macro.
Can you please give me as a Code.

Thanks,
Maheshrk
 
Upvote 0
Re: Need Macro for formula need to change based on cell value

I Need below formulas.

If A1 = "All" then in Cell T1 it should be
VLOOKUP(K1,N7:R500,2,0)
Else VLOOKUP(K1,S7:W500,2,0)

If B1 = "All" then in Cell U1 it should be
VLOOKUP(K2,N7:R500,3,0)
Else VLOOKUP(K2,S7:W500,3,0)

If C1 = "All" then in Cell V1 it should be
VLOOKUP(K3,N7:R500,4,0)
Else VLOOKUP(K3,S7:W500,4,0)

If D1 = "All" then in Cell W1 it should be
VLOOKUP(K4,N7:R500,5,0)
Else VLOOKUP(K4,S7:W500,5,0)

Please help me with the code
 
Last edited:
Upvote 0
Re: Need Macro for formula need to change based on cell value

I Need below formulas.
That seems different to what you wrote at the beginning but this should put those formulas in T1:W1

Code:
Sub Insert_Formulas()
  Range("T1").Formula = "=IF(A1=""All"",VLOOKUP(K1,N7:R500,2,0),VLOOKUP(K1,S7:W500,2,0))"
  Range("U1").Formula = "=IF(B1=""All"",VLOOKUP(K2,N7:R500,3,0),VLOOKUP(K2,S7:W500,3,0))"
  Range("V1").Formula = "=IF(C1=""All"",VLOOKUP(K3,N7:R500,4,0),VLOOKUP(K3,S7:W500,4,0))"
  Range("W1").Formula = "=IF(D1=""All"",VLOOKUP(K4,N7:R500,5,0),VLOOKUP(K4,S7:W500,5,0))"
End Sub
 
Upvote 0
Thanks Peter.
But I need only one VLOOKUP formula to show there. If it shows complete formula then i can use the IF Formula there which will not fulfill my requirement.

I need the Macro to read Cell "A1" and based on that in "T1" the formula should get updated. Same for other Cells.

Thanks,
Maheshrk
 
Upvote 0
I don't understand. As far as I can see my suggestion does exactly what is described in post 5.
Can you spell out in more detail and with specific example(s) what you need?
 
Upvote 0
I think the OP is looking for this which will only show "one VLOOKUP formula"

Code:
Sub Insert_Formulas()
  Range("T1").Formula = IIf(Range("A1") = "All", "VLOOKUP(K1,N7:R500,2,0)", "VLOOKUP(K1,S7:W500,2,0))")
  Range("U1").Formula = IIf(Range("B1") = "All", "VLOOKUP(K2,N7:R500,3,0)", "VLOOKUP(K2,S7:W500,3,0))")
  Range("V1").Formula = IIf(Range("C1") = "All", "VLOOKUP(K3,N7:R500,4,0)", "VLOOKUP(K3,S7:W500,4,0))")
  Range("W1").Formula = IIf(Range("D1") = "All", "VLOOKUP(K4,N7:R500,5,0)", "VLOOKUP(K4,S7:W500,5,0))")
End Sub

WBD
 
Upvote 0
I think the OP is looking for this which will only show "one VLOOKUP formula"

Code:
Sub Insert_Formulas()
  Range("T1").Formula = IIf(Range("A1") = "All", "VLOOKUP(K1,N7:R500,2,0)", "VLOOKUP(K1,S7:W500,2,0))")
  Range("U1").Formula = IIf(Range("B1") = "All", "VLOOKUP(K2,N7:R500,3,0)", "VLOOKUP(K2,S7:W500,3,0))")
  Range("V1").Formula = IIf(Range("C1") = "All", "VLOOKUP(K3,N7:R500,4,0)", "VLOOKUP(K3,S7:W500,4,0))")
  Range("W1").Formula = IIf(Range("D1") = "All", "VLOOKUP(K4,N7:R500,5,0)", "VLOOKUP(K4,S7:W500,5,0))")
End Sub

WBD
Ah WBD, you may be right, although the cell results will be the same won't they?

If you are correct, then those formulas would each need an "=" sign and the second of each pair would need one less closing ")"

Code:
Sub Insert_Formulas2()
  Range("T1").Formula = IIf(Range("A1") = "All", "=VLOOKUP(K1,N7:R500,2,0)", "=VLOOKUP(K1,S7:W500,2,0)")
  Range("U1").Formula = IIf(Range("B1") = "All", "=VLOOKUP(K2,N7:R500,3,0)", "=VLOOKUP(K2,S7:W500,3,0)")
  Range("V1").Formula = IIf(Range("C1") = "All", "=VLOOKUP(K3,N7:R500,4,0)", "=VLOOKUP(K3,S7:W500,4,0)")
  Range("W1").Formula = IIf(Range("D1") = "All", "=VLOOKUP(K4,N7:R500,5,0)", "=VLOOKUP(K4,S7:W500,5,0)")
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top