将超链接添加到此VBA

时间:2019-03-30 13:31:54

标签: excel vba hyperlink directory

下面的VBA允许用户选择一个文件夹,然后完整路径显示在活动工作表的第1列中。

我如何修改这些路径以使其用作超链接?

ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make: *** [Project] Error 1

谢谢!

2 个答案:

答案 0 :(得分:0)

由于您的代码已经获取了完整的文件规范,因此我们可以使用数据来完成=HYPERLINK()公式:

Sub cmdList()
    Dim sPath   As String
    Dim fOut    As Variant
    Dim r       As Integer

    Dim Cell As Range

    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select directory"
        .InitialFileName = ThisWorkbook.Path & "\"
        .AllowMultiSelect = False
        If .Show = 0 Then Exit Sub
        sPath = .SelectedItems(1)
    End With
    fOut = Split(CreateObject("WScript.Shell").exec("cmd /c dir """ & sPath & """ /a:-h-s /b /s").StdOut.ReadAll, vbNewLine)
    r = 5
    Range(r & ":" & Rows.Count).Delete
   Cells(r, 1).Resize(UBound(fOut) + 1, 1).Value = WorksheetFunction.Transpose(fOut)

   '*************************************************************

   Dim dq As String,  rng As Range
   dq = Chr(34)

   Set Rng = Cells(r, 1).Resize(UBound(fOut) + 1, 1)
   For Each Cell In Rng
        Cell.Formula = "=HYPERLINK(" & dq & Cell.Value & dq & "," & dq & Cell.Value & dq & ")"
   Next Cell

End Sub

答案 1 :(得分:0)

在“ End sub”语法上方添加代码。以下代码会将Activecell值更改为超链接

ActiveSheet.Hyperlinks.Add Activecell,Activecell.Value

希望对您有帮助。