我对VBA非常陌生,我找不到答案和教程非常有用。
所以我想做的是检测下拉列表中选择的值是否已更改,并从那里将excel表的视图更改为相关单元格。
我的问题是很多样本VBA代码并没有真正附带解释。现在我拥有的是
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$A$1" Then
With Application
.EnableEvents = False
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
'SWITCH CASE CODE HERE
With Application
.EnableEvents = True
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
End If
End Sub
但是,我真的不知道EnableEvents
,ScreenUpdating
,Calculation
做了什么。
我想知道
提前致谢!
编辑:更新的代码仍无效...
Private Sub Worksheet_Change(ByVal Target As Range)
'Prints nothing
Debug.Print Target.Address
If Target.Address = "$A$1" Then
'Prints nothing
Debug.Print Target.Value
With Application
.EnableEvents = False
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
If Target.Value = "Goto B1" Then
ActiveWindow.ScrollColumn = B
ActiveWindow.ScrollRow = 1
End If
With Application
.EnableEvents = True
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
End If
End Sub
答案 0 :(得分:0)
您可以在电子表格中使用两种组合框;一个是表单元素,另一个是您可以使用数据验证 / 列表选项创建的。
如果组合是由数据验证形成的,那么即使我现在没有机会尝试,您的代码也会正常工作。
如果您的组合是表单元素,那么您可以参考以下答案:Combobox Change Event
我并不完全得到你需要的东西"切换视图"但如果您想从代码中找到另一个单元格,则可以使用Range("A1").Select
类型代码。
您也可以使用Excel的记录宏功能;在Excel中查看Excel为您的操作创建的代码。然后,您可以在实际代码中使用这些代码部分作为一种参考。
答案 1 :(得分:0)
好的,我找到了问题的解决方案。
假设下拉选择框位于单元格A1,
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
With Application
.EnableEvents = False
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
If Target.Value = "Goto B1" Then
ActiveWindow.ScrollColumn = 2 'Column B
ActiveWindow.ScrollRow = 1
End If
With Application
.EnableEvents = True
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
End If
End Sub