[英]Identifying and highlighting a blank row in Excel VBA
場景:每行包含23列; 20個將包含用戶填充的數據,最后3個將通過vba自動生成。 在運行時,如果vba代碼將任何行的前20列標識為空白單元格,則將整行聲明為空白並突出顯示。
我已經能夠編寫以下代碼:
For Each rng In Range("A1:A" & LastRow)
With rng
If .Value < 1 Then
MsgBox "Blank Cell found"
blnkcnt = 0
For Each mycl In Range("A" & ActiveCell.Row & ":T" & ActiveCell.Row)
With mycl
If .Value < 1 Then
blnkcnt = blnkcnt + 1
End If
End With
Next mycl
If blnkcnt = 20 Then
lCount = lCount + 1
With .EntireRow.Interior
.ColorIndex = 6
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
End If
End If
End With
Next rng
If lCount > 0 Then
MsgBox "Data contains blank row(s): " & lCount
End
Else
MsgBox "No blank Rows"
End If
不要使用它。 使用CountA
檢查在那20列中是否有任何數據。
看到這個( 未經測試 )
Dim ws As Worksheet
Dim i As Long
'~~> Change this to the relevant worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> Find lastrow and change accordingly
'~~> For demonstration purpose using a hard coded value
lastrow = 10
For i = 1 To lastrow
'~~> Use CountA to check if there is any data
If Application.WorksheetFunction.CountA(.Range("A" & i & ":T" & i)) = 0 Then
MsgBox "Row " & i & " is blank"
'
'~~> Rest os your code
'
End If
Next i
End With
我在每行的前20列中使用了COUNTBLANK函數來確定是否存在任何空白單元格。
Dim rng As Range, lCount As Long, LastRow As Long
With ActiveSheet 'set this worksheet properly!
LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
For Each rng In .Range("A1:A" & LastRow)
With rng.Resize(1, 20)
If Application.CountBlank(.Cells) = 20 Then 'All 20 cells are blank
lCount = lCount + 1
.EntireRow.ClearContents
.EntireRow.Interior.ColorIndex = 6
End If
End With
Next rng
End With
If lCount > 0 Then
MsgBox "Data contains blank row(s): " & lCount
Else
MsgBox "No blank Rows"
End If
如果所有20個單元格均為空白,則將整行設置為空白,並應用黃色突出顯示。
我正在使用COUNTBLANK函數 ,因為尚不清楚公式是否返回零長度的字符串。 COUNTBLANK將把這些視為空白。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.