Create custom function

  • Thread starter Thread starter andyiain
  • Start date Start date
A

andyiain

Hi,

I use the following formula all the time: =IF(H2="Saturday"
"Saturday", IF(H2="Sunday", "Sunday", IF(K2>0.79, "Evening"
"Daytime"))), where the H column has the day of the week and the
column the time.

Each time I have a new speadsheet I type this in from scratch and cop
it down for as many rows as I have (generally a few thosand), not a bi
deal I'll grant you.

I was wondering would it be 'better' to make a custom function tha
performs this task and if so how should one go about this.
understand the concepts around creating a custom function but it's al
very new.

Any help would be appreicated.

Andy
 
This will do what you want

Function myform(myday, myvalue)
If myday = "Saturday" Or myday = "Sunday" Then
myform = myday
Exit Function
End If
If myvalue > 0.79 Then
myform = "Evening"
Else
myform = "Daytime"
End If
End Function

To call it use =myform(H2,K2)
If you want to use it in many workbooks, save it in Personal.XLS and call
with =Personal!myform(H2,K2)
Remember that UDF can be a lot slower than build-in functions
best wishes
 
Go to the VBIDE (Alt-F11)

Insert a code module (Insert>Module)

Type in

Public Function MyFunc(DayRange As Range, TimeRange As Range)
If DayRange.Count > 1 Or TimeRange.Count > 1 Then
MyFunc = CVErr(xlErrRef)
Else
If DayRange.Value = "Saturday" Or DayRange.Value = "Sunday" Then
MyFunc = DayRange.Value
ElseIf TimeRange.Value > 0.79 Then
MyFunc = "Evening"
Else
MyFunc = "Daytime"
End If
End If
End Function


and use it in the worksheet like so

=MyFunc(H2,K2)


--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top