自动创建新单元格的超链接

时间:2015-07-27 18:35:09

标签: vba hyperlink cell

我有一个代码,可以在另一个名为“管理”的工作表中创建一个新工作表的超链接,在单元格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

1 个答案:

答案 0 :(得分:1)

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

;

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

            Sub counter()

            Dim x As Range
            Set x = Range("A1")

                x = x + 1

            Range("A1").Value = x

            End Sub

每次点击制作新工作表时,您的计数器都会上升,您的单元格引用也会发生变化。