1. 程式人生 > >Excel VBA實現批量建立連結

Excel VBA實現批量建立連結

Sub link()
    Dim num, sheetname
    
    Worksheets(1).Select
    
    num = WorksheetFunction.CountA(Columns("c:c"))
    'MsgBox num
    
    For i = 2 To num
        '把第一個sheet中第3列第i行單元格的值賦值給sheetname,作為後面建立sheet時的名稱
        sheetname = Sheets(1).Cells(i, 3)
        
        '用單元格的值作為sheet名建立sheet
On Error Resume Next Worksheets.Add(after:=Worksheets(Worksheets.Count)).Name = sheetname '在新建的sheet中,給A1單元格輸入“返回”字串 Sheets(sheetname).Cells(1, 1) = "返回" '將新建的sheet中,返回字串所在單元格建立連結,連結地址是第一個sheet中第3列,第i行單元格 Sheets(sheetname).Hyperlinks.Add Anchor:=Sheets(sheetname).Cells(1
, 1), Address:="", SubAddress:= _ "彙總!C" & i 'MsgBox """" & sheetname & "!A2""" 'MsgBox Sheets(1).Cells(i, 3) '在第一個sheet中第3列,第i行新增連結,連結地址是第i個sheet的A1單元格 Sheets(1).Hyperlinks.Add Anchor:=Sheets(1).Cells(i, 3), Address:="", SubAddress:=sheetname & "
!A1" Next End Sub