I am using Microsoft Excel 2013 for Windows. I am trying to set up a data validation using VBA code provided in a different thread ( Allow cell population according to other cell's contents ), but am running into issues.
I have a data validation list set up in Column E of the worksheet with list options of "Yes", "No", "N/A". If the user selects "No" or "N/A", I would like "N/A" to automatically appear in Column F and have the cell locked from editing. If the user selects "Yes", then I would like Column F to be unlocked and for the user to be able to enter a number (any number).
Below is the code I am using in VBA. When I run it, I am getting and error that highlights the line of code "If (Target = "No") Or (Target = "N/A") Then" and a box that says "Type Mismatch". Any suggestions? Thanks so much!
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Unprotect Password:="Your Password"
Application.EnableEvents = False
If Not Intersect(Range("E:E"), Target) Is Nothing Then
If (Target = "No") Or (Target = "N/A") Then
Target.Offset(0, 1).Validation.Delete
Target.Offset(0, 1) = "n/a"
Target.Offset(0, 1).Locked = True
Else
Target.Offset(0, 1).Locked = False
End If
End If
Application.EnableEvents = True
ActiveSheet.Protect Password:="Your Password"
End Sub
Your Target
is defined as a Range ( ByVal Target as Range
). Try replacing your if statements with If (Target.Value = "No") Or (Target.Value = "N/A") Then
edit: Hm, I don't get the error when just using Target
. If you put a line break at that line (with your cursor in the line, press F9), what happens when you change a value in column E? I suspect it has to do with the Data Validation being used.
Edit2: Hm, it works with my data validation too. Can you post some same data perhaps, or a screenshot of your column E (the relevant area)?
The only way I can reproduce your error is to change multiple values in column E with either a paste operation or by typing a value into a multiple select of cells and finishing with Ctrl + Enter↵ . The following will handle multiple changes.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("E:E"), Target) Is Nothing Then
On Error GoTo bm_safe_exit
Application.EnableEvents = False
Me.Unprotect Password:="Your Password"
Dim rng As Range
For Each rng In Intersect(Range("E:E"), Target)
Select Case LCase(rng.Value2)
Case "no", "n/a"
rng.Offset(0, 1).Validation.Delete
rng.Offset(0, 1) = "n/a"
rng.Offset(0, 1).Locked = True
Case "yes"
rng.Offset(0, 1).Locked = False
Intersect(Range("E:E"), Target).Cells(1).Offset(0, 1).Select
Case Else
'cannot be here. do nothing
End Select
Next rng
End If
bm_safe_exit:
If Not Application.EnableEvents Then
Me.Protect Password:="Your Password"
Application.EnableEvents = True
End If
End Sub
I've reorganized some lines in your code and made the unprotect and event disable process only if a change or changes is/are within column E. I also swapped out your If ...
statement for a Select Case statement as I find these more representative of the type of conditions you are using.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.