2013-04-22 113 views
0

我試圖自動創建圖片文件的超鏈接列表。我的工作表具有A列中列出的文件名,我希望將這些文件的超鏈接(保存在工作表的父文件夾中)更改爲B列。我是VBA的初學者,但是這應該相當簡單,但我無法找到一種方法來做到這一點。如何創建本地存儲文件的超鏈接列表?

我嘗試使用宏錄製和得到這個:

Sub Hyperlink() 
' 
' Hyperlink Macro 
' 
' Keyboard Shortcut: Ctrl+l 
' 
    ActiveCell.Offset(0, -1).Range("Table1[[#Headers],[ACTIVITY '#]]").Select 
    ActiveCell.FormulaR1C1 = "file(a)" 
    ActiveCell.Offset(0, 1).Range("Table1[[#Headers],[ACTIVITY '#]]").Select 
    ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:= _ 
     "..\file(a).JPG", TextToDisplay:="..\file(a).JPG" 
    ActiveCell.Offset(1, 0).Range("Table1[[#Headers],[ACTIVITY '#]]").Select 
End Sub 

任何幫助將是非常讚賞。乾杯。

回答

0

您可以遍歷單元格並創建超鏈接並引用包含數據的列。

Sub CreateJpgHyperLinks() 
Dim iRow, iCol As Integer 'row and column counters 

iRow = 1 'change to 2 if there are headers 
iCol = 1 'Column A 
    'this assumes there is data in all cells in column A 
    Do While ActiveSheet.Cells(iRow, iCol).Value <> "" 
     'set the link in column B and point it to the info in column A 
     ActiveSheet.Hyperlinks.Add Anchor:=ActiveSheet.Cells(iRow, iCol + 1), Address:=ActiveSheet.Cells(iRow, iCol).Value, _ 
     TextToDisplay:=ActiveSheet.Cells(iRow, iCol).Value 
     'move to the next row 
     iRow = iRow + 1 
    Loop 

End Sub 

要查找的文件夾,你可以使用下面

'get path to current workbook 
workbookPath = ActiveWorkbook.Path 
'find the last slash in the workbook path 
iLastFolderSlash = InStrRev(workbookPath, "\") 
'create the folder location by removing the last folder from the path 
jpgFolderPath = Left(workbookPath, iLastFolderSlash) 
+0

謝謝您的方法。我會給這個鏡頭。 – TrevorS 2013-04-23 15:56:25

+0

剛試過這個解決方案。有兩件事我想討論一下。 1)我想添加「.JPG」超鏈接和顯示名稱。 2)超鏈接需要鏈接到樹中上面的文件夾(例如\\照片\照片電子表格\ spreadsheet.xls ==>指向照片的超鏈接) 我希望這很清楚。再次感謝你的幫助。非常感激。 – TrevorS 2013-04-23 16:10:46

+0

您是否嘗試自行添加這些功能?我可以爲你做,但你什麼都不學。嘗試一下,如果遇到問題,請回復一下。 – Sorceri 2013-04-23 23:20:34

相關問題