Advice to alter current prolematic code on userform

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,832
Office Version
  1. 2007
Platform
  1. Windows
I have a userform where one of my Comboboxes must allow either of the following.
N/A from drop down selection or a 17 character value.

The user should be able to select N/A from the drop down & move on but at present the code sees it as not 17 characters so keeps popping up the Msgbox & even when i try to close the form the Msg keeps popping etc but after a while it does close.

If the user doesnt select the N/A then they must enter a 17 character value, Example JHMDG12345TE210258

Code currently in use for ComboBox9 BUT think possibly a new version is needed for it to work correctly
Thanks

VBA Code:
Private Sub ComboBox9_Change()
    ComboBox9 = UCase(ComboBox9)
End Sub
Private Function ExitFunc() As Integer

If Len(Me.ComboBox9.Value) < 17 Then
    MsgBox "VIN MUST BE 17 CHARACTERS IN LENGTH" & vbCr & vbCr & "CONTINUE TO EDIT THE VIN ?", vbYesNo + vbCritical
    
If vbYes Then
   ComboBox9.SetFocus
Else
   Unload McListForm
End If
End If
ComboBox9 = UCase(ComboBox9)

End Function
Private Sub ComboBox9_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Len(ComboBox9.Value) <> 17 Then
        Cancel = True
        MsgBox "VIN MUST BE 17 CHARACTERS IN LENGTH" & vbNewLine & vbNewLine & "PLEASE CHECK & TRY AGAIN", vbCritical, "VIN NUMBER LENGTH MESSAGE"
    
    End If
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Please try changing this line accordingly
Rich (BB code):
If Len(Me.ComboBox9.Value) < 17 and Me.ComboBox9.Value <> "N/A" Then
 
Upvote 0
Jeffrey,
If you want to use formatting, you must use the Rich code tags, not the VBA ones (formatting does not render properly within VBA code tags).
I updated your post for you.
 
Upvote 0
Please try changing this line accordingly
Rich (BB code):
If Len(Me.ComboBox9.Value) < 17 and Me.ComboBox9.Value <> "N/A" Then

Still i get the Msgbox pop up advising it must be 17 characters

EaseUS_2024_11_25_16_41_12.jpg
 
Upvote 0
There are two locations where you evaluate the length of the control. Change this:
If Len(ComboBox9.Value) <> 17 Then
To This:
If Len(ComboBox9.Value) <> 17 and ComboBox9.Value <> "N/A" Then
 
Upvote 0
ok ive changed it to the following.

Do i actually need both of them anyway ?

Rich (BB code):
Private Function ExitFunc() As Integer

If Len(Me.ComboBox9.Value) < 17 And Me.ComboBox9.Value <> "N/A" Then
    MsgBox "VIN MUST BE 17 CHARACTERS IN LENGTH" & vbCr & vbCr & "CONTINUE TO EDIT THE VIN ?", vbYesNo + vbCritical
    
If vbYes Then
   ComboBox9.SetFocus
Else
   Unload McListForm
End If
End If
ComboBox9 = UCase(ComboBox9)

End Function
Private Sub ComboBox9_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Len(ComboBox9.Value) <> 17 And ComboBox9.Value <> "N/A" Then
        Cancel = True
        MsgBox "VIN MUST BE 17 CHARACTERS IN LENGTH" & vbNewLine & vbNewLine & "PLEASE CHECK & TRY AGAIN", vbCritical, "VIN NUMBER LENGTH MESSAGE"
    
    End If
End Sub
 
Upvote 0
I mean im not sure why this one is even being used.

The other gives the user the option to edit value in combobox.

So is it ok to remove this one ?

VBA Code:
Private Sub ComboBox9_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Len(ComboBox9.Value) <> 17 And ComboBox9.Value <> "N/A" Then
        Cancel = True
        MsgBox "VIN MUST BE 17 CHARACTERS IN LENGTH" & vbNewLine & vbNewLine & "PLEASE CHECK & TRY AGAIN", vbCritical, "VIN NUMBER LENGTH MESSAGE"
    
    End If
End Sub
 
Upvote 0
It all depends if you want to check that they have the right value just as they leave the combobox or when they are closing the form.

When are you calling this: Private Function ExitFunc() As Integer
 
Upvote 0
All done thanks.
I kept the code that checks when the user leaves the Combobox Thanks
 
Upvote 0

Forum statistics

Threads
1,224,899
Messages
6,181,627
Members
453,058
Latest member
rmd0725

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