VBA迭代循环

我需要突出显示每行中超过列AU中该行数据的单元格; 然后为下一行做这个,但是我不能得到列AU的值是迭代的,意味着改变到下一行的值。

让我知道我在这里做错了什么。

 Sub highlight() Application.ScreenUpdating = False Dim myRange As Range Dim i As Long, j As Long Set myRange = Range("F11:AP20") For n = 11 To 20 For i = 1 To myRange.Rows.Count For j = 1 To myRange.Columns.Count If myRange.Cells(i, j).Value < Range(AUn).Value Then myRange.Cells(i, j).Interior.Color = 65535 Else myRange.Cells(i, j).Interior.ColorIndex = 2 End If Next j Next i Next n End Sub 

  1. 你有1个额外的循环
  2. AUn被视为空白variables。 我想你想在Col AU的相关行上工作。

这是你正在尝试? ( 未经testing

我已经评论了代码,所以让我知道如果你仍然有任何疑问:)

 Sub highlight() Application.ScreenUpdating = False Dim myRange As Range Dim n As Long, j As Long Dim ws As Worksheet '~~> Change this to the relevant worksheet Set ws = ThisWorkbook.Sheets("Sheet1") With ws '~~> Set your range Set myRange = .Range("F11:AP20") '~~> Loop through the rows (11 to 20 in this case) For n = myRange.Row To (myRange.Row + myRange.Rows.Count - 1) '~~> Loop through the columns (F to AP in this case) For j = myRange.Column To (myRange.Column + myRange.Columns.Count - 1) If .Cells(n, j).Value < .Range("AU" & n).Value Then .Cells(n, j).Interior.Color = 65535 Else .Cells(n, j).Interior.ColorIndex = 2 End If Next j Next n End With End Sub