macro to force use to enter valid month and year

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,603
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

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
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,224,746
Messages
6,180,705
Members
452,994
Latest member
Janick

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