A little VBA with date inputs and msgbox checks

bugarin1010

Board Regular
Joined
Jan 28, 2008
Messages
59
Hi,

I'm trying to force a user to input a date as "mm/yyyy" format only. I'm using a msgbox to capture the user response and if it passes, I go onto my next step. Otherwise, I want to have the user correct the date input format. I have the following code that I believe is almost there, but something isn't working quite right since it gets stuck in an endless loop. Here is the code:

Code:
Sub start_date()
Dim start_date As Integer
Dim UserEntry As String
Dim Msg As String
Dim TheDate As String
Msg = "Enter Date as mm/yyyy"
TheDate = Format(Date, "mm/yyyy")
    
    Do
        UserEntry = InputBox(Msg)
        If UserEntry = "" Then Exit Sub
        If UserEntry = TheDate Then
            ActiveSheet.Range("B16").Value = UserEntry
            Exit Sub
        Else
            Msg = "Please try again.  Enter date as mm/yyyy"
        End If
    Loop
End Sub

Any help is greatly appreciated. Thanks in advance!!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi Vog II,

Thanks for the link and your response. Would this method work if I absolutely need the date to be on the first of every month?

The reason I need this is that my program cycles many times, aggregates the different iterations and then plots them based on the month that they fall. This happens over and over again. If the days are not on the first of the month, my "index" and "match" functions won't work very well.

Thanks!
 
Upvote 0
Hi Vog II,

I see what you meant. I think I have what I need now. Here's the final code:


Code:
Sub start_date()
Dim start_date As Integer
Dim UserEntry As String
Dim Msg As String
Dim TheDate As String
Msg = "Enter Date as mm/yyyy"
    
    Do
        UserEntry = InputBox(Msg)
        If UserEntry = "" Then Exit Sub
        If IsDate(UserEntry) Then
            ActiveSheet.Range("B16").Value = Format(UserEntry, "mm/yyyy")
            Exit Sub
        Else
            Msg = "Please try again.  Enter date as mm/yyyy"
        End If
    Loop
End Sub

Thanks again for you help and the link!!
 
Upvote 0
In formula

=DATE(YEAR(A1),MONTH(A1),1)

which should be translatable into VBA.
 
Upvote 0
That new formula you gave me works great as well to force the input to be on the first of the month! That's something new for me so thanks again.
 
Upvote 0

Forum statistics

Threads
1,213,485
Messages
6,113,931
Members
448,533
Latest member
thietbibeboiwasaco

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