Userform Only Allowed Characters to be used in Column

SteynBS

Board Regular
Joined
Jun 27, 2022
Messages
111
Office Version
  1. 365
Platform
  1. Windows
Hi Guys,

I have a user from with an option button. When the option button is ticked and the user form is closed, the user can only use allowed characters in a selected column. This is a code I have, but it fails. Please help

Private Sub OptionButton4_Click()

Dim rng As Range
If Intersect(Target, Range("C:C")) Is Nothing Then
Application.ScreenUpdating = False
Application.EnableEvents = False
For Each rng In Intersect(Range("C2:C50000"), Target)
Select Case rng
Case "Y", "N", "N/A", ""
' OK
Case Else
rng.Value = "N"
End Select
Next rng
Application.EnableEvents = True
Application.ScreenUpdating = True
End If

End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Are you saying that the code you provided is NOT allowing users to enter anything but those characters OR you wanting the users to only be able to use those characters?

Either way, the code is not going to work as shown. TARGET is usually a range provided by an event in a sheet when a user either selects a new cell, double clicks a cell, or right clicks a cell.

Maybe you're wanting it to check the cells that have been selected prior to opening the form. In that case you would use SELECTION.

I'm lost at exactly what you need. Are you asking for selected cells to have Data Validation with those choices?

Jeff
 
Upvote 0
Are you saying that the code you provided is NOT allowing users to enter anything but those characters OR you wanting the users to only be able to use those characters?

Either way, the code is not going to work as shown. TARGET is usually a range provided by an event in a sheet when a user either selects a new cell, double clicks a cell, or right clicks a cell.

Maybe you're wanting it to check the cells that have been selected prior to opening the form. In that case you would use SELECTION.

I'm lost at exactly what you need. Are you asking for selected cells to have Data Validation with those choices?

Jeff
Hi,

This code does not do anything. Throws out an error.

When the option button on the user form is selected and the user form is closed, the user should only be able to apply the values "Y" "N" "N/A" in column C or in this case from C2 to C50000
 
Upvote 0
Maybe something like this?

VBA Code:
Sub OptionButton4_Click()
  
  With Range("C2:C5000").Validation
      .Delete
      .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
      xlBetween, Formula1:="Y,N,N/A"
      .IgnoreBlank = True
      .InCellDropdown = True
      .InputTitle = ""
      .ErrorTitle = ""
      .InputMessage = ""
      .ErrorMessage = ""
      .ShowInput = True
      .ShowError = True
  End With
End Sub
 
Upvote 0
Solution
Maybe something like this?

VBA Code:
Sub OptionButton4_Click()
 
  With Range("C2:C5000").Validation
      .Delete
      .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
      xlBetween, Formula1:="Y,N,N/A"
      .IgnoreBlank = True
      .InCellDropdown = True
      .InputTitle = ""
      .ErrorTitle = ""
      .InputMessage = ""
      .ErrorMessage = ""
      .ShowInput = True
      .ShowError = True
  End With
End Sub
Hello, Yes this is perfect, thank you so much.
 
Upvote 0
Hello, Yes this is perfect, thank you so much.

The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

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