Solved

add a custom function to excel

Posted on 2014-09-25
6
134 Views
Last Modified: 2014-09-26
MonthName( number, [ abbreviate ] ) I am looking to somehow get this VBA function into excel as a custom function
Can anyone help?
0
Comment
Question by:Serena2345
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 47

Accepted Solution

by:
Martin Liss earned 167 total points
ID: 40345045
MonthName is built into Excel. Here's an explanation. If you already know that then please explain further what you need.
0
 
LVL 6

Assisted Solution

by:johnb25
johnb25 earned 166 total points
ID: 40345107
Serena,

Paste this into a module in your Personal.xlsx workbook:
Public Function NameMth(MonthNo As Long) As String

NameMth = MonthName(MonthNo)

End Function

Open in new window


Then in Excel type =NameMth(3) and the formula returns March.

John
0
 
LVL 24

Assisted Solution

by:Steve
Steve earned 167 total points
ID: 40345485
Is there a real need for a custom function?
You can use the TEXT function as below:

=TEXT("25/" & 1 &"/2014","mmmm")
You can use a cell ref in place of the 1 in the middle (this is the month number).
"mmmm" = full month name
"mmm" = short month name

A lot of users prefer not to receive workbooks with VBA.
A lot of IT departments do not like VBA in workbooks either.
So there is an argument to avoid VBA User Defined Functions.

In VBA you can use Format("25/" & 1 & "/2014", "mmmm")
FORMAT can be quite versatile in VBA

ATB
Steve
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 14

Expert Comment

by:frankhelk
ID: 40345585
I agree to the anwers who recommend to avoid custom functions ... On the other side I see the use in a functional example, and I won't spare the opportunity to give one to those who wnat to implement a custom function - even if only for educational purpose:

To add some fine art to the Example of johnb25, I would add some parameter check to the function and return a proper error code - the code below would return a #Value! error if the month number is out of the desired range [1...12]:
Public Function NameMth(MonthNo As Long) As Variant

If (MonthNo < 1 ) Or (MontNo > 12) then
   NameMth = CVErr(xlErrValue)
Else
  NameMth = MonthName(MonthNo)
End If

End Function

Open in new window

See here for some more info about returning errors to Excel from VBA.
0
 
LVL 24

Expert Comment

by:Steve
ID: 40345605
frank... typo on line 3...

'MontNo' should read 'MonthNo'

;)
0
 
LVL 12

Expert Comment

by:James Elliott
ID: 40345689
Another formula solution:

=IFERROR(CHOOSE(A1,"January","February","March","April","May","June","July","August","September","October","November","December"),"")

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question