Consulting

Results 1 to 20 of 31

Thread: Get Underlying Hyperlinks Out of Friendly Names

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location

    Get Underlying Hyperlinks Out of Friendly Names

    Column A contains hyperlinks underneath other text.
    How can I pull the hyperlinks into column B?

    And then, what if I also wanted to trash the underlying hyperlinks in Column A?

    ~Anne Troy

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    maybe something like this??...

    Sub transfer()
    Dim rng As Range
        Set rng = Range("A1:A10")
        rng.Copy
        Range("B1:B10").PasteSpecial (xlPasteAll)
        With rng
        .Hyperlinks.Delete
        End With
    End Sub

    change ranges to suit. (if i understood correctly).

  3. #3
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    LOL!

    I'm hoping for this to be a future KB contribution. I can't have hard-coded cells! Hee hee. Sorry. I shoulda said so, Zack.

    Let's suppose that I want all cells from A2 and down the used range of column A.
    Last edited by Anne Troy; 05-25-2004 at 04:08 PM.
    ~Anne Troy

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    ok, how about this:

    Sub transfer()
    Dim rng As Range
        Set rng = Range("A2", Range("A2").End(xlDown))
        rng.Copy
        rng.Offset(0, 1).PasteSpecial (xlPasteAll)
        With rng
        .Hyperlinks.Delete
        End With
        Application.CutCopyMode = False
    End Sub

    better?

    or will there possibly be non-contiguous ranges wanted with this? if so, how about:

    Sub transfer()
    Dim rng As Range
        Set rng = Range("A2", Range("A65536").End(xlUp))
        rng.Copy
        rng.Offset(0, 1).PasteSpecial (xlPasteAll)
        With rng
        .Hyperlinks.Delete
        End With
        Application.CutCopyMode = False
    End Sub
    this better? or am i shootin blanks here?

  5. #5
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    I thought you had kids?


    Okay...bad, bad me.

    WORKED GREAT!!
    ~Anne Troy

  6. #6
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location

    Talking

    Quote Originally Posted by Dreamboat
    Okay...bad, bad me.
    damn straight...

  7. #7
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    more overhead on the forum
    I saw this question posted elsewhere, and wished we had it as a contribution at www.vbaexpress.com/kb, which is the primary purpose of this site, and will be implemented as soon as we have a submission form.

    I wanted this thread to result in a contribution by whoever...
    All we need to do is handle, say, any range in col A gets dumped over to the adjacent cells in B. Yes, it'd be nice if it can be in ANY col and get dumped over to the next column to the right.

    User interface is unnecessary at this point since it's not an addin or anything like that. However, is this something we should add to the World Tag Excel Addin?

    I can't test the code because I can't run it. If we're going to add it to the KB, then we need those instructions. I'm sure YOU all know how, but me don't!
    ~Anne Troy

  8. #8
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Yes, Mark, the UI and the functionality are different beasts - it was Anne's second post that I thought brought the UI into scope, and there is probably a bigger issue here to do with run instructions.

    If I were an ignorant user (some say I am, of course ) how could I take a KB solution and use it? Posting detailed instructions with every piece of code is way over the top; should there be some generic instructions somewhere so that, in an individual case it's sufficient to say something like - paste this code into a standard code module (or ThisWorkbook, or wherever), close the VBA, etc. - which can then be referred to?

    Anyway, in this instance, none of the code posted so far has separated out the underlying hyperlinks, so here's some code which does ..

    Private Sub transfer(ByRef rng As Range, ByRef Target As Range)
    Dim h As Hyperlink
    rng.Copy
    Target.PasteSpecial (xlPasteAll)
    Application.CutCopyMode = False
    With rng
        .Hyperlinks.Delete
    End With
    For Each h In Target.Hyperlinks
        h.TextToDisplay = h.Address & IIf(h.SubAddress = "", "", IIf(h.Address = "", "", " - ") & h.SubAddress)
    Next
    End Sub
    This could be used with something like ..

    Sub TestIt()
    ' Get SourecRange from User
    ' Get TargetRange from User
    transfer SourceRange, TargetRange
    End Sub

    where Getting the ranges from the User are (probably) other KB code routines.
    Last edited by Aussiebear; 04-30-2023 at 02:09 AM.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  9. #9
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Well, well, well - I didn't tag my second piece of 'code' but the system identified it as VBA - very clever!
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  10. #10
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Quote Originally Posted by TonyJollans
    Well, well, well - I didn't tag my second piece of 'code' but the system identified it as VBA - very clever!
    Not exactly...
    A little birdie happened to see it...
    ~Anne Troy

  11. #11
    VBAX Mentor XL-Dennis's Avatar
    Joined
    May 2004
    Location
    ?stersund, Sweden
    Posts
    499
    Location
    Hi all :hi

    Unlike other boards I like to see that we also include error-handling in the provided solutions, especially when they are supposed to be used by less skilled user.

    Therefore I made the following add-ons:

    Option Explicit
    Sub Do_It()
    Dim rnSource As Range, rnStart As Range, rnTarget as Range
    If ActiveSheet.ProtectContents = False Then
        If TypeName(Selection) = "Range" Then
            With Selection
                If .Columns.Count <> 1 Then
                    MsgBox "Please make sure that the selection only contains one column.", vbInformation
                    GoTo ExitHere
                ElseIf .Areas.Count <> 1 Then
                    MsgBox "Please make sure that the selection only covers one area.", vbInformation
                    GoTo ExitHere
                Else
                    Set rnSource = Selection
                End If
            End With
        Else
            MsgBox "You need to select a range before executing this procedure.", vbExclamation
            GoTo ExitHere
        End If
    Else
        MsgBox "You need to unprotect the worksheet before executing this procedure.", vbExclamation
        GoTo ExitHere
    End If
    On Error Resume Next
    Set rnStart = Application.InputBox( _
    Prompt:="Please select the first cell in the target range:", _
    Title:="Select targetrange", _
    Type:=8)
    On Error GoTo 0
    If rnStart Is Nothing Then
        GoTo ExitHere
    Else
        Set rnTarget = rnStart.Resize(rnSource.Rows.Count, 1)
        Transfer rnSource, rnTarget
    End If
    ExitHere:
    Exit Sub
    End Sub
    
    Private Sub Transfer(ByRef rng As Range, ByRef Target As Range)
    Application.ScreenUpdating = False
    rng.Copy
    Target.PasteSpecial (xlPasteAll)
    With rng
        .Hyperlinks.Delete
    End With
    With Application
        .CutCopyMode = False
        .ScreenUpdating = True
    End With
    End Sub
    Kind regards,
    Dennis
    Last edited by XL-Dennis; 05-27-2004 at 04:06 AM.
    Kind regards,
    Dennis

    ExcelKB | .NET & Excel | 2nd edition PED


  12. #12
    Quote Originally Posted by TonyJollans
    Yes, Mark, the UI and the functionality are different beasts - it was Anne's second post that I thought brought the UI into scope, and there is probably a bigger issue here to do with run instructions.
    Huh? Are you referring to the bit where Anne says she doesn't want the range hard-coded?

    I guess I agree with the last two comments from Tony and Dennis about environment and error trapping, but really, it all seems like overkill for a relatively simple post. Perhaps an example of these techniques in the KB would be easier to simply refer to than to answer every question with 30 lines of code.

  13. #13
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    I like the idea of always having full error handling, but it's going to put a load on the approvers.

    BTW, you need rnTarget As Range
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  14. #14
    VBAX Mentor XL-Dennis's Avatar
    Joined
    May 2004
    Location
    ?stersund, Sweden
    Posts
    499
    Location
    Tony,

    Thanks for the heads up - I added it in the original post

    Yes, I agree but I believe it should only be necessary for the KB-articles.

    Kind regards,
    Dennis
    Kind regards,
    Dennis

    ExcelKB | .NET & Excel | 2nd edition PED


  15. #15
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location

    Lightbulb

    In my book a procedure should never make incidental changes to the environment. I know this is not always possible in VBA, but with ScreenUpdating, I think your example should have ..

    Private Sub Transfer(ByRef rng As Range, ByRef Target As Range) 
    Dim CallerScreenUpdating as boolean
            CallerScreenUpdating = Application.ScreenUpdating
            Application.ScreenUpdating = False 
    ' rest of code 
    Application.ScreenUpdating = CallerScreenUpdating 
    End Sub
    Without this, the caller may have set it False only to find it True after calling a sub-routine which he wouldn't want.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  16. #16
    VBAX Mentor XL-Dennis's Avatar
    Joined
    May 2004
    Location
    ?stersund, Sweden
    Posts
    499
    Location
    G8 -

    Kind regards,
    Dennis
    Kind regards,
    Dennis

    ExcelKB | .NET & Excel | 2nd edition PED


  17. #17
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    This code, intended for the KB, would be utilitarian: once and done, most likely.
    It would be cool if the person didn't have to choose the range, but merely only needed to ensure that wherever their hyperlinks reside, then the cell to the right is blank and can be populated with the hyperlinks.

    I don't think we need to get into a big deal with a typical KB entry like this.

    However! Were we to implement this feature into the addin...that's a different story!
    ~Anne Troy

  18. #18
    VBAX Mentor XL-Dennis's Avatar
    Joined
    May 2004
    Location
    ?stersund, Sweden
    Posts
    499
    Location
    it all seems like overkill for a relatively simple post
    Yupp, but I like to see where we stand when it comes to some aspects for the KB-articles. A general write-up about error-handling is great but as I said I like to see some stuff different here.

    So let the comments come about it

    Kind regards,
    Dennis
    Kind regards,
    Dennis

    ExcelKB | .NET & Excel | 2nd edition PED


  19. #19
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    But not here, Den. Can we take this discussion where it belongs?

    http://www.vbaexpress.com/forum/showthread.php?t=66

    ~Anne Troy

  20. #20
    VBAX Regular
    Joined
    Jun 2004
    Location
    The Netherlands
    Posts
    34
    Location
    Hi Dreamboat,
    here another approach. If I read the question correctly then you want to have the hyperlink address in column B. Hereby I asume that the cell contains a value which is shown like "test". Underlying there is a link towards http://www.test.com. This last value you want to see in column B. IN this case you might think about using a macro like:


    Sub HyperlinkAddr()
    Application.ScreenUpdating = False
    rowsA = Cells(Rows.Count, 1).End(xlUp).row
    For i = 1 To rowsA
        If Cells(i, 1).Hyperlinks.Count > 0 Then
            Cells(i, 1).Offset(0, 1).value = Cells(i, 1).Hyperlinks(1).Address
        End If
    Next i
    Application.ScreenUpdating = True
    End Sub

    I hope this is what you needed.

    regards,
    Jeroen

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •