我的一些工作表顶部有一个导航栏,想要在整个工作簿中应用导航代码,而不是必须在每个工作表中粘贴代码。它的工作原理是单击一个单元格,然后调用导航宏。我有以下代码(片段)功能,但肯定必须有一个更有效的方法来做到这一点,而不是粘贴在每张表中:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Navigation bar
If Not Intersect(Target, Range("E2:I3")) Is Nothing Then
Call goto_Introduction
End If
If Not Intersect(Target, Range("J2:N3")) Is Nothing Then
Call goto_OverviewInputs
End If
If Not Intersect(Target, Range("O2:S3")) Is Nothing Then
Call goto_PopulationSize
End If
如果之前有人问过,但到目前为止还没有找到任何解决方案,请道歉......
答案 0 :(得分:1)
您可以将逻辑转移到模块中:
'put this within a module
Option Explicit
Public Sub NavigationBar(ByVal Target As Range)
If Not Intersect(Target, Target.Parent.Range("E2:I3")) Is Nothing Then
goto_Introduction 'note that the call statement is deprecated
'and not needed to call a procedure!
End If
If Not Intersect(Target, Target.Parent.Range("J2:N3")) Is Nothing Then
goto_OverviewInputs
End If
If Not Intersect(Target, Target.Parent.Range("O2:S3")) Is Nothing Then
goto_PopulationSize
End If
End Sub
所以你只需要在每个工作表中插入对该逻辑的调用
'put this within the worksheets you need it
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
NavigationBar Target
End Sub