Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Add a substring to an existing hyperlink address using VBA

Posted on 2013-11-15
3
Medium Priority
?
377 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 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

877 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