Solved

Add a substring to an existing hyperlink address using VBA

Posted on 2013-11-15
3
343 Views
Last Modified: 2013-11-18
Dear Experts:

I would like to run a macro that performs the following task

Loop thru all the cells in Column F.
... As soon as the macro hits a grey shaded cell (RGB 250, 250, 250) ...
... go three cells to the left (Column C) ...
... and add the string '_copy' at the end of the stored hyperlink (but before the file extension)  that is stored in that cell

For example:

First grey cell in F 147
The hyperlink in C 147 has the following address : C:\myfolder\47-338-44-09.tif
The new hyperlink address after running the macro will be: C:\myfolder\47-338-44-09_copy.tif
That is all of the stored hyperlinks have the following make-up
C:\myfolder\##-###-##-##.tif

After running the macro the hyperlink addresses will say:
C:\myfolder\##-###-##-##_copy.tif

Help is much appreciated.

Thank you very much in advance.

Regards, Andreas
0
Comment
Question by:AndreasHermle
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
Michael earned 500 total points
ID: 39651465
Hi Andreas,

the following code is one approach to do this:

Sub addToHyperlink()
    Dim rng As Range, cell As Range
    Dim lr As Integer
    Dim strHyperlink As String
    
    lr = Cells(Rows.Count, 3).End(xlUp).Row
    Set rng = Range(Cells(1, 6), Cells(lr, 6))
    
    For Each cell In rng
        If cell.Interior.Color = RGB(250, 250, 250) Then
            strHyperlink = cell.Offset(, -3).Value
            strHyperlink = Mid(strHyperlink, 1, Len(strHyperlink) - 4) & "_copy" & Right(strHyperlink, 4)
            cell.Offset(, -3).Value = strHyperlink
        End If
    Next
End Sub

Open in new window


I hope this works for you. It assumes that every corresponding cell in column C of a grey cell in column F, contains a hyperlink. If not, it will throw in an error.

Remember to test the code on a copy of your workbook.

Joop
0
 

Author Comment

by:AndreasHermle
ID: 39654473
Hi Joop,

thank you very much for your swift help. I will test the code tomorrow and let you know.

Again, thank you very much for your professional help.

Regards, Andreas
0
 

Author Closing Comment

by:AndreasHermle
ID: 39656497
Hi Joop,

works like a charm, thank you very much for your great and professional support.

Regards, Andreas
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now