Reputation: 1
I keep on getting the Case without select case error while trying to apply code to make all drop downs to the right of a cell reset/change back to choose when an option on the left has changed.
I am very new to VBA and the macro side of excel and like I said above, I am trying to get this code to apply to the whole worksheet, i want to make it so when selecting drop down options, is if go back and change one element the future elements (everything to the right) changes back to the "Choose" option.
I found some code online and have tried to change it to suit my needs however whenever i try to run it i get the case without case error. Example of the code i am using is below:
Option Explicit
Const CHOOSE = "Choose"
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo ErrorHandler
Dim targetCell As Range
Dim nextCell As Range
Dim oldCalc As Excel.XlCalculation
If Not Intersect(Target, [DataEntryTable]) Is Nothing Then
If [Radio_Choice] = 1 Then
With Application
.EnableEvents = False
.ScreenUpdating = False
oldCalc = .Calculation
.Calculation = xlCalculationManual
End With
For Each targetCell In Target
'Clear any cells that use 'SubList' to the right of targetCell in the current table.
If targetCell.Column < (targetCell.ListObject.ListColumns.Count + targetCell.ListObject.Range.Column - 1) Then 'there are table cells to the right
For Each nextCell In targetCell.Offset(, 1).Resize(, targetCell.ListObject.ListColumns.Count + targetCell.ListObject.Range.Column - targetCell.Column - 1)
If HasValidationFormula(nextCell) Then
If nextCell.Validation.Formula1 = "=SubList" Then nextCell.Value = ""
End If
Next nextCell
End If
Select Case Target.Cells.Validation.Formula1
Case "=SubList"
If targetCell.Value = "" Then
targetCell.Value = CHOOSE
ElseIf targetCell.Offset(, -1).Value = CHOOSE Then
targetCell.Value = ""
ElseIf targetCell.Value = CHOOSE Then
'Do nothing
Else
Set nextCell = targetCell.Offset(, 1)
If HasValidationFormula(nextCell) Then
If nextCell.Validation.Formula1 = "=SubList" Then nextCell.Value = CHOOSE
End If
End If
End Select
End If
Next targetCell
With Application
.EnableEvents = True
.ScreenUpdating = True
.Calculation = oldCalc
End With
End If
End If
Exit Sub
ErrorHandler:
With Application
.EnableEvents = True
.ScreenUpdating = True
If oldCalc <> 0 Then .Calculation = oldCalc
End With
MsgBox Err.Description, vbCritical, Name & ".Worksheet_Change()"
End Sub
Private Function HasValidationFormula(cell As Range) As Boolean
On Error GoTo ValidationNotExistsError
If cell.Validation.Formula1 <> "" Then
HasValidationFormula = True
Else
HasValidationFormula = False
End If
Exit Function
ValidationNotExistsError:
HasValidationFormula = False
End Function
Any help would be great. This is the piece of code I am having trouble with even when adding the select case.
Select Case Target.Cells.Validation.Formula1
Case "=SubList"
If targetCell.Value = "" Then
targetCell.Value = Choose
ElseIf targetCell.Offset(, -1).Value = Choose Then
targetCell.Value = ""
ElseIf targetCell.Value = Choose Then
'Do nothing
Else
Set nextCell = targetCell.Offset(, 1)
If HasValidationFormula(nextCell) Then
If nextCell.Validation.Formula1 = "=SubList" Then nextCell.Value = Choose
End If
End If
End Select
Upvotes: 0
Views: 778
Reputation: 2526
I found the problem of your code, let see below:
'~~~~~~~~~~~~ above code ~~~~~~~~
If HasValidationFormula(nextCell) Then
If nextCell.Validation.Formula1 = "=SubList" Then nextCell.Value = CHOOSE
End If
End If
End Select
End If '<- this line is extra
'~~~~~~~~~~~~ below code ~~~~~~~~
Just remove the extra End If
, It will OK.
Upvotes: 0
Reputation: 25266
The syntax of the Select statement is:
Select Case (expression)
Case <case element>
Case ...
....
Case Else
End Select
In your case there is no Select Case (expression)
before your first Case "=SubList"
.
Upvotes: 1