我能够用黄色突出显示空白单元格,但无法弄清楚如何也突出显示整个行。最终目标是使该行突出显示为黄色,空白单元格显示为红色。不知道一行中是否有多个红色/空白单元格是否会引起问题-我不想再次用黄色突出显示整个行,并冒着丢失红色单元格的风险。到目前为止,我所得到的并不多-它通过谷歌搜索和reddit拼凑而成,因此,如果这太草率或不需要多余的行,我深表歉意。除了我出于必要而提出的建议外,我不了解VBA。我一直在搜索论坛,但在这个特定问题上没有发现太多。我确实有其他要求-L&K列不是唯一需要检查的字段。我将需要检查重复的值,过期的日期,已知的不正确的值等,但是我认为一旦降低了基准,就可以自行调整和自定义更强大的宏。任何帮助,将不胜感激。
Sub StatusReportQA()
'delete cognos headers
Rows("1:4").Select
Selection.Delete Shift:=xlUp
Dim LR As Long, cell As Range, rng As Range
With Sheets("Page1_1")
LR = .Range("A" & Rows.Count).End(xlUp).Row
For Each cell In Range("K2:K" & LR)
If cell = vbNullString Then
cell.Interior.ColorIndex = 6
End If
Next cell
For Each cell In Range("L2:L" & LR)
If cell = vbNullString Then
cell.Interior.ColorIndex = 6
End If
Next cell
End With
End Sub
答案 0 :(得分:3)
答案 1 :(得分:2)
您可以执行以下操作:
Sub StatusReportQA()
Const MAX_COLS As Long = 20 'for example
Dim sht As Worksheet, rw As Long
Dim LR As Long, rngRed As Range, rngRow As Range
Set sht = Sheets("Page1_1")
sht.Rows("1:4").Delete Shift:=xlUp 'delete cognos headers
LR = sht.Range("A" & Rows.Count).End(xlUp).Row
For rw = 2 To LR
Set rngRed = Nothing 'reset range for problem cells
With sht.Rows(rw)
Set rngRow = .Cells(1).Resize(1, MAX_COLS) 'the row of data
'clear any previous fill
rngRow.Interior.ColorIndex = xlNone
'perform your checks
If Len(.Cells(1, "K").Value) = 0 Then BuildRange rngRed, .Cells(1, "K")
If Len(.Cells(1, "L").Value) = 0 Then BuildRange rngRed, .Cells(1, "L")
'done checking
If Not rngRed Is Nothing Then
'found some problems, so color the row and then the problem cells
rngRow.Interior.Color = vbYellow
rngRed.Interior.Color = vbRed
End If
End With
Next rw
End Sub
'utility sub for building a range
Sub BuildRange(ByRef rngTot As Range, rngAdd As Range)
If rngTot Is Nothing Then
Set rngTot = rngAdd
Else
Set rngTot = Application.Union(rngTot, rngAdd)
End If
End Sub
答案 2 :(得分:0)
Sub StatusReportQA()
Dim LR As Long
Dim cell As Range
Dim WS As Worksheet
'delete cognos headers
Rows("1:4").Delete Shift:=xlUp
Set WS = Worksheets("Page1_1")
LR = WS.UsedRange.SpecialCells(xlCellTypeLastCell).Row
For Each cell In Range("K2:K" & LR)
If cell = vbNullString Or cell.Offset(0, 1) = vbNullString Then
cell.EntireRow.Interior.ColorIndex = 6
If cell = vbNullString Then cell.Interior.ColorIndex = 3
If cell.Offset(0, 1) = vbNullString Then cell.Offset(0, 1).Interior.ColorIndex = 3
End If
Next cell
End Sub
上面是一些给出预期结果的代码。第一件事是,当使用excel中的对象(工作表,单元格,范围等)时,几乎不需要.Select
它们,并且如果可能的话,应避免选择它们
要获得理想的结果,您必须考虑事情发生的顺序。首先,您必须检查感兴趣的任一列中的单元格是否为空白If cell = vbNullString Or cell.Offset(0, 1) = vbNullString Then
,如果其中任何一个为空白,则使用cell.EntireRow
设置整个颜色以突出显示整个行索引值。然后检查哪个单元格为空并设置其颜色索引值