Typed formula within macro

amkerchner

New Member
Joined
May 23, 2007
Messages
10
I would like to add a button on the first page of my workbook that does an update on a later sheet. I have found when I run the macro it will "paste" what ever the last thing I copied instead of the formula I would like it to paste. Because I am at a primitive stage in my macro writing I am still using the "record" feature and then editing the macro manually. Is there a way to type in the formula into the command so that each time I run the macro it will insert the same function into the cell/column?

The formula is as follows:

=IF(ISERROR(IF(AND(D2=D3,D3=D4,D4=D5),((I2*J2+I3*J3+I4*J4+I5*J5)/SUM(J2:J5)),(IF(AND(D2=D3,D3=D4),((I2*J2+I3*J3+I4*J4)/(SUM(J2:J4))),IF(D2=D3,((I2*J2+I3*J3)/(SUM(J2,J3))),I2))))),"",(IF(AND(D2=D3,D3=D4,D4=D5),((I2*J2+I3*J3+I4*J4+I5*J5)/SUM(J2:J5)),(IF(AND(D2=D3,D3=D4),((I2*J2+I3*J3+I4*J4)/(SUM(J2:J4))),IF(D2=D3,((I2*J2+I3*J3)/(SUM(J2,J3))),I2))))))

And I need it to run each time I click the button I've made. Any suggestions? I am attempting to take the weighted average of cost when there is a repeated number in column D.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I have no idea if this is really this simple, but you can test something like this.

Code:
Sub FormulaMacro()

Range("F1").Formula = "=IF(ISERROR(IF(AND(D2=D3,D3=D4,D4=D5),((I2*J2+I3*J3+I4*J4+I5*J5)/SUM(J2:J5)),(IF(AND(D2=D3,D3=D4),((I2*J2+I3*J3+I4*J4)/(SUM(J2:J4))),IF(D2=D3,((I2*J2+I3*J3)/(SUM(J2,J3))),I2))))),"""",(IF(AND(D2=D3,D3=D4,D4=D5),((I2*J2+I3*J3+I4*J4+I5*J5)/SUM(J2:J5)),(IF(AND(D2=D3,D3=D4),((I2*J2+I3*J3+I4*J4)/(SUM(J2:J4))),IF(D2=D3,((I2*J2+I3*J3)/(SUM(J2,J3))),I2))))))"

End Sub
This I believe would put the formula into F1. Hope this is something like what you were looking to accomplish.
 
Upvote 0
That formula could probably be simplified if you could tell us what it is supposed to do.
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,429
Members
448,961
Latest member
nzskater

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