VBA:将带有输入变量的超链接添加到选定单元格

VBA:将带有输入变量的超链接添加到选定单元格,vba,excel-2010,Vba,Excel 2010,我想将带有单元格目标的超链接添加到所选单元格。 例如,首先选择范围(“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) ActiveShee

我想将带有单元格目标的超链接添加到所选单元格。
例如,首先选择范围(“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

但是,这两个代码都报告了错误,如“Require object”

我想知道如何在这个超链接中使用输入的变量。添加“函数?

谢谢~

您想要的东西可以通过VBA中的两种输入框中的任何一种来实现

1:Application.inputbox()

2:Inputbox()

  • 使用application.inputbox并将其type=8设置为返回范围

  • 将范围指定给myvalue时,需要使用“Set”


  • 您所需要的可以通过VBA中的两种类型的inputbox中的任何一种来实现

    1:Application.inputbox()

    2:Inputbox()

  • 使用application.inputbox并将其type=8设置为返回范围

  • 将范围指定给myvalue时,需要使用“Set”


  • 在您的示例中,H2没有被引用为字符串,所以VBA认为它是可变的。请参见下面的示例,它可以执行您想要的操作,但您只能键入地址,也可以在当前工作簿的工作表之间键入地址,但不能像这样引用其他工作簿

    如果您想做一些不同的事情,record macro可能会帮助您。工作代码:

    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 
    

    在您的示例中,H2没有被引用为字符串,所以VBA认为它是可变的。请参见下面的示例,它可以执行您想要的操作,但您只能键入地址,也可以在当前工作簿的工作表之间键入地址,但不能像这样引用其他工作簿

    如果您想做一些不同的事情,record macro可能会帮助您。工作代码:

    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 
    
    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 
    
    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