简体   繁体   English

使用链接(而不是按钮)使用VBA还原到Excel中的上一个选项卡

[英]Using a link, not a button, to revert to previous tab in Excel using VBA

I have searched around for help but have been unsuccessful. 我到处寻找帮助,但没有成功。 I have found different codes to use a button on a worksheet to go back to the previous worksheet the user was on. 我发现不同的代码可以使用工作表上的按钮返回到用户所在的上一个工作表。 What the company I work for wants are links on the top of almost each worksheet to function as a navigation pane for the users. 我想要的公司想要的是几乎每个工作表顶部的链接,它们充当用户的导航窗格。 I have created links at the top of the necessary sheets which all work as expected. 我已经在所有必要工作表的顶部创建了链接,所有链接均按预期工作。 I originally added code to my workbook to use a back link (previous sheet user was on, not the sheet before the current sheet) at cell "A5" and it worked for awhile but as I've been progressing on other items within the workbook, the code stopped working. 我最初将代码添加到工作簿中,以使用“ A5”单元格中的反向链接(使用以前的工作表用户,而不是当前工作表之前的工作表),它工作了一段时间,但随着我在工作簿中进行其他工作, ,代码停止工作。 I have compared the code on the working workbook with the nonworking workbook and they're the same and I don't believe other code is causing it to malfunction. 我已经将正常工作簿上的代码与非正常工作簿上的代码进行了比较,它们是相同的,并且我不认为其他代码会导致它出现故障。 Looking at the code below, does anyone have a suggestion for me? 查看下面的代码,有人对我有建议吗?

On worksheets that will have a back button I have: 在将有一个后退按钮的工作表上,我有:

Option Explicit 

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 
 If Selection.Count = 1 Then
  If Not Intersect(Target, Range("A5")) Is Nothing Then 
   Call SelectLast 
  End If 
 End If
End Sub

In the ThisWorkbook object I have the following: 在ThisWorkbook对象中,我具有以下内容:

Private Sub Workbook_SheetDeactivate(ByVal Sh As Object) 
  LastSheet = Sh.Name 
End Sub

Finally, I have the following in it's own module: 最后,我在其自己的模块中具有以下内容:

Public LastSheet As String 
Sub SelectLast() 
  Application.Sheets(LastSheet).Select 
End Sub

Any assistance would be greatly appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

Add a workbook-level name "LastSheet": 添加工作簿级别的名称“ LastSheet”:

在此处输入图片说明

Then add this code in the ThisWorkbook module: 然后在ThisWorkbook模块中添加以下代码:

Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    'EDIT
    ThisWorkbook.Names("lastsheet").RefersToR1C1 = "='" & Sh.Name & "'!R1C1"
End Sub

For your "Previous sheet" hyperlinks use: 对于“上一页”超链接,请使用:

=HYPERLINK("#LastSheet","Previous sheet")

If you wanted to get fancy with your "back" link and show the destination sheet name in the link: 如果您想通过“后退”链接看上一眼,并在链接中显示目标工作表名称:

=HYPERLINK("#lastsheet",
            "<< back to - " & SUBSTITUTE(MID(CELL("address",lastsheet),1+
            FIND("]",CELL("address",lastsheet)),200),"!$A$1",""))

Craig, this should do the trick for you 克雷格,这应该帮你

I made a label look like a hyperlink & operate as one. 我使标签看起来像超链接,并作为一个超链接进行操作。 I have basically stored the name of the sheet in the label's captions and have the workbook select the sheet that matches that text 我基本上已经将工作表的名称存储在标签的标题中,并让工作簿选择与该文本匹配的工作表

Private Sub Label1_Click()
    Dim strLoc As String
    strLoc = Label1.Caption
    ThisWorkbook.Sheets(strLoc).Select
End Sub

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM