【问题标题】:create hyperlink to new cell automatically自动创建指向新单元格的超链接
【发布时间】:2015-10-18 02:04:56
【问题描述】:

我有一个代码,它创建一个超链接到一个新工作表,该工作表是在另一个名为“管理”的工作表中的单元格“A6”中创建的。如何对其进行编码,以便每次创建新工作表时,都会创建指向不同单元格的超链接?例如,为每个新工作表创建 A7、A8、A9 等。

这是我目前的代码

Private Sub Button8_Click()

Dim newSheet As Worksheet
Dim newName As String

Do
newName = Application.InputBox("What do you want to name the new sheet?", Type:=2)
If newName = "False" Then Exit Sub: Rem cancel pressed

Set newSheet = ThisWorkbook.Sheets.Add

On Error Resume Next
    newSheet.Name = newName
    newName = Error
On Error GoTo 0

If newName <> vbNullString Then
    Application.DisplayAlerts = False
        newSheet.Delete
    Application.DisplayAlerts = True
    MsgBox newName
End If

Loop Until newName = vbNullString

ThisWorkbook.Worksheets("Version Checklist").Cells.Copy
newSheet.Paste

Dim targetSheet As Worksheet
Dim targetRange As Range
Dim linkedSheet As Worksheet
Dim linkRange As Range

'set variable to the sheet the hyperlink will link to
Set targetSheet = ThisWorkbook.Sheets(ActiveSheet.Name)

' specify the range on the summary sheet to link to
Set targetRange = targetSheet.Range("A1:Z100")

' set variable to sheet that will have the hyperlink
Set linkedSheet = ThisWorkbook.Sheets("Management")

' specify where on that sheet we'll create the hyperlink
Set linkRange = linkedSheet.Range("A6")

' create the hypperlink on the copied sheet pointing
' back to the summary sheet
linkedSheet.Hyperlinks.Add Anchor:=linkRange, Address:="", SubAddress:= _
    "'" & targetSheet.Name & "'!" & targetRange.Address, _
    TextToDisplay:=targetSheet.Name

End sub

【问题讨论】:

    标签: vba hyperlink cell


    【解决方案1】:

    您可以将单元格设置为您所经历的点击次数的永久计数器,并使用此值来确定单元格地址的数字部分。添加一些这样的代码将有助于运行计数器:

                Sub counter()
    
                Dim x As Range
                Set x = Range("A1")
    
                    x = x + 1
    
                Range("A1").Value = x
    
                End Sub
    

    然后你可以为超链接参考写这样的东西:

                Set linkRange = linkedSheet.Range("A" & x)
    

    每次您单击以制作新工作表时,您的计数器都会增加,并且您的单元格引用会发生变化。

    【讨论】:

      猜你喜欢
      • 2011-01-17
      • 1970-01-01
      • 2020-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多