2

I have the following code that extracts a hyperlink from a cell and then moves it to an adjacent cell. My goal is to be able to specify multiple columns and the exact column the cells should move to.

As you can see, the code is taking the hyperlink value from columns B1 to B2000 and copying the hyperlink value to column C (cll.Column + 1), but I would like to specify the exact column I want to copy the hyperlink to.

For example: I want all hyperlinks to copy from B to G; D to F; and E to H.

Sub hyper()
 Dim sht As Worksheet: Set sht = Worksheets("Sheet1")
 Dim cll As Range
 For Each cll In sht.Range("B2:B2000")
 If cll.Hyperlinks.Count > 0 Then
     sht.Cells(cll.Row, cll.Column + 1).Value = cll.Hyperlinks(1).Address
 End If
   Next cll
 msgbox "The macro has completed running"
 End Sub

I don't much about VBA macros so any help is appreciated. Thank you very much! ~Adam

1 Answer 1

2

Select Case can improve your code. Just make sure that you fix the Magic Numbers within as you wish:

Sub Hyper()
    Dim sht As Worksheet: Set sht = Worksheets("Sheet1")
    Dim cll As Range
    For Each cll In sht.Range("B2:H2000")
        If cll.Hyperlinks.Count > 0 Then
            Select Case cll.Column

            Case Range("A1").Column:
                sht.Cells(cll.Row, 15).Value = cll.Hyperlinks(1).Address
            Case Range("E1").Column
                sht.Cells(cll.Row, 20).Value = cll.Hyperlinks(1).Address
            Case Range("G1").Column
                sht.Cells(cll.Row, 25).Value = cll.Hyperlinks(1).Address

            End Select
        End If
    Next cll
    MsgBox "The macro has completed running"
End Sub
Sign up to request clarification or add additional context in comments.

6 Comments

Wow, this Is really great. It works perfect. Thank you so much!
quick question, is there a way to specify column AA instead of (cll.row, 27) for example? Just curious. thank you!
@Vityata - maybe I'm misunderstanding, but couldn't OP just use Cells(cll.Row, "AA") - i.e. the column index can be a number or string?
@Adam - take a look at the comment above.
"AA" worked. Thank you both for being rockstars, very very much appreciated!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.