2015-07-20 46 views
0

我試圖將工作簿中的引號名稱超鏈接到文件的位置。不是所有的文件都存在,我想測試一個布爾值來查看它們在創建超鏈接之前是否存在。我一直在使用活動單元格的文件名來保留一個字符串和搜索。命名約定是'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 
+1

在路徑變量的末尾需要另一個反斜槓。 'path =「C:\ Some file path \」' –

回答

0

MacroMan的提示有幫助嗎?如果沒有,讓我知道,我會採取一個裂縫。

有一件事,雖然對VBA學習非常好,但如何(避免。激活和.Select)[How to avoid using Select in Excel VBA macros。我已經調整了代碼,考慮到這一點,它應該運行一些更流暢:

Sub LoopRange() 

Dim currRow As Integer, lastRow As Integer 
Dim ws As String, quoteID As String 
Dim path   As String 
Dim FileName  As String 
Dim cellValue  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 
    cellValue = Sheets(ws).Cells(currRow, 1) 
    'quoteID = "Quote_" & ActiveCell.Value 
    quoteID = "Quote_" & Sheets(ws).Cells(currRow, 1) 
    Debug.Print quoteID 
    FileName = path & quoteID & ".pdf" 
    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 
+1

是的,它確實定義了路徑。有一段時間,我只是讓它循環後, – PeaButter

+1

謝謝你的遠見不要使用活動單元格! – PeaButter

+0

@PeaButter - 謝謝,在啓動VBA時學習這是一件很棒的事情。此外,您可以跳過整個「cellValue」變量,因爲它不會在任何地方使用。我編輯它以顯示如何繞過'.select'和'.activate',但您也可以使用'quoteID =「Quote_」&cellValue'。否則,該變量可以被註釋/刪除。 – BruceWayne

0

我的文件名與代碼不匹配。我需要 「Quote_」 「.PDF」 添加到quoteID和文件類型,在這種情況下,以文件名

** 子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:\Users\pxbotr\Documents\Quote DataBase\" 

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 & ".pdf" 
    If Dir(FileName) <> "" And quoteID <> "" Then 
     Sheets(ws).Hyperlinks.Add anchor:=Cells(currRow, 1), Address:=FileName, TextToDisplay:=quoteID 
    End If 
    currRow = currRow + 1 
Wend 

結束子

**

+0

縮短代碼的一種方法是修改'ws =「quoteLog」',然後執行'dim ws As Worksheet'然後'Set ws = Sheets(「Quote LOG」)'然後在後面的代碼中只要做'lastRow = ws.cells(ws.rows.count,...'和'ws.Hyperlinks.add ...' – BruceWayne