Formula Property in Excel VBA Functions

Jfarmer64

New Member
Joined
Aug 21, 2008
Messages
16
Hello-

Trying to write an excel VBA function which will populate a range of cells with values

Here is the Code


Function setvalue(UnitPrice, Qty, Freight, R)
'R is a range of cells
'Set the sales Price
R.Cells(1).Formula = Qty * (UnitPrice + Freight)
' calculate freight
R.Cells(2).Formula = Qty * Freight * -1
'Calculate Net
R.Cells(3).Formula = R.Cells(1).Value + R.Cells(2).Value

setvalue = 0
End Function

This function seems to set the R.Cells(1) and stops. Also, the Cell which calls the function displays a #value instead of Zero.

Any help would be appreciated....

Cheers!
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
User defined functions aren't meant to write values to cells.

They are only really meant to return the value to the cell they are in.

Why do you even need code for what looks to me like a simple formulas.:eek:
 
Upvote 0
No it can't is the simple answer.:)
 
Upvote 0
Welcome to the board.

Have you considered using a standard sub instead of trying to use a UDF?
 
Upvote 0
Oaktree-

Thanks for your reply. I have considered it would like to use a UDF as I would like to pass arguments to the function to create formulas for the cells in the range.

The example I supplied was to understand the theory and methods on how to do this.
 
Upvote 0
You still can... consider something like this:

Code:
Sub test()
Call setvalue(12.5, 100, 0.35, Range("A1:A3"))
End Sub

Sub setvalue(UnitPrice As Single, Qty As Long, Freight As Single, R As Range)
'R is a range of cells
'Set the sales Price
R.Cells(1) = Qty * (UnitPrice + Freight)
' calculate freight
R.Cells(2) = Qty * Freight * -1
'Calculate Net
R.Cells(3) = R.Cells(1).Value + R.Cells(2).Value
End Sub
 
Upvote 0
Nothing wrong with using a UDF to do this as long as the UDF isn't called from a worksheet cell (ie call it from a sub within code)...
 
Upvote 0
Thanks for the help and clarification. The overall attempt of this was to build a multi-sheet workbook that has the formula located on 1 tab which would have been looked up via vlookup on other tabs and populated on different sheets. The intent being 2 fold:

1.) Provide a consolidated sheet of formulas for auditiing purposes
2.) Provide a central location to add/modify formulas for sheet maintenance purposes

I will play around with this and see if I can get the results I am looking for.

Thanks for all the assistance.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,216,066
Messages
6,128,582
Members
449,459
Latest member
20rayallen

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