+ Reply to Thread
Results 1 to 14 of 14

Trying to get Macro to apply to a set Range

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Trying to get Macro to apply to a set Range

    hi everyone,

    I have the following code I found online which seems to do what essentially need it to do with a couple of small adjustments:

    Sub Apply_Hyperlinks()
        Dim intCount As Integer
        Dim rngRange As Range
        Dim strTMP As String
        For Each rngRange In Selection
            If Trim(rngRange.Value) <> "" Then
                strTMP = rngRange.Text
                intCount = Application.Find("http://", strTMP, 1)
                rngRange.Hyperlinks.Add Anchor:=Cells(rngRange.Row, rngRange.Column), Address:=Mid(strTMP, intCount), ScreenTip:=Mid(strTMP, intCount), TextToDisplay:=Left(strTMP, intCount - 1)
            End If
        Next rngRange
    End Sub
    What I'm trying to do is have this code automatically applied to cell ranges K1:K3000 and M1:M3000. I've tried a dozen variations and it keep getting an error code 13 each time on the intCount=Application line. Any ideas?

    Thanks very much for your help.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Trying to get Macro to apply to a set Range

    If you want to find out where a substring is in a string then use InStr.
    intCount = InStr(strTMP, "http://")
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Re: Trying to get Macro to apply to a set Range

    Hi Norie,

    Thanks. The code appears to work fine when I do not try and set a specific range. Do you have any ideas how I could apply the two ranges i listed above to the code?

    Thanks!

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Trying to get Macro to apply to a set Range

    I don't see why it wouldn't work for specific range(s) provided you have selected them before running the code.

    You might try using Selection.Cells just to make sure the loop is going through cells.

  5. #5
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Re: Trying to get Macro to apply to a set Range

    Hi Norie,

    What I'm trying to do is have the range of cells pre-selected in the code itself rather than selecting the range manually and then running the code.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Trying to get Macro to apply to a set Range

    If you wanted the code to run on a specific range change Selection to refer to that specific range.
    
     For Each rngRange In Range("K1:K3000, M1:M3000")

  7. #7
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Re: Trying to get Macro to apply to a set Range

    Hi Norie,

    I tried that method, and I applied it again at your suggestion. I got error code 5: Invalid procedure call or argument and the following line highlighted:

    rngRange.Hyperlinks.Add Anchor:=Cells(rngRange.Row, rngRange.Column), Address:=Mid(strTMP, intCount), ScreenTip:=Mid(strTMP, intCount), TextToDisplay:=Left(strTMP, intCount - 1)
    Arg!!!!

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Trying to get Macro to apply to a set Range

    Did you add a check for intCount not being 0?

    It will be 0 if "http://" is not found in the cell.

  9. #9
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Re: Trying to get Macro to apply to a set Range

    Hi Norie,

    My VBA skills are quite basic. How would I add a check for intCount not being 0?

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Trying to get Macro to apply to a set Range

    Perhaps.
    Sub Apply_Hyperlinks()
    Dim intCount As Integer
    Dim rngRange As Range
    Dim strTMP As String
    
        For Each rngRange In Range("K1:K3000, M1:M3000")
    
            If Trim(rngRange.Value) <> "" Then
                strTMP = rngRange.Text
                intCount = InStr(strTMP, "http://")
                If intCount<> 0 Then
                    rngRange.Hyperlinks.Add Anchor:=Cells(rngRange.Row, rngRange.Column), Address:=Mid(strTMP, intCount), ScreenTip:=Mid(strTMP, intCount), TextToDisplay:=Left(strTMP, intCount - 1)
                End If
            End If
    
        Next rngRange
    
    End Sub

  11. #11
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Re: Trying to get Macro to apply to a set Range

    Hi Norie,

    Thanks for the suggestions. I tried it but got an error code 13 with the
    If Trim(rngRange.Value) <> "" Then
    line highlighted.

    Any thoughts?

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Trying to get Macro to apply to a set Range

    That's a problem with the data not the code.

  13. #13
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Re: Trying to get Macro to apply to a set Range

    Hi Norie,

    Well the contents of the cells in those ranges are all text strings or blank. There are no formula in any of the cells. In your opinion, what in particular might cause such an error if its the data and and not the code?
    Last edited by Groovicles; 07-24-2014 at 10:08 AM.

  14. #14
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Re: Trying to get Macro to apply to a set Range

    Figured it out. thanks for your input, Norie.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Apply macro only to a range of cells
    By aloconnor in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-10-2014, 09:04 AM
  2. [SOLVED] Apply Macro First UnprotectSheet After Apply Macro ProtectAgain Solved by : Sixthsense
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-02-2014, 05:32 AM
  3. [SOLVED] Apply macro to highlighted range
    By Ophi352 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-15-2013, 04:55 AM
  4. [SOLVED] Macro to apply Conditional Formatting to a range in every worksheet
    By Floydlevedale in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-15-2013, 01:53 AM
  5. dynamic range in macro to apply calculations
    By psrs0810 in forum Excel General
    Replies: 2
    Last Post: 11-09-2010, 08:59 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1