+ Reply to Thread
Results 1 to 13 of 13

Message for invalid entry

Hybrid View

  1. #1
    Registered User
    Join Date
    04-24-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Message for invalid entry

    I would like a message to pop up on my spreadsheet if an entry is either blank or greater than 20. Currently an error message will pop up but it will allow the user to navigate to the next cell without correcting the information. If the user inputs "25", I would like the message to pop up and then the original cell to be selected in order for the user to be forced to put a value between 1 and 20.

    My current code:

    Dim strCurrentCell As String
        Dim strB3 As String
        Dim strB4 As String
        Dim strMessage As String
        strCurrentCell = ActiveCell.Address
        strB3 = "$B$3"
        strB4 = "$B$4"
        
        'Data entry controls
    
        strMessage = "Check your entry for Customers," _
            & " there can only be up to twenty" _
            & " customers per class period."
            
        If strCurrentCell = strB3 Then
            'Assignment of Values from Work Sheet Variable
            sngCustomers = Range("Customers").Value
            
            If sngCustomers > 20 Or sngCustomers < 0 Then
                MsgBox strMessage, vbOKOnly, "Error"
                Range("Customers").Select
                Exit Sub
            End If
        
        End If

  2. #2
    Forum Contributor
    Join Date
    08-22-2011
    Location
    Texas, United States
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    516

    Re: Message for invalid entry

    What subroutine have you placed this code in?
    Regards,
    amotto

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)

  3. #3
    Registered User
    Join Date
    04-24-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Message for invalid entry

    I placed it in a private sub under the worksheet .

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

  4. #4
    Forum Contributor
    Join Date
    08-22-2011
    Location
    Texas, United States
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    516

    Re: Message for invalid entry

    Try to put it into the exit event

  5. #5
    Registered User
    Join Date
    04-24-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Message for invalid entry

    I'm sorry, I don't quite understand what you mean by that.

  6. #6
    Forum Contributor
    Join Date
    08-22-2011
    Location
    Texas, United States
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    516

    Re: Message for invalid entry

    oh sorry i thought for some reason you were in a module or userform, not sure what i was thinking. Try this
            If sngCustomers > 20 Or sngCustomers = "" Then
                MsgBox strMessage, vbOKOnly, "Error"
                Range("Customers").value = ""
                Range("Customers").Select
                Exit Sub
            End If

  7. #7
    Forum Contributor
    Join Date
    08-22-2011
    Location
    Texas, United States
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    516

    Re: Message for invalid entry

    if that doesn't work could you post your entire code so that i may play with it on my end.

  8. #8
    Registered User
    Join Date
    04-24-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Message for invalid entry

    It didn't work on my end. I am honestly very new with VBA so my entire spreadsheet needs a lot of work. That is one component of the spreadsheet and it is located at the top of the sheet. The purpose of the sheet is for the end user to input number of customers and painting ID and the worksheet will automatically generate the remaining inventory. If the inventory is below a minimum level, the user will be notified through a message. The entire spreadsheet is not working but I figured I would take the errors step by step in order to complete the project. The painting ID's are treated similarly to number of customers as the ID's range between 100-110. The code is posted below:

    Option Explicit
    
    Dim sngCustomers As Single
    Dim sngPainting As Single
    Dim sngCanvases As Single
    Dim sngAprons As Single
    Dim intWhite As Integer
    Dim intYellow As Integer
    Dim intOrange As Integer
    Dim intRed As Integer
    Dim intPurple As Integer
    Dim intBlue As Integer
    Dim intGreen As Integer
    Dim intFlesh As Integer
    Dim intSienna As Integer
    Dim intBrown As Integer
    Dim intBlack As Integer
    
    Private Sub cmdReset_Click()
        Range("Activity").Select
        Selection.ClearContents
        Range("Customers").Select
    End Sub
    
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Dim strCurrentCell As String
        Dim strB3 As String
        Dim strB4 As String
        Dim strMessage As String
        strCurrentCell = ActiveCell.Address
        strB3 = "$B$3"
        strB4 = "$B$4"
        
        'Data entry controls
    
        strMessage = "Check your entry for Customers," _
            & " there can only be up to twenty" _
            & " customers per class period."
            
        If strCurrentCell = strB3 Then
            'Assignment of Values from Work Sheet Variable
            sngCustomers = Range("Customers").Value
            
            If sngCustomers > 20 Or sngCustomers = "" Then
                MsgBox strMessage, vbOKOnly, "Error"
                Range("Customers").Value = ""
                Range("Customers").Select
                Exit Sub
            End If
        
        End If
        
        strMessage = "Check your entry for Painting," _
            & " the options range from 100-110."
        
        If strCurrentCell = strB4 Then
            'Assignment of Values from Work Sheet Variable
            sngPainting = Range("Painting").Value
            
            If sngPainting > 110 Or sngPainting < 100 Then
                MsgBox strMessage, vbOKOnly, "Error"
                Range("Painting").Select
                Exit Sub
            End If
            
        End If
       
        If strCurrentCell = strB4 Then
            'Assignment of values from worksheet to variables
            sngCustomers = Range("Customers").Value
            sngPainting = Range("Painting").Value
            sngCanvases = Range("Canvases").Value
            sngAprons = Range("Aprons").Value
            intWhite = Range("White").Value
            intYellow = Range("Yellow").Value
            intOrange = Range("Orange").Value
            intRed = Range("Red").Value
            intPurple = Range("Purple").Value
            intBlue = Range("Blue").Value
            intGreen = Range("Green").Value
            intFlesh = Range("Flesh").Value
            intSienna = Range("Sienna").Value
            intBrown = Range("Brown").Value
            intBlack = Range("Black").Value
            
            'Calculation of Inventory based on Customers
            sngCanvases = sngCanvases - sngCustomers
            sngAprons = sngAprons - sngCustomers
            
            'Calculation of Inventory based on Painting
            If sngPainting = 100 Then
                intWhite = intWhite - 0.5
                intPurple = intPurple - 0.5
                intBlue = intBlue - 0.5
                intGreen = intGreen - 0.5
                intBrown = intBrown - 0.5
                intBlack = intBlack - 0.5
            End If
                
            If sngPainting = 101 Then
                 intYellow = intYellow - 0.5
                 intRed = intRed - 0.5
                 intPurple = intPurple - 0.5
                 intBlue = intBlue - 0.5
                 intBrown = intBrown - 0.5
            End If
                 
            If sngPainting = 102 Then
                intWhite = intWhite - 0.5
                intYellow = intYellow - 0.5
                intOrange = intOrange - 0.5
                intRed = intRed - 0.5
                intPurple = intPurple - 0.5
                intFlesh = intFlesh - 0.5
                intSienna = intSienna - 0.5
            End If
                
            If sngPainting = 103 Then
                intRed = intRed - 0.5
                intPurple = intPurple - 0.5
                intSienna = intSienna - 0.5
                intBrown = intBrown - 0.5
            End If
                
            If sngPainting = 104 Then
                intWhite = intWhite - 0.5
                intYellow = intYellow - 0.5
                intOrange = intOrange - 0.5
                intBlue = intBlue - 0.5
                intFlesh = intFlesh - 0.5
                intBlack = intBlack - 0.5
            End If
                
            If sngPainting = 105 Then
                intWhite = intWhite - 0.5
                intBlue = intBlue - 0.5
                intGreen = intGreen - 0.5
                intSienna = intSienna - 0.5
                intBrown = intBrown - 0.5
                intBlack = intBlack - 0.5
            End If
                
            If sngPainting = 106 Then
                intWhite = intWhite - 0.5
                intRed = intRed - 0.5
                intBrown = intBrown - 0.5
            End If
                
            If sngPainting = 107 Then
                intWhite = intWhite - 0.5
                intYellow = intYellow - 0.5
                intOrange = intOrange - 0.5
                intRed = intRed - 0.5
                intBlue = intBlue - 0.5
                intGreen = intGreen - 0.5
                intSienna = intSienna - 0.5
            End If
                
            If sngPainting = 108 Then
                intWhite = intWhite - 0.5
                intOrange = intOrange - 0.5
                intRed = intRed - 0.5
                intBlue = intBlue - 0.5
                intGreen = intGreen - 0.5
                intFlesh = intFlesh - 0.5
            End If
            
            If sngPainting = 109 Then
                intWhite = intWhite - 0.5
                intYellow = intYellow - 0.5
                intRed = intRed - 0.5
                intPurple = intPurple - 0.5
                intBlue = intBlue - 0.5
            End If
                
            If sngPainting = 110 Then
                intWhite = intWhite - 0.5
                intYellow = intYellow - 0.5
                intBlue = intBlue - 0.5
                intBlack = intBlack - 0.5
            End If
            
            'Advice regarding minimum inventory points
            If sngCanvases < 400 Then
                MsgBox "Reorder Canvases"
            End If
            
            If sngAprons < 400 Then
                MsgBox "Reorder Aprons"
            End If
            
            If intWhite < 128 Then
                MsgBox "Reorder White Paint"
            End If
            
            If intYellow < 128 Then
                MsgBox "Reorder Yellow Paint"
            End If
            
            If intOrange < 128 Then
                MsgBox "Reorder Orange Paint"
            End If
            
            If intRed < 128 Then
                MsgBox "Reorder Red Paint"
            End If
            
            If intPurple < 128 Then
                MsgBox "Reorder Purple Paint"
            End If
            
            If intBlue < 128 Then
                MsgBox "Reorder Blue Paint"
            End If
            
            If intGreen < 128 Then
                MsgBox "Reorder Green Paint"
            End If
            
            If intFlesh < 128 Then
                MsgBox "Reorder Flesh Paint"
            End If
            
            If intSienna < 128 Then
                MsgBox "Reorder Sienna Paint"
            End If
            
            If intBrown < 128 Then
                MsgBox "Reorder Brown Paint"
            End If
            
            If intBlack < 128 Then
                MsgBox "Reorder Black Paint"
            End If
        End If
    
    End Sub

  9. #9
    Forum Contributor
    Join Date
    08-22-2011
    Location
    Texas, United States
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    516

    Re: Message for invalid entry

    it looks like you are setting sngcustomers as single but you have not specified what it is, for example sngcustomers = Range("A2").value If the code doesn't know what sngcustomers it will not know what you are trying to do

    Are you setting this as a range in your workbook like i am guessing your are setting Customers

  10. #10
    Registered User
    Join Date
    04-24-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Message for invalid entry

    All of the ranges are defined in the workbook. Range B3 = Customers within my working spreadsheet page.

  11. #11
    Forum Contributor
    Join Date
    08-22-2011
    Location
    Texas, United States
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    516

    Re: Message for invalid entry

    i am just testing right now and this works for me.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Range("B3").Value = "" Or Range("B3") > 20 Then
    MsgBox "test"
    Range("B3").Value = ""
    Range("B3").Select
    End If
    End Sub
    This tells me that you are probably not taking in the value of the the range or something

  12. #12
    Registered User
    Join Date
    04-24-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Message for invalid entry

    I copied your code into Excel and it is still not working correctly. The message displays just the same way as mine had originally, but it does not force the user to remain on the original cell.

  13. #13
    Forum Contributor
    Join Date
    08-22-2011
    Location
    Texas, United States
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    516

    Re: Message for invalid entry

    Do you have this code in sheet 1 or do you have it somewhere else. What the code does for me if the user chooses a cell it check b3 to make sure it is not blank and <20 if so no error, and the user can move on else it gives a message and the user will never be able to get out of the loop unless B3 is <20. It will continue to give the error when the user moves out of the cell.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1