自动创建指向新单元格的超链接
create hyperlink to new cell automatically
我有一个代码可以创建指向新作品sheet 的超链接,该作品是在另一个名为 'Management' 的作品sheet 中创建的,位于单元格 'A6' 中。我如何编写代码以便每次创建新的 sheet 时,都会创建指向不同单元格的超链接?例如,为每个新 sheet 创建 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
您可以将单元格设置为您所经历的点击次数的永久计数器,并使用该值来确定单元格地址的数字部分。添加一些这样的代码将有助于 运行 计数器:
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)
每次您点击创建一个新的 sheet 时,您的计数器都会增加,您的单元格引用也会改变。
我有一个代码可以创建指向新作品sheet 的超链接,该作品是在另一个名为 'Management' 的作品sheet 中创建的,位于单元格 'A6' 中。我如何编写代码以便每次创建新的 sheet 时,都会创建指向不同单元格的超链接?例如,为每个新 sheet 创建 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
您可以将单元格设置为您所经历的点击次数的永久计数器,并使用该值来确定单元格地址的数字部分。添加一些这样的代码将有助于 运行 计数器:
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)
每次您点击创建一个新的 sheet 时,您的计数器都会增加,您的单元格引用也会改变。