Nested for next loops not iterating the outside loop.

arooney88

Board Regular
Joined
Feb 17, 2014
Messages
61
I have a range of data from A2:A34 with various names in it that I need to copy to the range E9:E14. I only need to copy and paste unique names (I don't need a double of the same name). I am pretty sure using a nested For Next loop is the way to go but I'm having trouble getting the outer loop to go to the next iteration. Right now this is only giving me the last name in the in range A2:A34 repeated in E9:14. I was looking into using Exit For but when I added that in the code, the outer loop iterated but then the inner loop started over at 2.
Any help with this would be greatly appreciated. Thanks! Below is my code:

<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; white-space: inherit; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">
Rich (BB code):
 Sub FillTable()
Rich (BB code):
Dim tableCount As Integer
Dim rowCount As Integer

For tableCount = 9 To 13
If Range("E" & tableCount).Value = "" Then

For rowCount = 2 To 34
If Range("E" & tableCount).Value = Range("A" & rowCount).Value Then

ElseIf Range("E" & tableCount).Value <> Range("A" & rowCount).Value Then

Range("E" & tableCount).Value = Range("A" & rowCount).Value
End If
Next rowCount
End If
Next tableCount

End Sub
</code>
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
This uses the Advanced Filter feature to copy unique values.

Code:
    Range("A1:A34").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("E8"), Unique:=[COLOR=darkblue]True[/COLOR]

Note: It does include the header in the filtering and copying. That's why I copied to E8 instead of E9. The header could be cleared after the copy.
 
Upvote 0
This solution ended up working for me.

Code:
sub FillTable()

Dim tableCount As Integer
Dim rowCount As Integer
Dim n As Integer


'fill name column
n = 0
For rowCount = 2 To 34
  For tableCount = 9 To 9 + n
      If Range("E" & tableCount).Value = Range("A" & rowCount).Value Then
        ' name already found, break out of loop
        Exit For
      ElseIf Range("E" & tableCount).Value = "" Then
        Range("E" & tableCount).Value = Range("A" & rowCount).Value
        n = n + 1
      End If
  Next tableCount
Next rowCount

end sub
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,614
Members
449,090
Latest member
vivek chauhan

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