4

I need help in Excel. My question is: How can I get the cell 42 of each row in this loop? As:

For Each r In Sheets("Sheet2").UsedRange.Rows
     sval = r.Cells(42)

     If sval = "" Then
         If r.Cells(6).Value <> "" And r.Cells(7).Value <> "" And r.Cells(9).Value <> "" And r.Cells(10).Value <> "" And r.Cells(11).Value <> "" And r.Cells(12).Value <> "" Then
             MsgBox "wtehi"
             r.EntireRow.Interior.ColorIndex = 0
         Else
             MsgBox "yallow"
             emptyMand = "ok"
             r.EntireRow.Interior.ColorIndex = 6
         End If
     End If

Next
2
  • What exactly do you mean ? Row 42 of every column ? Commented Dec 14, 2012 at 16:16
  • i wanna loop for every used rows in the sheet and want to get the cells and values of column 42 for every row as in as the above , i wanna to see the value of column is empty then wanna to see if the cell 6 7 9 10 11 12 are empty then make the color for the row white else make it yallow Commented Dec 14, 2012 at 16:25

2 Answers 2

4

To loop through all rows of worksheet ws and, for each row, get the cell on column 42, you can do this:

For Each rw in ws.UsedRange.Rows
    cell = ws.Cells(rw.Row, 42)
Next

However, the method below is twice as fast, and more readable:

For i = 1 to ws.UsedRange.Rows.Count
    cell = ws.Cells(i, 42)
Next
Sign up to request clarification or add additional context in comments.

2 Comments

cell variable is an object reference so Set statement must be used during assignment else it will throw this error. Know more about Set keyword here
@RBT If OP wants to reference a cell as Range object, then you're right, but he wanted to reference the value, so in this case cell = ws.Cells(i, 42).Value would have clarified it.
0

Another recommended non-macro option being of course to use conditional formatting, I'd suggest this for the macro part:

Dim cl As Range
For Each cl In Intersect(Sheets("Sheet2").UsedRange, Sheets("Sheet2").Columns(42))

     If Len(cl) = 0 Then
         If Application.WorksheetFunction.CountIf(Cells(cl.Row, 6).Resize(1, 5), "") <> 5 Then
             MsgBox "wtehi"
             cl.EntireRow.Interior.ColorIndex = 0
         Else
             MsgBox "yallow"
             emptyMand = "ok"
             cl.EntireRow.Interior.ColorIndex = 6
         End If
     End If

Next cl

1 Comment

solved, i knew how to get the cell of every cell in the loop by Cells(rowVariable.Row, 42). thanks for help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.