Adding a formula in VB

mandye

New Member
Joined
Mar 4, 2003
Messages
45
I have a macro set up to populate the active cell with a vlookup formula based on cell entries offset to the left and above of the active cell...


Sub Lookup_Formula()

Dim c, d, e As String

c = ActiveCell.Offset(0, -4).Value
d = ActiveCell.Offset(0, -2).Value
e = ActiveCell.Offset(-2, 0).Value

ActiveCell.Formula = "=vlookup("" & c & ""," & d & "_overview," & e & ",false)"

End Sub


Unfortunately the macro returns it as

=VLOOKUP(" & c & ",Miscellaneous_Cross_Checks_overview,1,FALSE)

Can someone tell me how to add in the value of C with quotation marks, rather than how it is returning it as above.

Thank you in anticipation.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Try this, but not sure it's what you're looking for :

ActiveCell.Formula = "=vlookup(" & c & "," & d & "_overview," & e & ",false)"


Also, you are only declaring the variable "e" as String.

If you want to declare all variables (not that it really matters in this case), then :-

Dim c as String, d as String, e As String

Or :

Dim c$, d$, e$


Also, a more efficient code structure ;-

With ActiveCell

c = .Offset(0, -4).Value
d = .Offset(0, -2).Value
e = .Offset(-2, 0).Value

.Formula = "=vlookup(" & c & "," & d & "_overview," & e & ",false)"

End with
 
Upvote 0
ActiveCell.Formula = "=vlookup(c & "," & d & "_overview," & e & ",false)"

I think this is what you're looking for.
 
Upvote 0
Thank you for your quick response, but I actually have to have C in quotation marks though, so that it reads

=vlookup("c",d, e, false)

Any other suggestions?
 
Upvote 0
Is this it? :-

ActiveCell.Formula = "=vlookup(""c""," & d & "_overview," & e & ",false)"
 
Upvote 0
Sorry for the confusion here but C is a variable and returns the value from another range, for example c = monday


I need the formula to return

vlookup("monday", d,e,false)

The suggestions that have been given are returning

vlookup("c",d,e,false) or vlookup(monday,d,e,false)

Basically I need the monday/c to appear in quotes.

Please keep persevering! :D
 
Upvote 0

Forum statistics

Threads
1,221,691
Messages
6,161,322
Members
451,696
Latest member
Senthil Murugan

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