我在某些工作表的顶部有一个导航栏,并希望在整个工作簿上应用导航代码,而不必在每个工作表中粘贴代码。它通过单击一个单元格,然后调用导航宏。我有以下代码(摘要)功能,但肯定必须有一种更有效的方法来执行此操作,而不是在每张表中粘贴:
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
很抱歉是否以前询问过,但到目前为止尚未找到任何解决方案...
您可以将逻辑传输到模块中:
'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