Nested For Next Problem

kayza

Board Regular
Joined
Apr 29, 2015
Messages
61
Office Version
  1. 2007
Platform
  1. Windows
Hi.

I have a collection of data which consists of several items for which I will collect based on the criteria I have specified using the For Next statement.


and here is an example of the data :
[TABLE="class: grid, width: 200, align: left"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]User[/TD]
[TD]ID[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Jhon[/TD]
[TD]A[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]dean[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]billy[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]smith[/TD]
[TD]A[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]lovely[/TD]
[TD]A[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]amber[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]Lina[/TD]
[TD]A[/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD]joy[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]10[/TD]
[TD]...[/TD]
[TD]...[/TD]
[/TR]
</tbody>[/TABLE]




the results I want :
[TABLE="class: grid, width: 200, align: left"]
<tbody>[TR]
[TD][/TD]
[TD]D[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]dean[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]billy[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]amber[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]joy[/TD]
[/TR]
</tbody>[/TABLE]




and here is the rough code that I use, which results are not what I want
Code:
For a = 2 To 10
If Cells(a, 2) = "B" Then
    For b = 1 To 4 
    Cells(b, 4) = Cells(a, 1)
Next b
End If
Next a



thanks in advance for your help
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
For a = 2 To 10
If Cells(a, 2) = "B" Then
Cells(a, 4) = Cells(a, 1)
End If
Next a
 
Upvote 0
Hi.
thanks for the quick response.
after I use your code, the placement results in accordance with the search line.


and I'm afraid this will not work in my worksheet. and I also plan to use the second for next statement by using step
 
Upvote 0
Dim a As Long, b As Long, RowCount As Long


RowCount = 2


For a = 2 To 10
If Cells(a, 2) = "B" Then
Cells(RowCount, 4) = Cells(a, 1)
RowCount = RowCount + 1
End If
Next a
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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