macro to force use to enter valid month and year

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have the following macro below that forces the user to input a Valid month and year in cell A3


When running the macro, the month and year is not inserted

It would be appreciated if someone could kindly assist me


I have posted a similar question on link below





Code:
 Sub DateInput()
Dim strDate As String
Dim myDate As String

Sheets("WML").Select
If Range("A3").Value > Now() Then
Exit Sub
Else
Range("A3").Select

  strDate = InputBox("enter the accounting Month and Year eg Aug 2020", "mm/yyyyy")
If IsDate(strDate) Then

strDate = Format(CDate(strDate), "mm/yyyy")
MsgBox strDate
Else
MsgBox "Wrong date format"
End If


  End If

End Sub [code]
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Where should the date be 'inserted'?
 
Upvote 0
Try this:

VBA Code:
Sub DateInput()
Dim strDate As String
Dim myDate As String

Sheets("WML").Select
If Range("A3").Value > Now() Then
    Exit Sub
Else
    'Range("A3").Select
    strDate = InputBox("enter the accounting Month and Year eg Aug 2020", "mm/yyyyy")
    If IsDate(strDate) Then
    Range("A3") = Format(CDate(strDate), "'mm/yyyy") 'input to cell here
    'MsgBox strDate
    Else
        MsgBox "Wrong date format"
    End If
End If
End Sub

Note the leading single apostrophe in "'mm/yyyy". This is required to prevent Excel from auto converting it to a date 01/mm/yyyy.
Remove the apostrophe if that is what (date) you want.
 
Upvote 0
thanks for the help. Your code works perfectly
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,749
Members
448,989
Latest member
mariah3

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