Error handling in vba

Swoootie

Board Regular
Joined
Jun 28, 2012
Messages
66
Hi all,

Please find below some code that I've pulled together.

I have 2 questions:

1. What I would like to see happen is that when an invalid date is entered that after the error message pops up is that the code resumes from where the user is asked to enter the date details. How can I ge this to work? I thought GoTo 0 should do it but it didn't work.

2. When the month is entered by the user it errors unless the month is added in upper case. Is there a way that I can set it as not being case sensitive?

I would be very grateful for any help that anyone can give me.

Thanks


Sub DATA()
Dim Month As Variant
Dim Year As Variant
Dim Day As Variant

Year = Application.InputBox("ENTER THE YEAR AS YYYY)
Month = Application.InputBox("ENTER THE MONTH AS MMM)
Day = Application.InputBox("ENTER THE Day AS DD)

Sheets("DATA ENTRY ").Select
Range("B14").ClearContentsIf YEAR = "2009" Then
Range("B14") = "Y1"
ElseIf YEAR= "2010" Then
Range("B14") = "Y2"
ElseIf YEAR= "2011" Then
Range("B14") = "Y3"
ElseIf YEAR= "2012" Then
Range("B14") = "Y"
Else: MsgBox ("PLEASE ENTER VALID DATE")

Exit Sub
End If
 

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.
Hi,
part of your problem is you have declared Year, Month & Day as local variables but these are VBA functions & should not be used in this way.

You can do what you want with just one inputbox & ask user to enter a valid date. You can then extract the Year element of the date by using the Year Function.

See if following helps:

Code:
Sub GetYear()
    Dim InputVal As Variant
    Dim LineFeed As String
    LineFeed = Chr(10) & Chr(10)
    Do
        InputVal = InputBox("Enter Date", "Enter Date")
        If InputVal = vbNullString Then
            msg = MsgBox("Do You Want To Quit?" & Space(10), 36, "Exit Application")
            If msg = 6 Then Exit Sub
        ElseIf Not IsDate(InputVal) Then
            MsgBox InputVal & Chr(10) & "Input Is Not A Valid Date", 16, "Error"
        Else
            Exit Do
        End If
    Loop
    With Sheets("DATA ENTRY")
        Select Case Year(CDate(InputVal))
        Case 2009
            .Range("B14") = "Y1"
        Case 2010
            .Range("B14") = "Y2"
        Case 2011
            .Range("B14") = "Y3"
        Case 2012
            .Range("B14") = "Y"
        End Select
    End With
End Sub

Hope Helpful

Dave
 
Upvote 0
Hi,

Apologies for the delay in replying and thanks very much for your response.
Got it to work by declaring the date variables as Public.

Thanks
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,580
Members
449,039
Latest member
Arbind kumar

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