4

我需要 Excel 帮助。我的问题是:如何在此循环中获取每行的单元格 42?作为:

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
4

2 回答 2

4

要遍历工作表的所有行,ws并为每一行获取第 42 列上的单元格,您可以执行以下操作:

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

但是,下面的方法速度是原来的两倍,而且可读性更强:

For i = 1 to ws.UsedRange.Rows.Count
    cell = ws.Cells(i, 42)
Next
于 2014-09-16T15:54:28.507 回答
0

另一个推荐的非宏选项当然是使用条件格式,我建议将这个用于宏部分:

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
于 2012-12-14T16:43:48.083 回答