Sub error when condition not found

ddander54

Board Regular
Joined
Oct 18, 2012
Messages
66
I need some help with this Sub when the text condition isn't met inside the For loop. The Sub works perfectly if each cell in the column meets the condition and it also works on a column until the condition isn't met, then stops.
Based on what I have read, it needs something like IF, THEN, ELSE, but when I try that inside the For loop I keep getting errors.
Ideally, I'd like it to move on to the next cell if it can't find ">" in the cell, until it reaches the last row with data in the cell.
In this example the first column completed without stopping, the second column stopped after the first cell and the third column won't work at all.

PPM LinkImpact Assessment LinkROM Link
PPM LinkImpact Assessment Link#Value!
PPM Link#Value!#Value!
PPM Link<a href="http://blah/blah/Lists/IA/DispForm.aspx?ID=261">http://blah/blah/Lists/IA/DispForm.aspx?ID=261</a><a href="http://blah/blah/blah/small/50463%20XYZ%20%20Annual%20AntiFraud%20Verification%20Repo/1.%20Initiate/XYZ%2050463%20ROM%20-%20Annual%20foo%20Verification%20Report.xlsm">ROM Link</a>
PPM Link#Value!#Value!
PPM Link#Value!#Value!
PPM Link<a href="http://blah/blah/Lists/IA/DispForm.aspx?ID=581">http://blah/blah/Lists/IA/DispForm.aspx?ID=581</a><a href="http://blah/blah/blah/ABC/99999%20ABC%20%20eLTCAS%20foo%20Approval%20Letter%20Update/1.%20Initiate/ABC%2051110%20ROM%20-%20efoo%20Claim%20Approval%20Letter%20Update.xlsm">ROM Link</a>
PPM Link<a href="http://blah/blah/Lists/IA/DispForm.aspx?ID=261">http://blah/blah/Lists/IA/DispForm.aspx?ID=261</a><a href="http://blah/blah/blah/small/77777%20BBB%20%20Annual%20foo%20Verification%20Repo/1.%20Initiate/BBB%2050463%20ROM%20-%20Annual%20foo%20Verification%20Report.xlsm">ROM Link</a>
PPM Link<a href="http://blah/blah/Lists/IA/DispForm.aspx?ID=562">http://blah/blah/Lists/IA/DispForm.aspx?ID=562</a><a href="http://blah/blah/blah/DDD/51165%20DDD%20%20Active%20foo%20Listing%20RERUN%20%20was%20PP/1.%20Initiate/51165%20DDD%20Active%20foo%20Listing%20Rerun%20ROM%20(formerly%2050814).xlsm">51165 ROM </a>
PPM Link#Value!#Value!
PPM Link#Value!<a href="http://blah/blah/_layouts/15/listform.aspx?PageType=6&amp;ListId=%7B5E40835D%2D8C93%2D40C4%2DB6E9%2D7DCF62BCE046%7D&amp;ID=403">ROM Link</a>


VBA Code:
Option Explicit

Sub AddHyperlinksfrom_hRef_CellText()
'JBeaucaire  (11/9/2009)
Dim LR As Long, i As Long, Txt As String, UserCol As String

'Create InputBox, and Assign value returned by InputBox function to variable UserCol
UserCol = InputBox(Prompt:="Please enter a Column (A-Z)", title:="Create Excel VBA InputBox", Default:="Column value here")

LR = Range(UserCol & Rows.Count).End(xlUp).Row

    'Change Number value (2) to be the start row
    For i = 2 To LR
        Txt = Mid(Cells(i, UserCol), 10, InStr(Cells(i, UserCol), ">") - 11)
        ActiveSheet.Hyperlinks.Add _
            Anchor:=Cells(i, UserCol), _
            Address:=Txt, _
            ScreenTip:=Txt, _
            TextToDisplay:=Cells(1, UserCol).Text
            'TextToDisplay:=Cells(i, "A").Text
        
    Next i

End Sub

Thanks in advance,
Don
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
60,134
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
Sub AddHyperlinksfrom_hRef_CellText()
'JBeaucaire  (11/9/2009)
Dim LR As Long, i As Long, Txt As String, UserCol As String, x As Long

'Create InputBox, and Assign value returned by InputBox function to variable UserCol
UserCol = InputBox(Prompt:="Please enter a Column (A-Z)", Title:="Create Excel VBA InputBox", Default:="Column value here")

LR = Range(UserCol & Rows.Count).End(xlUp).Row

    'Change Number value (2) to be the start row
   For i = 2 To LR
      If Not IsError(Cells(i, UserCol)) Then
         x = InStr(Cells(i, UserCol), ">")
         If x > 11 Then
            Txt = Mid(Cells(i, UserCol), 10, x - 11)
            ActiveSheet.Hyperlinks.Add _
               Anchor:=Cells(i, UserCol), _
               Address:=Txt, _
               ScreenTip:=Txt, _
               TextToDisplay:=Cells(1, UserCol).Text
               'TextToDisplay:=Cells(i, "A").Text
         End If
      End If
   Next i

End Sub
 
Solution

ddander54

Board Regular
Joined
Oct 18, 2012
Messages
66
How about
VBA Code:
Sub AddHyperlinksfrom_hRef_CellText()
'JBeaucaire  (11/9/2009)
Dim LR As Long, i As Long, Txt As String, UserCol As String, x As Long

'Create InputBox, and Assign value returned by InputBox function to variable UserCol
UserCol = InputBox(Prompt:="Please enter a Column (A-Z)", Title:="Create Excel VBA InputBox", Default:="Column value here")

LR = Range(UserCol & Rows.Count).End(xlUp).Row

    'Change Number value (2) to be the start row
   For i = 2 To LR
      If Not IsError(Cells(i, UserCol)) Then
         x = InStr(Cells(i, UserCol), ">")
         If x > 11 Then
            Txt = Mid(Cells(i, UserCol), 10, x - 11)
            ActiveSheet.Hyperlinks.Add _
               Anchor:=Cells(i, UserCol), _
               Address:=Txt, _
               ScreenTip:=Txt, _
               TextToDisplay:=Cells(1, UserCol).Text
               'TextToDisplay:=Cells(i, "A").Text
         End If
      End If
   Next i

End Sub
Fluff,

Thanks for the 'fix'! It works perfectly!

Don
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
60,134
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Forum statistics

Threads
1,136,800
Messages
5,677,809
Members
419,721
Latest member
StuckInWork

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top