How to use a text in a cell as a formula in VBA.

Martijn

New Member
Joined
Mar 9, 2009
Messages
1
I would like to make a flexible macro that uses the formula in row 3 of a column to calculate the results of row 5 to last (32333 f.e.)

This is my code so far:
Sub formuletest()
Rijen = Range("AS1").Value
kolom = Range("AS2").Value
formule = Range("AS3").Formula
For i = 5 To Rijen
T_lu = Range("M" & i).Value
Worksheets(1).Cells(i, kolom).Value = formule
Next i
End Sub

formula in AS3 is =polynome(T_lu) and it is already made in VBA.
in my code in row 7 I get:
Worksheets(1).Cells(i, kolom).Value = "=polynome(T_lu)" whitch doesn't do the trick...
It would be nice if this could be any formula that I write in AS3

Thanx
Martijn
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hi, usually use copy with a formula:
Code:
Range("A1").Copy  Destination:=Range("B1")

Or with many cells:
Code:
Range("A1").Copy Destination:=Range("B1:B1000")

That will copy the formula from A1 to the destination cell, with the same mixed or relative referencing as in the original formula.

seems like you need to change the value in parentheses in the formula though. So maybe:
Code:
Dim MyFormule

MyFormule = "=polynome(" & T_lu & ")"
Worksheets(1).Cells(i, kolom)[COLOR="Blue"].Formula[/COLOR] = MyFormule

Or, if Cell AS3 contains not the whole formula but just its name:
Code:
MyFormule = "=" & Range("AS3").Value & "(" & T_lu & ")"
Worksheets(1).Cells(i, kolom)[COLOR="Blue"].Formula[/COLOR] = MyFormule

--you can see this is getting trickier as we go. But you want to build the String which is the formula you need, then enter it as the .Formula Property or .FormulaR1C1 property, not the .Value property.

Alex
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,707
Members
448,981
Latest member
recon11bucks

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