Fill formula from VBA

  • Thread starter Thread starter Archie
  • Start date Start date
A

Archie

Hi,

I try to enter a formula in a cell using VBA: =VLOOKUP($L2;'Sheet
2'!$A$2:$D$6000;2;FALSE)
This fails with a Run-time error '1004'
Entering this formula by hand works OK

When I remove the reference to 'Sheet 2' it all works OK:
=VLOOKUP($L2;$A$2:$D$6000;2;FALSE)

It looks like a bug to me (or a feature?)

How can I insert this formula refering to another sheet in a cell using VBA?
 
If you want the formula
range("a2").formula="=yourformla"
if you want the result
range("a2")=application.vlookup(etc
 
You need to enter the formula using US English conventions:

ActiveCell.Formula = "=VLOOKUP($L2,'Sheet 2'!$A$2:$D$6000,2,FALSE)"

this assumes that the name of the sheet is "Sheet 2" with a space and not
the default "Sheet2" without a space.

if you want to use semicolons (not recommended for VBA)


ActiveCell.FormulaR1C1 = "=VLOOKUP($L2;'Sheet 2'!$A$2:$D$6000;2;FALSE)"

and this assumes that the name of the function in your regional version is
VLOOKUP
 
Don,

I am using the first method to enter to formula and it fails.
This because of the reference to another Sheet.
Leaving this reference out solves the problem, but I need the reference
Entering the formula by hand solves the problem, but the formula has to
filled in on 6000 lines :(
 
Tom,

Thanks! That realy helps!
I was confused because in Excel the formule is shown with semicolons, but
from VBA I have to enter them using comma's.

Archie
 
In the first method you would write the formula EXACTLY as if it were a
formula because you have asked excel to place that formula there for you
insted of you typing it in.
..formula= "=sheet1!a1"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top