Excel formula convert to VB Code

rkhuprus

New Member
Joined
May 8, 2013
Messages
3
How would i use the excel formula below in the vb code?

=IF(VLOOKUP(B3, G8:N17, MATCH(H7, G7:N7, 0), FALSE), H7 & "|", "") & IF(VLOOKUP(B3, G8:N17, MATCH(I7, G7:N7, 0), FALSE), I7 & "|", "") & IF(VLOOKUP(B3, G8:N17, MATCH(J7, G7:N7, 0), FALSE), J7 & "|", "") & IF(VLOOKUP(B3, G8:N17, MATCH(K7, G7:N7, 0), FALSE), K7 & "|", "") & IF(VLOOKUP(B3, G8:N17, MATCH(L7, G7:N7, 0), FALSE), L7 & "|", "") & IF(VLOOKUP(B3, G8:N17, MATCH(M7, G7:N7, 0), FALSE), M7 & "|", "") & IF(VLOOKUP(B3, G8:N17, MATCH(N7, G7:N7, 0), FALSE), N7 & " ", "")
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Pretty simple to physically put it in VBA but as that's quite a long formula I suspect there are neater ways of writing it though without context I can't help you

To put in VBA first open the VBE editor (Alt+F11) then write sub test() to start a procedure and add any of the following lines depending on what you want where my_formula is the loooong formula

my_formula = "=IF(VLOOKUP(B3, G8:N17, MATCH(H7, G7:N7, 0), FALSE), H7 & "|", "") & IF(VLOOKUP(B3, G8:N17, MATCH(I7, G7:N7, 0), FALSE), I7 & "|", "") & IF(VLOOKUP(B3, G8:N17, MATCH(J7, G7:N7, 0), FALSE), J7 & "|", "") & IF(VLOOKUP(B3, G8:N17, MATCH(K7, G7:N7, 0), FALSE), K7 & "|", "") & IF(VLOOKUP(B3, G8:N17, MATCH(L7, G7:N7, 0), FALSE), L7 & "|", "") & IF(VLOOKUP(B3, G8:N17, MATCH(M7, G7:N7, 0), FALSE), M7 & "|", "") & IF(VLOOKUP(B3, G8:N17, MATCH(N7, G7:N7, 0), FALSE), N7 & " ", "")"
'NB careful to be " at the beginning and the end of the e.g. Evalulate("=formula")
evaluate(my_formula)

'If you want to put the result in a cell then write:
range("a1") = evaluate(my_formula)

'If you want to actually write the formula to the spreadsheet so it's in the cell rather than just the result use:
range("a1").formulaR1C1 = my_formula
'That will keep the references to cells B3 etc.

'You could also use relative references by changing your formula e.g.
range("a1").formulaR1C1 = "=R[2]C[3] "
'would set A1 "=C2" i.e. the cell 2 rows down and 3 columns across
 
Upvote 0
Awesome! Thanks for the quick and detailed response.

When I try to apply your soluation, i am getting a "type mismatch" error during run time when i try to assign the results to a variable (PossibleRaces = Evaluate(formula)). The formula suppose to return a string (example: AAA|BBB|CCC). Any ideas as to why?

Dim PossibleRaces As String
Dim formula As String
Dim PossibleRacesSplit As Variant

formula = "=IF(VLOOKUP(B3, G8:N17, MATCH(H7, G7:N7, 0), FALSE), H7 & ""|"", """") & IF(VLOOKUP(B3, G8:N17, MATCH(I7, G7:N7, 0), FALSE), I7 & ""|"", """") & IF(VLOOKUP(B3, G8:N17, MATCH(J7, G7:N7, 0), FALSE), J7 & ""|"", """") & IF(VLOOKUP(B3, G8:N17, MATCH(K7, G7:N7, 0), FALSE), K7 & ""|"", """") & IF(VLOOKUP(B3, G8:N17, MATCH(L7, G7:N7, 0), FALSE), L7 & ""|"", """") & IF(VLOOKUP(B3, G8:N17, MATCH(M7, G7:N7, 0), FALSE), M7 & ""|"", """") & IF(VLOOKUP(B3, G8:N17, MATCH(N7, G7:N7, 0), FALSE), N7, """")"

PossibleRaces = Evaluate(formula)
 
Upvote 0

Forum statistics

Threads
1,213,563
Messages
6,114,332
Members
448,566
Latest member
Nickdozaj

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