HelloKhritty
New Member
- Joined
- Jan 7, 2022
- Messages
- 9
- Office Version
- 2016
- 2013
- 2011
- 2010
- Platform
- Windows
Hi, having a hard time creating program in which I can use 2 targets in a worksheet,
The first one was successful, however, when I'm putting the second target, the code was not running or feels like vb is not reading my code.
my goal is when the target is in Ecell, and the dropdown was changed to "Redirect" Fcell will have the inputmessage "Please provide complete address" and when the dropdown is "send to Payee" the Fcell will have the inputmessage "This cell was disabled" or if possible the Fcell will be diabled and will not accept any input.
So the below codes are for the 1st one, and really running, I'll just need to insert a code for my second problem. hope you can help me. Really appreciate it.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Khrit
Application.EnableEvents = False
Dim aCell As Range
Dim RestrictedLength As Long
Dim ecell As Range
Dim Address As String
'~~> Check if the change happened in the range A2:A25
If Not Intersect(Target, Range("A2:A25")) Is Nothing Then
'~~> Check all changed cells (if applicable)
For Each aCell In Target
'~~> Decide the text length
Select Case aCell.Value
Case "UPS": RestrictedLength = 6
Case "FedEx": RestrictedLength = 9
End Select
'~~> Apply the validation
With Range("B" & aCell.Row).Validation
.Delete
.Add Type:=xlValidateTextLength, AlertStyle:=xlValidAlertStop, _
Operator:=xlEqual, Formula1:=RestrictedLength
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = "Check Length"
.InputMessage = "You can only enter a maximum of " & _
RestrictedLength & " characters only!"
.ErrorTitle = "Check #"
.ErrorMessage = "You can only enter a maximum of " & _
RestrictedLength & " characters only!"
.ShowInput = True
.ShowError = True
End With
Next aCell
End If
Letscontinue:
Application.EnableEvents = True
Exit Sub
Khrit:
MsgBox Err.Description
Resume Letscontinue
End Sub
The first one was successful, however, when I'm putting the second target, the code was not running or feels like vb is not reading my code.
my goal is when the target is in Ecell, and the dropdown was changed to "Redirect" Fcell will have the inputmessage "Please provide complete address" and when the dropdown is "send to Payee" the Fcell will have the inputmessage "This cell was disabled" or if possible the Fcell will be diabled and will not accept any input.
So the below codes are for the 1st one, and really running, I'll just need to insert a code for my second problem. hope you can help me. Really appreciate it.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Khrit
Application.EnableEvents = False
Dim aCell As Range
Dim RestrictedLength As Long
Dim ecell As Range
Dim Address As String
'~~> Check if the change happened in the range A2:A25
If Not Intersect(Target, Range("A2:A25")) Is Nothing Then
'~~> Check all changed cells (if applicable)
For Each aCell In Target
'~~> Decide the text length
Select Case aCell.Value
Case "UPS": RestrictedLength = 6
Case "FedEx": RestrictedLength = 9
End Select
'~~> Apply the validation
With Range("B" & aCell.Row).Validation
.Delete
.Add Type:=xlValidateTextLength, AlertStyle:=xlValidAlertStop, _
Operator:=xlEqual, Formula1:=RestrictedLength
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = "Check Length"
.InputMessage = "You can only enter a maximum of " & _
RestrictedLength & " characters only!"
.ErrorTitle = "Check #"
.ErrorMessage = "You can only enter a maximum of " & _
RestrictedLength & " characters only!"
.ShowInput = True
.ShowError = True
End With
Next aCell
End If
Letscontinue:
Application.EnableEvents = True
Exit Sub
Khrit:
MsgBox Err.Description
Resume Letscontinue
End Sub