VBA: 将带有输入变量的超链接添加到所选单元格
VBA: add a hyperlink with inputted variable to a selected cell
我想将带有单元格目标的超链接添加到 selected 单元格。
比如,先是select range("H3"),然后出来一个输入框,我输入“2”,VBA程序会插入一个超链接,链接到单元格” H2" 到单元格 "H3".
我试过如下代码:
Sub test1()
Dim myValue As Variant
myValue = InputBox("Input the cell that you want to link to!", "Please input", H2)
ActiveSheet.Range.Hyperlinks.Add Anchor:=Selection, Address:=Sheets("Selected_Formated").Range("F" & myValue.Value)
End Sub
或
Sub test2()
Dim myValue As Variant
myValue = InputBox("Input the cell that you want to link to!", "Please input", H2)
ActiveSheet.Range.Hyperlinks.Add Anchor:=Range("H3"), SubAddress:="Selected_Formated!" & myValue.Value & ""
End Sub
但是,这两个代码都报告了错误,例如 "Require object".
我想知道如何在这个 "Hyperlinks.Add" 函数中使用输入的变量?
谢谢~
你想要的可以通过VBA中的两种输入框实现。
1: Application.inputbox()
Sub test1()
Dim myValue As Range
Set myValue = Application.InputBox("Input the cell that you want to link to!", Title:="Please input", Default:="H2", Type:=8)
ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:=myValue.Address
End Sub
2: 输入框()
Sub test2()
Dim myValue As Range, i As Integer
i = InputBox("Input the cell that you want to link to!", "Please input", 2)
Set myValue = ActiveSheet.Cells(i, ActiveCell.Column)
ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:=myValue.Address
End Sub
使用Applcation.inputbox并将其type=8设置为return一个范围。
为 myvalue 分配范围时需要使用 'Set'。
在您的示例中,H2 未作为字符串引用,因此 VBA 认为它是可变的。看我下面的例子它应该做你想做的,但你只能输入地址,你可以在当前工作簿的工作表之间输入地址,但你不能这样引用另一个工作簿。
如果您想做一些不同的事情,也可以录制宏。工作代码:
Dim xTargetRange As Range: Set xTargetRange = Selection 'here you could create function out of this or something
Dim sAnswer As String 'output needs to be string (or variant)
'dont forget to include name of sheet for between sheet referencing - when you dont include it it refers only to cells in current sheet.
sAnswer = InputBox("Input the cell that you want to link to!", "Please input", "'" & xTargetRange.Parent.Name & "'!H2")
'macro record showed that SubAddress is used for referencing within workbook (address is propably used for using URL to reference webpages etc.).
xTargetRange.Parent.Hyperlinks.Add Anchor:=xTargetRange, Address:="", SubAddress:=sAnswer, TextToDisplay:=sAnswer
我想将带有单元格目标的超链接添加到 selected 单元格。
比如,先是select range("H3"),然后出来一个输入框,我输入“2”,VBA程序会插入一个超链接,链接到单元格” H2" 到单元格 "H3".
我试过如下代码:
Sub test1()
Dim myValue As Variant
myValue = InputBox("Input the cell that you want to link to!", "Please input", H2)
ActiveSheet.Range.Hyperlinks.Add Anchor:=Selection, Address:=Sheets("Selected_Formated").Range("F" & myValue.Value)
End Sub
或
Sub test2()
Dim myValue As Variant
myValue = InputBox("Input the cell that you want to link to!", "Please input", H2)
ActiveSheet.Range.Hyperlinks.Add Anchor:=Range("H3"), SubAddress:="Selected_Formated!" & myValue.Value & ""
End Sub
但是,这两个代码都报告了错误,例如 "Require object".
我想知道如何在这个 "Hyperlinks.Add" 函数中使用输入的变量?
谢谢~
你想要的可以通过VBA中的两种输入框实现。
1: Application.inputbox()
Sub test1()
Dim myValue As Range
Set myValue = Application.InputBox("Input the cell that you want to link to!", Title:="Please input", Default:="H2", Type:=8)
ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:=myValue.Address
End Sub
2: 输入框()
Sub test2()
Dim myValue As Range, i As Integer
i = InputBox("Input the cell that you want to link to!", "Please input", 2)
Set myValue = ActiveSheet.Cells(i, ActiveCell.Column)
ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:=myValue.Address
End Sub
使用Applcation.inputbox并将其type=8设置为return一个范围。
为 myvalue 分配范围时需要使用 'Set'。
在您的示例中,H2 未作为字符串引用,因此 VBA 认为它是可变的。看我下面的例子它应该做你想做的,但你只能输入地址,你可以在当前工作簿的工作表之间输入地址,但你不能这样引用另一个工作簿。
如果您想做一些不同的事情,也可以录制宏。工作代码:
Dim xTargetRange As Range: Set xTargetRange = Selection 'here you could create function out of this or something
Dim sAnswer As String 'output needs to be string (or variant)
'dont forget to include name of sheet for between sheet referencing - when you dont include it it refers only to cells in current sheet.
sAnswer = InputBox("Input the cell that you want to link to!", "Please input", "'" & xTargetRange.Parent.Name & "'!H2")
'macro record showed that SubAddress is used for referencing within workbook (address is propably used for using URL to reference webpages etc.).
xTargetRange.Parent.Hyperlinks.Add Anchor:=xTargetRange, Address:="", SubAddress:=sAnswer, TextToDisplay:=sAnswer