excel 2010 edit multiple hyperlinks VBA, macro

2019-09-16 17:55发布

问题:

...a simple question (for some :)

the goal is to change a part of hyperlinks in a range of cells

  • sheet 'Progress'
  • range 'd9:d38'

hyperlinks now (rc001 is in D9; rc002 in D10 and so on)

..\..\..\..\..\..\..\AppData\Roaming\Microsoft\Excel\VNHS2_Reconciliation_report_RC001.xlsx

hyperlinks to be

VNHS2_Reconciliation_report_RC001.xlsx (rc001 increasing by 1 up to 030)

anyone? pretty please :)

回答1:

This is a simple loop with basic string replacement. I define a base Hyperlink address:

Const sFile As String = "..\..\..\..\..\..\..\AppData\Roaming\Microsoft\Excel\VNHS2_Reconciliation_report_RC000.xlsx"

And then use the Replace method to change the 000 part to 001, 002, etc., for each cell.

Sub Foo()
Const sFile As String = "..\..\..\..\..\..\..\AppData\Roaming\Microsoft\Excel\VNHS2_Reconciliation_report_RC000.xlsx"
Dim rngLinks As Range: Set rngLinks = Range("D9:D38")
Dim cl As Range
Dim i As Integer

For Each cl In rngLinks
    i = i + 1
    cl.Hyperlinks.Add cl, Replace(sFile, "000", Format(i, "00#"))

Next



End Sub