Using Sheet.Cells Property with a Range

business_analyst

Board Regular
Joined
Jun 5, 2009
Messages
99
Hell All,

So I would like to look through a range of cells on a sheet to check if they have numbers in them. Basically it is a sheet with 4 columns (A through D). And 10 rows (2 through 11). I would like to loop through all four columns, and check that same row range for each column. The code below does this for the first row of each column. But how to I check all the rows for each column? I cannot insert a range into the sheet1.cells property can I? And I do not know how to use the range property with cell indexes. Any help would be greatly appreciated.

Code:
dim row as integer
dim col as integer

col = 1
row = 2

For col = 1 to 4
    If sheet1.cells(row, col) = 0 then
        Msgbox "The range is equal to 0"
    end if
next col
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
I think you are looking for a nested loop?

Code:
Sub foo()
 
    Dim lRow As Long
    Dim lCol As Long
 
    For lRow = 2 To 11
        For lCol = 1 To 4
 
            If Sheet1.Cells(lRow, lCol).Value = 0 Then
                MsgBox "The range is equal to 0"
            End If
 
        Next lCol
    Next lRow
 
End Sub
 
Upvote 0
Perhaps

Code:
Dim irow As Integer
Dim icol As Integer


For icol = 1 To 4
    For irow = 2 To 11
        If Sheet1.Cells(irow, icol).Value = 0 Then
            MsgBox Sheet1.Cells(irow, icol).Address(False, False) & " is equal to 0"
        End If
    Next irow
Next icol
 
Upvote 0
Thanks, a nested loop would make sense if I was looking for each individual cell, but I was actually trying to see if the sum of all the rows in each column are equal to 0. For example, with column A, if row 2 through row 10 is equal to 0 then the user would be prompted with a message. Essentially, they should be allowed to enter a number in at least one cell in each column. Sorry if I didn't clarify that originally.
 
Upvote 0
You could use

worksheetfunction.sum(cells(irow,icol).resize(10))

to calculate the sum of values in column icol.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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