我試圖將工作簿中的引號名稱超鏈接到文件的位置。不是所有的文件都存在,我想測試一個布爾值來查看它們在創建超鏈接之前是否存在。我一直在使用活動單元格的文件名來保留一個字符串和搜索。命名約定是'XX-MMDDYY.XX。保存的文件名稱爲Quote_XX-MMDDYY.XX。我已將「Quote_」與活動單元格結合起來以搜索文件,但我的宏只似乎遍歷列表。結合ActiveCell.Value和字符搜索文件
Sub LoopRange()
Dim currRow As Integer, lastRow As Integer
Dim ws As String, quoteID As String
Dim path As String
Dim FileName As String
path = "C:\Some file path\"
ws = "Quote LOG"
currRow = 3
lastRow = Sheets(ws).Cells(Sheets(ws).Rows.Count, "A").End(xlUp).Row
While currRow <= lastRow
Sheets(ws).Cells(currRow, 1).Select
quoteID = "Quote_" & ActiveCell.value
FileName = path & quoteID
If Dir(FileName) <> "" And quoteID <> "" Then
Sheets(ws).Hyperlinks.Add anchor:=Cells(currRow, 2), Address:=FileName, TextToDisplay:=quoteID
End If
currRow = currRow + 1
Wend
End Sub
在路徑變量的末尾需要另一個反斜槓。 'path =「C:\ Some file path \」' –