我尝试应用代码以使单元格右侧的所有下拉菜单重置/更改为选择左侧的选项发生更改时,继续获取大小写无选择错误
我是VBA的新手和excel的宏观方面,就像我上面所说的那样,我试图让这段代码适用于整个工作表,我想在选择下拉选项时这样做,如果去返回并更改一个元素未来元素(右边的所有内容)更改回“选择”选项。
我在网上发现了一些代码,并试图改变它以满足我的需要,但每当我尝试运行它时我得到的情况没有大小写错误。 我正在使用的代码示例如下:
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
任何帮助都会很棒。这是我在添加选择案例时遇到问题的一段代码。
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
答案 0 :(得分:1)
Select语句的语法是:
Select Case (expression)
Case <case element>
Case ...
....
Case Else
End Select
在您的情况下,您的第一个Select Case (expression)
之前没有Case "=SubList"
。
答案 1 :(得分:0)
我发现了您的代码问题,请看下面的内容:
'~~~~~~~~~~~~ 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 ~~~~~~~~
只需删除额外的End If
即可。