use a macro to insert formulas in a cell

AnnetteTR

Board Regular
Joined
Aug 19, 2010
Messages
85
Hello
I'm about to make a macro where the user enters a lot of data for some rooms in an input box. These data are passed in one line in a spreadsheet. Next room input inserted in the next line, etc.
The problem is that in some columns/cells , I want to put a formula which is based on some of the other inputs. But how should the formula look like in VBA?
I have my input box, where I have a "Sub" for what happens when I press the Save button
Next available number is activated and data is stored with an offset:
With ActiveSync cell
. Offset (0, 22). Value = txtSS1.Text
. Offset (0, 23). Value = txtSS1br.Text
. Offset (0, 24). Value = txtLugeEtMin
. Offset (0, 25). Value = txtLugeEtMax

I want to create a formula where the cell reference for SS1br and LugeEtMin included

I expect to put the formula in with this feature:
'. Offset (0 , 26). Formula = SS1br * LugeEtMin * 0.3 * 3.6<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
But I want the formula to be put in cell .Offset (0 , 26) not the value<o:p></o:p>
What should be in the cell is an equation something like: = X4 * Z4 * 0.3 * 3.6
where the 4-figure must be the current row<o:p></o:p>


How should the formula look like?

Regards Annette
<o:p></o:p>
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Welcome to the Board.

Try:

Code:
.Offset(0, 26).FormulaR1C1 = "=RC[-3]*RC[-2]*0.3*3.6"
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
 
Upvote 0
Maybe something like this...

.Offset (0 , 26). Formula = "=" & SS1br & "*" & LugeEtMin & "* 0.3 * 3.6"
 
Upvote 0
Solution
Hello again<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

This time I want to create at formula that looks like this<o:p></o:p>
=IF(F6*G6<H6;H6;F6*G6)<o:p></o:p>
I keep being stopped by a runtime error and an invitation to debug
I put this in my macro:<o:p></o:p>
.Offset(0, 9).Formula = "=IF(RC[-4]*RC[-3]<RC[-2];RC[-2];RC[-4]*RC[-3])"<o:p></o:p>
Can anybody help?<o:p></o:p>

Regards Annette
 
Upvote 0
Oops only half of my "formula" is shown

I put this in my macro:
.Offset(0, 9).Formula = "=IF(RC[-4]*RC[-3]<RC[-2];RC[-2];RC[-4]*RC[-3])"

Regards Annette
 
Upvote 0
Hi
It looks that it get cut off when i submit my post

I will try again
My formula is like this:
Code:
.Offset(0, 9).Formula = "=IF(RC[-4]*RC[-3]<RC[-2];RC[-2];RC[-4]*RC[-3])"

Regards Annette
 
Upvote 0
Try:

Code:
.Offset(0, 9).FormulaR1C1 = "=IF(RC[-4]*RC[-3] < RC[-2],RC[-2],RC[-4]*RC[-3])"<RC[-2],RC[-2],RC[-4]*RC[-3])"< p>

VBA expects commas rather than semicolons and you need to use the FormulaR1C1 property because the formula is in R1c1 style.
 
Upvote 0
Is it impossible to write the "less than" sign in this blog?

my formula is:
.Offset(0, 9).Formula = "=IF(RC[-4]*RC[-3] "less than" RC[-2];RC[-2];RC[-4]*RC[-3])"

Hope it can be shown now.
Regards Annette
 
Upvote 0
Hi Andrew Poulsom

Thank you for your quick response - Ok i forgot the R1C1, and the comma I was not aware of.
Now it works !!

Regards AnnetteTR
<!-- / message -->
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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