Loop through in same range every nth row

anand6752

New Member
Joined
Jul 27, 2022
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Hi , I have to select the number of blocks(Column B) mentioned in column C for each District (column A) in this sheet, the first one would be the row number specified in column D, then for others the code should step every 5 row in same range (row 2:row17) in 1st case.

Any help ?

This is what I have

Sub Block_list()

Dim i As Long

For i = 14 To 17 Step 5

'First Block
Worksheets("Sheet2").Cells(i + 1, 4).Value = i

Next
End Sub
 

Attachments

  • Block Select.png
    Block Select.png
    28.6 KB · Views: 20

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Welcome to the Board!

This line makes no sense.
For i = 14 To 17 Step 5
Basically, it is saying count from 14 to 17 by 5s.
The first loop would do 14, and the second would try to do 19, which is outside of the range.
So this loop would only ever do 14 and stop there (1 single loop and finish).

I cannot say that your explanation of what you are trying to do is quite clear to me.
Can you walk us through an example, based on your sample data posted?
 
Upvote 0
Welcome to the Board!

This line makes no sense.

Basically, it is saying count from 14 to 17 by 5s.
The first loop would do 14, and the second would try to do 19, which is outside of the range.
So this loop would only ever do 14 and stop there (1 single loop and finish).

I cannot say that your explanation of what you are trying to do is quite clear to me.
Can you walk us through an example, based on your sample data posted?
The issue is it should not step outside the range, the second would try to do 19 (i:e 14+5) but it should not step outside the range, it should count from 1 when range ends , (it should start counting from one again ), the range for the first loop is (A2:A17) , i have to start from 14 which is fixed & for the second loop it should count from A2 if range ends.
 
Upvote 0
The issue is it should not step outside the range, the second would try to do 19 (i:e 14+5) but it should not step outside the range, it should count from 1 when range ends , (it should start counting from one again ), the range for the first loop is (A2:A17) , i have to start from 14 which is fixed & for the second loop it should count from A2 if range ends.
It won't step outside the range - it will NOT go beyond 17.

Do you understand what the "Step 5" part means?
That tells it to count by 5s. So if you are starting at 14, counting by 5s would be 14, 19, 24, 29, etc.
But since you put an upper limit of 17, the only number that meets the criteria is 14.
So it would just do 14 and stop there.

If you wanted to count by 1s, you would need to remove the "Step 5" part.
If you have no "Step" part, it defaults to count by 1s (which would also be the same as "Step 1").

I am still not clear on what exactly it is you are trying to do. Your explanation is very confusing.
Which is why I asked you to walk us through an actual example, using your data.
If you show us exactly what it is you are trying to do, it may make more sense to us.
 
Upvote 0
Select Blocks.xlsx
ABCDEF
1District nameBlock NameNo.To be selectedStart RangeExpectedCount within the Specified range (A2:A17) by interval of 5 stating from 14th one.
2AGRAACCHNERA314
3AGRAAKOLA314
4AGRABAH3142nd one
5AGRABAROLI AHEER3145th one
6AGRABICHPURI314
7AGRAETMADPUR314
8AGRAFATEHABAD314
9AGRAFATEHPUR SIKRI3143rd one
10AGRAJAGNER314
11AGRAJAITPUR KALAN314
12AGRAKHANDOLI314
13AGRAKHERAGARH314
14AGRAM.C.AGRA CITY3144th one
15AGRAPINAHAT3141st one
16AGRASAIYAN314
17AGRASHAMSHABAD314
Sheet1
 
Upvote 0
Count within the Specified range (A2:A17) by interval of 5 stating from 14th one.
I am sorry, but this line seems to make no sense to me (and since no one else has responded, I think I am probably not the only one who doesn't understand it).
Remember, while your problem is familiar to you (because you know and understand exactly the logic behind what you want to do), it is not to us.
All that we know about it is the little that you have shared with us in this post.

I am looking for a detailed explanation, walking us through the logic behind what you want to happen, showing us each step of the way.
Walk us through each iteration of your loop, explaining (in plain English) the logic behind it and what should ultimately happen.
 
Upvote 0

Forum statistics

Threads
1,222,830
Messages
6,168,509
Members
452,194
Latest member
Lowie27

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