Increment through range with VBA

Trebor8484

Board Regular
Joined
Oct 27, 2018
Messages
69
Office Version
  1. 2013
Platform
  1. Windows
Hi,

The code below is supposed to iterate through a range, so x starting from row 16 through to 25 and y to control the column increment from 5 to 13, i.e. E to M.

The line highlighted in red is incorrect, can someone advise how this should be written or a different approach please?

For reference, it is always columns E:M and rows 16 to 25.

Code:
For y = 5 To 13
        x = 16
        FailCount = 0
[COLOR=#ff0000]        For Each c In sht.Range(Cells(x), Cells(0, y))[/COLOR]
            If c.Value = "N" Then
                FailCount = FailCount + 1
            End If
        Next c
        If FailCount > 0 Then
            'rest of my code here
        End If
        x = x + 1
    Next y

Thanks
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Do you want to loop through all cells before checking "FailCount", or run some other code every time a cell = "N"?
 
Upvote 0
I need to record the value of FailCount for each row of data so basically each iteration of the y loop and reset it to zero between each column.

The rest of my code then summarises FailCount for each column.
 
Upvote 0
In that case, how about
Code:
   For y = 5 To 13
      failcount = 0
      For x = 16 To 25
         If Cells(x, y) = "N" Then failcount = failcount + 1
      Next x
      If failcount > 0 Then
      'rest of my code here
      End If
   Next y
 
Upvote 0
Couldn't you use Application.CountIf for each column?
 
Upvote 0
Thanks Fluff works great.

Norie, suppose I could have done that with individual statements for each column unless there is a more elegant way.
 
Upvote 0
Where are you putting the results?
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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