更新答案
更具挑战性,但绝对仍然可行!为了维护这些“随机数据字段”,我们将利用一个 Collection 对象来保存所有感兴趣的列号(即任何包含“计划”一词的列号)。
通过遍历第一行并构建Collection,我们稍后可以简单地遍历Collection并只查看重要的单元格。
下面被大量注释的代码应该可以解决问题。
Option Explicit
Public Sub DeleteBlankRowsRev2()
Dim wks As Worksheet
Dim lngLastRow As Long, lngLastCol As Long, lngIdx As Long
Dim varColItem As Variant
Dim blnAllBlank As Boolean
Dim col As Collection
Set col = New Collection
'First things first: we identify our basic variables
Set wks = ThisWorkbook.Worksheets("hello")
With wks
'Now that our sheet is defined, we'll find the last row and last column
lngLastRow = .Cells.Find(What:="*", LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
lngLastCol = .Cells.Find(What:="*", LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
'Awesome! With the last row and column in our pocket, we now
'know a lot about the data range we will be checking for blanks
'Here we loop through the first row, adding any column number
'that contains the word "plan"
For lngIdx = 1 To lngLastCol
If InStr(UCase(CStr(.Cells(1, lngIdx))), "PLAN") Then
col.Add lngIdx
End If
Next lngIdx
'Since we need to delete rows, we start from the bottom and move up
For lngIdx = lngLastRow To 1 Step -1
'Start by setting a flag to immediately stop checking
'if a cell is NOT blank
blnAllBlank = True
'Loop through all the column numbers contained in our collection
For Each varColItem In col
'If the cell is NOT blank, change the flag to False
'then exit this For loop
If .Cells(lngIdx, CInt(varColItem)) <> "" Then
blnAllBlank = False
End If
If Not blnAllBlank Then Exit For
Next varColItem
'Delete the row if the blnBlank variable is True
If blnAllBlank Then
.Rows(lngIdx).Delete
End If
Next lngIdx
End With
'That's it -- we're done!
MsgBox "Script complete!"
End Sub
原答案
下面的大量注释代码应该可以解决问题。
简而言之,我们:
- 确定计划数据的范围
- 循环遍历范围 BACKWARDS
- 对于每一行,如果上面 #1 中定义的空间中的每个单元格都是空的,则删除该行
别忘了庆祝!
Option Explicit
Public Sub DeleteBlankRows()
Dim wks As Worksheet
Dim lngLastRow As Long, lngLastCol As Long, lngIdx As Long, _
lngColCounter As Long
Dim blnAllBlank As Boolean
'First things first: we identify our basic variables
Set wks = ThisWorkbook.Worksheets("hello")
With wks
'Now that our sheet is defined, we'll find the last row and last column
lngLastRow = .Cells.Find(What:="*", LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
lngLastCol = .Cells.Find(What:="*", LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
'Awesome! With the last row and column in our pocket, we can
'loop over the full range (whether it only has 2 columns or 4)
'Since we need to delete rows, we start from the bottom and move up
For lngIdx = lngLastRow To 1 Step -1
'Start by setting a flag to immediately stop checking
'if a cell is NOT blank and initializing the column counter
blnAllBlank = True
lngColCounter = 2
'Check cells from left to right while the flag is True
'and the we are within the farthest-right column
While blnAllBlank And lngColCounter <= lngLastCol
'If the cell is NOT blank, trip the flag and exit the loop
If .Cells(lngIdx, lngColCounter) <> "" Then
blnAllBlank = False
Else
lngColCounter = lngColCounter + 1
End If
Wend
'Delete the row if the blnBlank variable is True
If blnAllBlank Then
.Rows(lngIdx).Delete
End If
Next lngIdx
End With
'That's it -- we're done!
MsgBox "Script complete!"
End Sub