2012-12-14 139 views
2

我需要Excel中的幫助。我的問題是:如何獲得此循環中每行的單元格42?正如:excel vba中的行上循環並獲取單元格

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 
+0

你究竟是什麼意思?每列第42行? – ApplePie

+0

我想循環工作表中的每一個使用的行,並希望獲得第42列的單元格和值爲上述的每一行,我想看到列的值是空的,然後想要看看單元格6 7 9 10 11 12是空的,然後使行的顏色爲白色,否則使yallow – samimvp

回答

0

另一個推薦的當然非宏一個選項是使用條件格式,我建議這對宏部分:通過工作ws和所有行

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 
+0

解決,我知道如何通過Cells(rowVariable.Row,42)得到循環中的每個單元格的單元格。感謝幫助 – samimvp

2

要循環,對於每一行,得到列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 
相關問題