Excel VBA - Selection.Hyperlinks(1).子地址更新倍数 rows/ranges
Excel VBA - Selection.Hyperlinks(1).SubAddress Updating Multiple rows/ranges
下面的 VBA 子例程 copies/inserts 电子表格中的新行,然后更新新行 (A2) 的第一个单元格中的超链接。嗯,这就是我想要它做的,但它也在更新被复制的行 (A3) 的超链接。我不知道为什么。
Sub New_Task()
Dim new_task_name As String
Dim new_ws As Worksheet
' Copy the new Tasks name
new_task_name = InputBox(Prompt:="New Task Name (no spaces)", Title:="New Task Name", Default:="new_task")
If new_task_name = "new_task" Or new_task_name = vbNullString Then
Exit Sub
End If
' Copy the current second row for the new task.
Rows("2:2").Select
Selection.Copy
Selection.Insert Shift:=xlDown
' Set the task name in Cell A2
Range("A2").Select
ActiveCell.Value = new_task_name
' Insert a new sheet.
Sheets("Template").Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = new_task_name
Range("A2").Select
ActiveCell.Value = Date
' Now we have the new worksheet, update the hyperlink to it.
Worksheets("Main").Activate
Range("A2").Select
' FIXME: the following line updates the hyperlink in A2 and A3.
Selection.Hyperlinks(1).SubAddress = "'" & new_task_name & "'" & "!A1"
End Sub
尝试在您的单元格中创建一个新的超链接,而不是更新被复制的单元格的 SubAddress
:
' Old...
Worksheets("Main").Activate
Range("A2").Select
' FIXME: the following line updates the hyperlink in A2 and A3.
Selection.Hyperlinks(1).SubAddress = "'" & new_task_name & "'" & "!A1"
' New...
Sheets("Main").Range("A2") = "=HYPERLINK(""#'" & new_task_name & "'!A1"")"
感谢 Bond,我手动删除了 'other' link (Ctrl+K)。完整答案,包括下面的友好名称。虽然我仍然不明白为什么原来的不起作用。
Sheets("Main").Range("A2") = "=HYPERLINK(""#'" & new_task_name & "'!A1"",""" & new_task_name & """ )"
下面的 VBA 子例程 copies/inserts 电子表格中的新行,然后更新新行 (A2) 的第一个单元格中的超链接。嗯,这就是我想要它做的,但它也在更新被复制的行 (A3) 的超链接。我不知道为什么。
Sub New_Task()
Dim new_task_name As String
Dim new_ws As Worksheet
' Copy the new Tasks name
new_task_name = InputBox(Prompt:="New Task Name (no spaces)", Title:="New Task Name", Default:="new_task")
If new_task_name = "new_task" Or new_task_name = vbNullString Then
Exit Sub
End If
' Copy the current second row for the new task.
Rows("2:2").Select
Selection.Copy
Selection.Insert Shift:=xlDown
' Set the task name in Cell A2
Range("A2").Select
ActiveCell.Value = new_task_name
' Insert a new sheet.
Sheets("Template").Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = new_task_name
Range("A2").Select
ActiveCell.Value = Date
' Now we have the new worksheet, update the hyperlink to it.
Worksheets("Main").Activate
Range("A2").Select
' FIXME: the following line updates the hyperlink in A2 and A3.
Selection.Hyperlinks(1).SubAddress = "'" & new_task_name & "'" & "!A1"
End Sub
尝试在您的单元格中创建一个新的超链接,而不是更新被复制的单元格的 SubAddress
:
' Old...
Worksheets("Main").Activate
Range("A2").Select
' FIXME: the following line updates the hyperlink in A2 and A3.
Selection.Hyperlinks(1).SubAddress = "'" & new_task_name & "'" & "!A1"
' New...
Sheets("Main").Range("A2") = "=HYPERLINK(""#'" & new_task_name & "'!A1"")"
感谢 Bond,我手动删除了 'other' link (Ctrl+K)。完整答案,包括下面的友好名称。虽然我仍然不明白为什么原来的不起作用。
Sheets("Main").Range("A2") = "=HYPERLINK(""#'" & new_task_name & "'!A1"",""" & new_task_name & """ )"