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

任何帮助将不胜感激。干杯。

4

1 回答 1

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)
于 2013-04-22T22:36:33.330 回答