MissingInAction
Board Regular
- Joined
- Sep 20, 2019
- Messages
- 85
- Office Version
- 365
- Platform
- Windows
Hi everyone. I'm busy creating an application form for our company that will change based on the selections a user makes. I have VBA code that the MrExcel community helped me with a while ago that works fine with hiding/showing tick boxes.
The first selection the user makes is to pick from a dropdown what type of form he wants, namely "new account", "change account" or "termination of account". If new or change is selected, VBA shows a type mismatch error and highlights
That is only a part of the code in that line. The full line is
How can I fix this? I'm still very new to VBA. Here is the full code:
Thank you
EDIT: Just to add, the code works perfectly. Everything does what it is suppose to do. I just cant figure out why I'm getting the error.
The first selection the user makes is to pick from a dropdown what type of form he wants, namely "new account", "change account" or "termination of account". If new or change is selected, VBA shows a type mismatch error and highlights
VBA Code:
If UCase(Target.Value) = "YES" Then
VBA Code:
If UCase(Target.Value) = "YES" Then Vis = True Else Vis = False
How can I fix this? I'm still very new to VBA. Here is the full code:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Vis As Boolean
If Target.Address = "$B$7" Then
Application.ScreenUpdating = False
' ActiveSheet.Unprotect "abc123"
If ActiveSheet.Range("B7").Value = "New Account" Then
Range("C14:E14").Select
' Performance Contract dropdown
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=Data!$G$2:$G$3"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = "Select ""Yes"" if the employee does not need any form of IT/phone/alarm code access, otherwise select ""No""."
.ErrorMessage = "Please select Yes or No"
.ShowInput = True
.ShowError = True
End With
Range("D7:E7,B15,B26,D9").Select
Range("B26").Activate
Selection.Locked = True
Selection.FormulaHidden = False
Range("B9:B12,D10:D11").Select
Range("D11").Activate
Selection.Locked = False
Selection.FormulaHidden = False
Selection.ClearContents
Range("B8").Select
Else
If ActiveSheet.Range("B7").Value = "Termination of Account" Then
' ActiveSheet.Unprotect "abc123"
' Application.ScreenUpdating = False
Range("B9:B12,D7:E7,D9:E11,C14:E14").Select
Range("C14:E14").Activate
Selection.Locked = False
Selection.FormulaHidden = False
Range("D9").Select
Selection.Locked = False
Selection.FormulaHidden = False
Range("B9").Select
ActiveCell.FormulaR1C1 = _
"=IF(R8C2="""","""",IF(R7C2=""Termination of Account"",XLOOKUP(R8C2,'User Data'!C6,'User Data'!C7,""Unknown Employee"")))"
Range("B10").Select
ActiveCell.FormulaR1C1 = _
"=IF(R8C2="""","""",IF(R7C2=""Termination of Account"",XLOOKUP(R8C2,'User Data'!C6,'User Data'!C2,""Unknown Employee"")))"
Range("B11").Select
ActiveCell.FormulaR1C1 = _
"=IF(R8C2="""","""",IF(R7C2=""Termination of Account"",XLOOKUP(R8C2,'User Data'!C6,'User Data'!C8,""Unknown Employee"")))"
Range("B12:B13").Select
ActiveCell.FormulaR1C1 = _
"=IF(R8C2="""","""",IF(R7C2=""Termination of Account"",XLOOKUP(R8C2,'User Data'!C6,'User Data'!C9,""Unknown Employee"")))"
Range("D9").Select
ActiveCell.Formula2R1C1 = _
"=IF(R8C2="""","""",IF(R9C3<>"""",IF(XLOOKUP(R8C2,'User Data'!C6,'User Data'!C11,"""")=0,"""",XLOOKUP(R8C2,'User Data'!C6,'User Data'!C11,"""")),""""))"
Range("D10:E10").Select
ActiveCell.FormulaR1C1 = _
"=IF(R8C2="""","""",IF(R7C2=""Termination of Account"",XLOOKUP(R8C2,'User Data'!C6,'User Data'!C14,""Unknown Employee"")))"
Range("D11:E11").Select
ActiveCell.FormulaR1C1 = _
"=IF(R8C2="""","""",IF(R7C2=""Termination of Account"",XLOOKUP(R8C2,'User Data'!C6,'User Data'!C12,""Unknown Employee"")))"
Range("B9:B13,D10:E11").Select
Range("D10:E11").Activate
Selection.Locked = True
Selection.FormulaHidden = False
Range("B8").Select
Else
If ActiveSheet.Range("B7").Value = "Change Account" Then
' ActiveSheet.Unprotect "abc123"
' Application.ScreenUpdating = False
Range("B9:B12,D9:D11,C14:E14").Select
Range("C14").Activate
Selection.Locked = False
Selection.FormulaHidden = False
Selection.ClearContents
Range("B9").Select
ActiveCell.FormulaR1C1 = _
"=IF(R8C2="""","""",IF(R7C2=""Change Account"",XLOOKUP(R8C2,'User Data'!C6,'User Data'!C7,""Unknown Employee"")))"
Range("B10").Select
ActiveCell.FormulaR1C1 = _
"=IF(R8C2="""","""",IF(R7C2=""Change Account"",XLOOKUP(R8C2,'User Data'!C6,'User Data'!C2,""Unknown Employee"")))"
Range("B11").Select
ActiveCell.FormulaR1C1 = _
"=IF(R8C2="""","""",IF(R7C2=""Change Account"",XLOOKUP(R8C2,'User Data'!C6,'User Data'!C8,""Unknown Employee"")))"
Range("B9:B11").Select
Range("B9:B11").Activate
Selection.Locked = True
Selection.FormulaHidden = False
Range("B8").Select
Else
Range("B9:B12,D9:E11").Select
Range("D10").Activate
Selection.Locked = False
Selection.FormulaHidden = False
Selection.ClearContents
Range("B8").Select
End If
End If
Range("C14:E14").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
:=xlBetween
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Range("B8").Select
End If
' ActiveSheet.Protect "abc123"
' ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Application.ScreenUpdating = True
End If
If Target.Address = "$B$9" Then
Application.ScreenUpdating = False
ActiveSheet.Unprotect "abc123"
If ActiveSheet.Range("B9").Value = "Contract" Or ActiveSheet.Range("B9").Value = "Councillor" Then
Range("D9:E9").Select
Range("D9").Activate
Selection.Locked = False
Selection.FormulaHidden = False
Selection.ClearContents
Range("B10").Select
Else
Range("D9:E9").Select
Range("D9").Activate
Selection.Locked = True
Selection.FormulaHidden = False
Range("B10").Select
End If
' ActiveSheet.Protect "abc123"
' ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Application.ScreenUpdating = True
End If
If UCase(Target.Value) = "YES" Then Vis = True Else Vis = False
' Performance Assist
If Target.Address = "$B$165" Then
HiLiteShapes "Perf_", Vis
End If
' Eunomia
If Target.Address = "$D$91" Then
HiLiteShapes "Eunomia", Vis
End If
' Alarm Code & Building Keys
If Target.Address = "$B$61" Then
HiLiteShapes "Alarm", Vis
End If
' IT Equipment
If Target.Address = "$E$55" Then
HiLiteShapes "ITEquip", Vis
End If
' Risk Management
If Target.Address = "$B$91" Then
HiLiteShapes "RiskMan", Vis
End If
' SDBIP
If Target.Address = "$B$152" Then
HiLiteShapes "SDBIPAdmin", Vis
End If
End Sub
Sub HiLiteShapes(MyStr As String, IsVis As Boolean)
Dim MyShape As Shape
For Each MyShape In ActiveSheet.Shapes
If Left(MyShape.Name, Len(MyStr)) = MyStr Then
MyShape.Visible = IsVis
End If
Next MyShape
End Sub
Sub RiskMan_Action_Owner_Change()
' Risk Management - Action Owner
HiLiteShapes "RiskOwn", Range("G144")
End Sub
Sub SDBIPAdmin_KPI_Owner_Change()
' SDBIP - KPI Owner
HiLiteShapes "SDBIPOwn", Range("G156")
End Sub
Thank you
EDIT: Just to add, the code works perfectly. Everything does what it is suppose to do. I just cant figure out why I'm getting the error.