当文件移动到另一个文件夹时更新文件的超链接

Update hyperlink to a file when the file is moved to another folder

我有两个文件夹中的文件,它们在彼此之间手动移动。 Excel 文件在这两个文件夹中的 C 列中包含所有文件名的列表。

该代码为该列表中的这些文件名创建了一个 hyperlink。我可以 运行 只有一个文件夹 link 的代码。如果文件移动到其他文件夹,hyperlink 将不再起作用。

如何在代码中添加另一个文件路径以及我应该如何更新它。

Option Explicit

Private Sub CommandButton1_Click()
Dim cell As Range

For Each cell In Range(Range("C3"), Range("C3").End(xlDown))
    ActiveSheet.Hyperlinks.Add cell, "Y:\training\" & cell.Value & ".xlsm"
Next

End Sub

创建文件超链接

Option Explicit

Private Sub CommandButton1_Click()
    
    Const Extension As String = ".xlsm"
    Dim FolderPaths As Variant
    FolderPaths = Array( _
        "Y:\Training\", _
        "Y:\Sleeping\")
    
    Dim cell As Range
    Dim FolderPath As Variant
    Dim FilePath As String

    For Each cell In Range(Range("C3"), Range("C" & Rows.Count).End(xlUp))
        If Len(cell.Value) > 0 Then
            For Each FolderPath In FolderPaths
                FilePath = FolderPath & cell.Value & Extension
                If Dir(FilePath) <> "" Then
                    ActiveSheet.Hyperlinks.Add cell, FilePath
                    Exit For
                End If
            Next FolderPath
        End If
    Next

End Sub