Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default Newbie Question

I am sure the answer is somewhere, but I can't seem to find it. I am
trying to add a user function to an Excel sheet so that anyone in the
sheet can use the function in a cell. Is this possible?

I am fairly competent with VB, so the inner code is not an issue. Can
someone point me in the right direction or post a sample code that
does something simple like adding 1 to a cell.

Thanks in advance.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Newbie Question

Random,

Open the VBA Editor and go to the Insert menu, and choose Module.
This will add a new code module to the VBProject of the workbook.
In that module, enter your code, something like

Public Function TimesTwo(V As Double) As Double
TimesTwo = V * 2
End Function

Then, you can call this function directly from the worksheet with
a formula like

=TimesTwo(5)


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com




"Random" <Random@nwhere wrote in message
...
I am sure the answer is somewhere, but I can't seem to find it.

I am
trying to add a user function to an Excel sheet so that anyone

in the
sheet can use the function in a cell. Is this possible?

I am fairly competent with VB, so the inner code is not an

issue. Can
someone point me in the right direction or post a sample code

that
does something simple like adding 1 to a cell.

Thanks in advance.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Newbie Question

These are called userdefined functions.

The live in a general module (just like your general subroutines).

But you pass them arguments and return a value.

Option Explicit
Function myFunction(rng1 As Range, rng2 As Range) As Variant

If rng1.Cells.Count 1 _
Or rng2.Cells.Count 1 Then
myFunction = "Error--Single cells only"
Exit Function
End If

If IsNumeric(rng1.Value) _
And IsNumeric(rng2.Value) Then
myFunction = rng1 * rng2 + (rng1 - rng2)
Else
myFunction = "Non-numeric Data"
End If

End Function

One really important thing to remember is to pass it everything that it needs.
If you do something like:

Option Explicit
Function myFunction2(rng1 As Range) As Variant
myFunction2 = rng1.value * worksheets("sheet1").range("a1").value
End Function

then excel doesn't know that your function should recalculate when A1 on sheet1
is changed.

Better would be:

Option Explicit
Function myFunction3(rng1 As Range,rng2 as range) As Variant
myFunction3 = rng1.value * rng2.value
End Function

Then use it like:
=myfunction(b9,sheet1!a1)

=======
the other thing that you'll to do is validate everything you can think of.
Single cell ranges should be single cell ranges.

If you expect numbers, then check for numbers.



Random wrote:

I am sure the answer is somewhere, but I can't seem to find it. I am
trying to add a user function to an Excel sheet so that anyone in the
sheet can use the function in a cell. Is this possible?

I am fairly competent with VB, so the inner code is not an issue. Can
someone point me in the right direction or post a sample code that
does something simple like adding 1 to a cell.

Thanks in advance.


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie question wayne New Users to Excel 6 June 27th 08 02:23 PM
Real Newbie newbie question Dave New Users to Excel 0 January 10th 07 07:55 PM
If Then Question from Newbie. . . hospitalgreg Excel Discussion (Misc queries) 6 October 16th 06 08:16 PM
Newbie Question - Subtraction Formula Question [email protected] Excel Discussion (Misc queries) 3 May 5th 06 05:50 PM
Newbie question bassman Excel Discussion (Misc queries) 1 October 31st 05 10:51 PM


All times are GMT +1. The time now is 01:19 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"