Checking every nth cell in named range

scoha

Active Member
Joined
Jun 15, 2005
Messages
428
I want to check every nth cell in a named range to see if it ="a"

If it does, i want to hide that column and next two columns and then keep going.

How do I use a "For Each c in MyRange" type line but looping through every nth cell and not each cell?
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
try to use

For each nth cell in MyRange or

for i=nth to lastrow...
try to do something like that
 
Upvote 0
Code:
  Dim oCell As Range
  Dim iCount As Long
  
  iCount = 0
  For Each oCell In Range("MyRange")
    iCount = iCount + 1
    If iCount Mod [COLOR=blue][B]7[/B][/COLOR] = [COLOR=red][B]1[/B][/COLOR] Then
     [COLOR=green] ' do something
[/COLOR]      MsgBox iCount
    End If
  Next oCell
Set the figure in blue to the interval you want to check - here 7 means every 7th cell - and the figure in red to where you want to start, so: 1 means check cells 1, 8, 15, etc; 2 means check cells 2, 9, 16, etc; 0 means check cells 7, 14, etc.

(Tested and working here.)
 
Upvote 0
Is your named range just a single row?

You could just skip to the appropriate cell each time. Something like this.
Rich (BB code):
Sub HideCols()
    Dim iCount As Long
    
    Application.ScreenUpdating = False
    With Range("MyRange")
        For iCount = 2 To .Columns.Count Step 3
            With .Cells(1, iCount)
                If .Value = "a" Then
                    .Resize(, 3).EntireColumn.Hidden = True
                End If
            End With
        Next iCount
    End With
    Application.ScreenUpdating = True
End Sub
Using the same idea as Ruddles, the red number indicates the first column of the named range to check and the blue number is how many columns to step across each time. So my code checks the 2nd, 5th, 8th etc columns in the range.
 
Upvote 0
...so simple when you know how!
Well in fact I didn't know how - I had to think about it for a moment.

When faced with creating loops to process data repetitively, the general technique is to work out where you need to start and end, and what the simplest pattern is for moving from one piece of data to the next. Once you've got those clear in your mind, the writing of the code often becomes a mechanical process.

I can't tell you how many times I've started a macro by typing exactly this:-
Code:
Sub xxx()
  
  Dim ws As Worksheet
  Dim iRow As Long
  Dim iLastRow As Long
  
  Set ws = ThisWorkbook.Sheets(1)
  iLastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
 
Upvote 0
Code:
  Dim oCell As Range
  Dim iCount As Long
  
  iCount = 0
  For Each oCell In Range("MyRange")
    iCount = iCount + 1
    If iCount Mod 7 = 1 Then
      ' do something
      MsgBox iCount
    End If
  Next oCell

I'm trying to use this to highlight cells with oCell.Interior.ColorIndex = 3

Is there a way to adapt this to highlight four cells, skip four cells, and repeat?

Thanks.
 
Upvote 0
So want you want to do is count in eights, with the first four cells of every eight highlighted and the last four cells of every eight not highlighted.

It would be something like this:-

Code:
  Dim iCount As Long
  
  For iCount = 1 To 100 Step 8
    Cells(iCount, "A").Resize(4, 1).Interior.ColorIndex = 3
  Next iCount

Of course you have to work out where to start and end instead of using 1 and 100, but if you try that code on a blank worksheet you'll see how it works.

Any probs, give me a shout.
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,792
Members
452,942
Latest member
VijayNewtoExcel

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