Error saying "Next without For" but I have (I think) the proper # of each.

greenmonster15

Board Regular
Joined
Aug 28, 2012
Messages
70
This code is looping through a range and looking for any cells that contain hyperlinks and then doing some other stuff.

I keep getting the error "Next without For" when running my loop. You'd think this would be an easy error to find. Near the bottom of the code where is reads "Next TargetRow" is the line which the error is coming up on. Help! Does it have something to do with the data type I declared my variables and the values I assigned them maybe?

Code:
'Loop Through Range Looking For Part Hyperlinks    
    Dim StartRow As Long
    Dim EndRow As Long
    Dim StartCol As Long
    Dim EndCol As Long
    Dim TargetRow As Long
    Dim TargetCol As Long
    Dim HyperCell As Range
    
    
    StartRow = 20
    EndRow = 50
    StartCol = "E"
    EndCol = "J"
    HyperCell = "A100"
    
    Cells(StartRow, StartCol).Activate
    For TargetCol = StartCol To EndCol
        For TargetRow = StartRow To EndRow
            If ActiveCell.Hyperlinks.Count >= 1 Then
            HyperCell.Value = GetAddress(ActiveCell)
            
            strItem_Name = ActiveCell.Value
    
            '---------Now Finding First Blank Row in Main sheet for column = 1
            MainSheet.Activate
            Cells(1, 1).Activate
            Set Loc = Cells.Find("", ActiveCell, xlValues, xlWhole, xlByColumns)
            If Not (Loc Is Nothing) Then
                lngWriteRow = Loc.Row
            End If
            Cells(lngWriteRow, 1).Activate
            
            Cells(lngWriteRow, lngItem_Name).Value = strItem_Name
            Cells(lngWriteRow, lngItem_URL).Value = HyperCell.Value
        
            Else
        Next TargetRow
    Next TargetCol
 

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
You are missing
Code:
End If
above the
Code:
Next TargetRow
line
 
Upvote 0
StartCol and EndCol should be String the way you are using them and your assignment of HyperCell is incorrect:
Code:
'Loop Through Range Looking For Part Hyperlinks
    Dim StartRow              As Long
    Dim EndRow                As Long
    Dim StartCol              As String
    Dim EndCol                As String
    Dim TargetRow             As Long
    Dim TargetCol             As Long
    Dim HyperCell             As Range




    StartRow = 20
    EndRow = 50
    StartCol = "E"
    EndCol = "J"
    Set HyperCell = Range("A100")


    Cells(StartRow, StartCol).Activate
    For TargetCol = StartCol To EndCol
        For TargetRow = StartRow To EndRow
            If ActiveCell.Hyperlinks.Count >= 1 Then
                HyperCell.Value = GetAddress(ActiveCell)


                strItem_Name = ActiveCell.Value


                '---------Now Finding First Blank Row in Main sheet for column = 1
                MainSheet.Activate
                Cells(1, 1).Activate
                Set Loc = Cells.Find("", ActiveCell, xlValues, xlWhole, xlByColumns)
                If Not (Loc Is Nothing) Then
                    lngWriteRow = Loc.Row
                End If
                Cells(lngWriteRow, 1).Activate


                Cells(lngWriteRow, lngItem_Name).Value = strItem_Name
                Cells(lngWriteRow, lngItem_URL).Value = HyperCell.Value


            End If
        Next TargetRow
    Next TargetCol
 
Upvote 0
StartCol and EndCol should be String the way you are using them and your assignment of HyperCell is incorrect:
Code:
'Loop Through Range Looking For Part Hyperlinks
    Dim StartRow              As Long
    Dim EndRow                As Long
    Dim StartCol              As String
    Dim EndCol                As String
    Dim TargetRow             As Long
    Dim TargetCol             As Long
    Dim HyperCell             As Range




    StartRow = 20
    EndRow = 50
    StartCol = "E"
    EndCol = "J"
    Set HyperCell = Range("A100")


    Cells(StartRow, StartCol).Activate
    For TargetCol = StartCol To EndCol
        For TargetRow = StartRow To EndRow
            If ActiveCell.Hyperlinks.Count >= 1 Then
                HyperCell.Value = GetAddress(ActiveCell)


                strItem_Name = ActiveCell.Value


                '---------Now Finding First Blank Row in Main sheet for column = 1
                MainSheet.Activate
                Cells(1, 1).Activate
                Set Loc = Cells.Find("", ActiveCell, xlValues, xlWhole, xlByColumns)
                If Not (Loc Is Nothing) Then
                    lngWriteRow = Loc.Row
                End If
                Cells(lngWriteRow, 1).Activate


                Cells(lngWriteRow, lngItem_Name).Value = strItem_Name
                Cells(lngWriteRow, lngItem_URL).Value = HyperCell.Value


            End If
        Next TargetRow
    Next TargetCol

Thanks for the help. This is my first language I've tried and I'm still in week 1. I knew something didn't look right about the data types!!
 
Upvote 0

Forum statistics

Threads
1,217,141
Messages
6,134,863
Members
449,895
Latest member
rainbow1112

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
Back
Top