VBA Month Name to Number in Excel

VBA Month Name to Number

VBA Month Name to Number in Excel

VBA convert Month Name to Month Number using Excel VBA Functions. Convert or change Month Name to number using VBA DateValue function and VBA Month function.

Macro to Convert Month Name to Number in Excel VBA

Let us see the Macro to convert Month Name to Month Number in Excel VBA. In the below macro you can change month name which is initiated to ‘sMonthName’ variable. Once you change month name accordingly it displays respective month number.

'VBA Month Name to Month Number
Sub VBA_Month_Name_To_Number()
    
    'Variable declaration
    Dim sMonthName As String
    Dim iMonthNumber As Integer
   
    sMonthName = "March"
    
    iMonthNumber = Month(DateValue("01-" & sMonthName & "-1900"))
        
    MsgBox "Month Name : " & sMonthName & vbCrLf & "Month Number : " & iMonthNumber, vbInformation, "Month Name to Number"

End Sub

Here is the output screen shot of above macro.
VBA Month Name to Number

Instructions to use Macro

Here are the instructions to use above macro in Visual basic editor.

  • Open Visual Basic Editor(VBE) by clicking Alt +F11
  • Go to code window by clicking F7
  • Copy above specified macro or procedure
  • Paste above copied code in code window
  • Run macro by clicking F5 or Run command
  • You can see output on the screen
  • Find above specified output screenshot.

Related VBA Functions used in this article

We have used Date and DateDiff VBA functions. Click on below specified links to learn more about VBA functions.

VBA DateDiff Function VBA Date Function






Leave a Comment

Your email address will not be published. Required fields are marked *