How to not allow an entry of '0' in a ComboBox

vbaNumpty

Board Regular
Joined
Apr 20, 2021
Messages
171
Office Version
  1. 365
Platform
  1. Windows
I have a combobox in a user form for quantities to be entered. The quantities can only be numbers through the use of the following keypress macro:
VBA Code:
Private Sub CaseQty_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

    If (KeyAscii > 47 And KeyAscii < 58) Then
        KeyAscii = KeyAscii
    Else
        KeyAscii = 0
        MsgBox "Please only use numbers for case quantities.", vbOKOnly + vbInformation, "Invalid Entry"
    End If

End Sub

However when it is being submitted, the entry can be '0' and still register. How can I prevent a value of zero from being accepted? I have tried the following but it doesn't work as the entry gets published to the workbook anyways:
VBA Code:
 ElseIf OrderEntry.CaseQty = 0 Then
        OrderEntry.CaseQty.Value = ""
        OrderEntry.CaseQty.SetFocus
        msgValue = MsgBox("Please enter a number greater than '0' for the Cases.", vbOKOnly + vbInformation, "Missing Information")
        Exit Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Change the 47 to 48. You could also write this line of code like this and it will also work...

=IF Chr(KeyAscii) Like "[1-9]" Then
I still need the ability to put a zero if the quantity is 10, 50, 100, or 150 for example. So removing the ability to type '0' isn't a possible solution.
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

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