Error handling in vba

Swoootie

Board Regular
Joined
Jun 28, 2012
Messages
64
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
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
5,251
Office Version
2013
Platform
Windows
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
 

Swoootie

Board Regular
Joined
Jun 28, 2012
Messages
64
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
 

Forum statistics

Threads
1,081,690
Messages
5,360,614
Members
400,592
Latest member
badgergurl

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top