VBA how to use a function within a sub ?

jsmith2094

New Member
Joined
Aug 19, 2021
Messages
35
Office Version
  1. 365
Platform
  1. Windows
Hey everyone,

I have created a function :

VBA Code:
Function Name_of_month(a)

    Name_of_month = "Unknown"
    If a = 1 Then Name_of_month = "January"
    If a = 2 Then Name_of_month = "February"
    If a = 3 Then Name_of_month = "March"
    If a = 4 Then Name_of_month = "April"
    If a = 5 Then Name_of_month = "May"
    If a = 6 Then Name_of_month = "June"
    If a = 7 Then Name_of_month = "July"
    If a = 8 Then Name_of_month = "August"
    If a = 9 Then Name_of_month = "September"
    If a = 10 Then Name_of_month = "October"
    If a = 11 Then Name_of_month = "November"
    If a = 12 Then Name_of_month = "December"

End Function

what I need to do is to assign number to Column A from 1-12 then based on the cell value then display a mesgbox containing the month of the year so for example 1= january, 2= febuary and so on.....

I have attempted but think im way off lol

Code:
Sub Enumerate_all_months()

Dim A As Integer    ' Create whole number variable

Dim b As String

    For A = 1 To 12
                        
    Cells(A, 1) = A
                        
    Next
    b = Name_of_month(A)
    MsgBox b
    

End Sub

Any help would be apricated :)
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi
Try
VBA Code:
Sub Enumerate_all_months()

Dim A As Integer    ' Create whole number variable

Dim b As String

    For A = 1 To 12
                        
    Cells(A, 1) = A
       b = Name_of_month(A)
    MsgBox b
    Next


End Sub
 
Upvote 0
Solution
You could probably shorten your function to something like:

Code:
Function Name_of_month(a)

    Name_of_month = "Unknown"
    On Error Resume Next
    Name_of_month = Format$(DateSerial(2000, a, 1), "mmmm")

End Function

then your msgbox needs to be inside the loop:

Code:
Sub Enumerate_all_months()

Dim A As Integer    ' Create whole number variable

    For A = 1 To 12
                       
    Cells(A, 1) = A
    MsgBox Name_of_month(A)                       
    Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,007
Messages
6,122,670
Members
449,091
Latest member
peppernaut

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