在UserForm和指定的路径上使用文本框创建超链接

时间:2018-12-07 11:19:25

标签: excel vba hyperlink userform

我有一个表格供用户使用UserForm导入新的信息/数据。

我希望用户键入文档名称,因为文件未正确标记(我不太在乎),我希望文本框成为超链接的标签,而路径由用户选择他们将文件移到适当的文件夹后。

Private Sub cmd_ADD_Click()
    'Copy input values to table2.
Dim ws As Worksheet
Set ws = ActiveSheet
Dim tbl As ListObject
Set tbl = ws.ListObjects("Table2")
Dim newrow As ListRow
Set newrow = tbl.ListRows.Add
Dim Label17 As String
Dim txtDrawing As String

With newrow

此行本来可以工作

    .Range(13) = txtDrawings.Value

现在,当添加为超链接时,我需要该值。我试过了...

    .Range(13).Hyperlinks.Add(Anchor:=.Range(13), _
      Address:=Label17, _
      ScreenTip:="DRAWING", _
      TextToDisplay:=txtDrawings.Value)

还有这个...

    .Range(13) = .Hyperlinks.Add(Label17, Me.txtDrawings.Value)

End With

End Sub

1 个答案:

答案 0 :(得分:2)

这对我有用

Dim ws As Worksheet
Set ws = ActiveSheet

Dim tbl As ListObject
Set tbl = ws.ListObjects("Table1")

Dim newrow As ListRow
Set newrow = tbl.ListRows.Add

With newrow
    .Range(13).Hyperlinks.Add Anchor:=.Range(13), _
                              Address:=Label17.Caption, _
                              ScreenTip:="DRAWING", _
                              TextToDisplay:=txtDrawings.Value
End With
  1. 删除代码中Anchor:之前的括号。同时删除所有On Error Resume Next。现在尝试上面的
  2. 删除行Dim Label17 As StringDim txtDrawing As String。这些是控件。