运行时错误1004,vba添加超链接

时间:2017-06-13 13:35:36

标签: excel vba excel-vba hyperlink runtime-error

有人可以在我需要的时候帮助我。我创建了一个userform,其hyperlink依赖于listbox的下拉列表。

尽管按下提交按钮时实际存在超链接,但我仍然收到错误消息

  

运行时错误1004.应用程序定义或对象定义错误。

当我调试ws.cells(iRow, 4)时突出显示的行

Private Sub Comm1_Click()

Dim iRow As Long
Dim ws As Worksheet
Dim ws2 As Worksheet
Dim rng As Range

Set ws = Worksheets("QttOutlay")
Set ws2 = Worksheets("LookupVals")

iRow = ws.Cells.Find(what:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

Set rng = ws.Cells(iRow)    

    ws.Cells(iRow, 2).Value = RmRef.Value
    ws.Cells(iRow, 3).Value = RetMod.Value
    ws.Cells(iRow, 4).Value = rng.Parent.Hyperlinks.Add(Anchor:=rng, Address:=WorksheetFunction.VLookup(RetMod.Value, ws2.Range("A:B"), 2, False), TextToDisplay:="Info")
    ws.Cells(iRow, 5).Value = OrdCod.Value
    ws.Cells(iRow, 6).Value = hmm.Value
    ws.Cells(iRow, 7).Value = lmm.Value
    ws.Cells(iRow, 8).Value = rdtype.Value
    ws.Cells(iRow, 9).Value = dtt.Value
    ws.Cells(iRow, 10).Value = Wtt.Value
    ws.Cells(iRow, 11).Value = Qt.Value
    ws.Cells(iRow, 12).Value = LPc.Value
    ws.Cells(iRow, 13).Value = Dt.Value
    ws.Cells(iRow, 14).Value = (LPc.Value * Dt.Value) * Qt.Value

End Sub

1 个答案:

答案 0 :(得分:2)

  1. Hyperlinks.Add Method返回您尝试分配给单元格值的超链接对象:ws.Cells(iRow, 4).Value = rng.Parent.Hyperlinks.Add(…)。这不会奏效。

  2. 我想ws.Cells(iRow, 4)是超链接的锚点,如:Anchor:=ws.Cells(iRow, 4)

  3. 所以而不是

    ws.Cells(iRow, 4).Value = rng.Parent.Hyperlinks.Add(Anchor:=rng, Address:=WorksheetFunction.VLookup(RetMod.Value, ws2.Range("A:B"), 2, False), TextToDisplay:="Info")
    

    你应该用这样的东西替换整行

    ws.Hyperlinks.Add Anchor:=ws.Cells(iRow, 4), Address:=WorksheetFunction.VLookup(RetMod.Value, ws2.Range("A:B"), 2, False), TextToDisplay:="Info"