[英]hyperlink in visual basic excel
what i want in the code is that when i click this cell B3 in sheet1 it will direct me to A5 in sheet2 then vice versa, when i click A5 in sheet2 it will bring back me to B3 in sheet1 https://i.stack.imgur.com/qUngD.jpg我在代码中想要的是,当我单击 sheet1 中的此单元格 B3 时,它会将我定向到 sheet2 中的 A5,反之亦然,当我单击 sheet2 中的 A5 时,它会将我带回 sheet1 中的 B3 https://i.stack .imgur.com/qUngD.jpg
Sub Macro3()
'
' Macro3 Macro
'
'
Range("B3").Select
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
"Sheet2!A5", TextToDisplay:="gg"
Sheets("Sheet2").Select
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
"Sheet1!B3", TextToDisplay:="gg"
Sheets("Sheet1").Select
End Sub
now my problem is what if the name of the sheet is userdefined?现在我的问题是如果工作表的名称是用户定义的怎么办? there's a messagebox that says you should input the name of the sheet.
有一个消息框说您应该输入工作表的名称。 so the sub address now changes it is not just "Sheet2" anymore.
所以子地址现在改变了,它不再只是“Sheet2”了。 for instance you put in the msgbox "123", the name of the sheet would now become "Sheet2 123".
例如,您在 msgbox 中输入“123”,工作表的名称现在将变为“Sheet2 123”。
You should take advantage of indexing of worksheets collection.您应该利用工作表集合的索引。
Sub IndexingSheets()
Sheets(1).Range("B3").Formula = _
"=HYPERLINK(""#" & ThisWorkbook.Sheets(2).Name & "!A5"", ""TextToDisplay"")"
Sheets(2).Range("A5").Formula = _
"=HYPERLINK(""#" & ThisWorkbook.Sheets(1).Name & "!B3"", ""TextToDisplay"")"
End Sub
This code assumes your sheet1 and sheet2 are always the first two sheets in the workbook.此代码假定您的 sheet1 和 sheet2 始终是工作簿中的前两个工作表。
Dim sheetExist As Boolean Sub PrefNamedSheets() Dim shName$, i& shName = InputBox("Whats the second sheet name?") For i = 1 To Worksheets.Count If StrComp(CStr(Sheets(i).Name), shName, vbTextCompare) = 0 Then sheetExist = True End If Next i If sheetExist Then ActiveSheet.Range("B3").Formula = _ "=HYPERLINK(""#" & shName & "!A5"", ""TextToDisplay"")" Sheets(shName).Range("A5").Formula = _ "=HYPERLINK(""#" & ThisWorkbook.Sheets(1).Name & "!B3"", ""TextToDisplay"")" Else Call PrefNamedSheets End If End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.