这是我的电子表格的示例格式。我尝试编写用于从第(2)行删除行到第一个空白单元格的代码,但是当数据较少时存在问题(意味着第20-30行)。这不能正常工作&删除所有行。但是当数据超过30行时,它正在工作。
A栏 row1公司名称删除 row2地址行删除 row3地址行删除 row4地址行删除 row5地址行删除 row6地址行删除 row7客户名称保留此信息 row8地址行删除 row9地址行删除 row10地址行删除 row11地址行删除 row12地址行删除 row13地址行删除 row14地址行删除 row15 BLANK ROW删除 row16 DATE RANGE(就像01-jan-16到31-jan-16 =>保持这个 row17 bLANK ROW删除 row18 ROW HEADINGS保留这个 第19行数据数据不要更改所有数据 第20行数据 第21行数据 第22行数据 第23行数据
我尝试使用以下代码执行此操作:
Sub EmptyRowFind()
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String
sourceCol = 1
rowCount = Cells(20, sourceCol).Row
For currentRow = 1 To rowCount
currentRowValue = Cells(currentRow, sourceCol).Value
If IsEmpty(currentRowValue) Or currentRowValue = "" Then
For i = i To currentRow - 2
Rows(1).Select
Selection.Delete Shift:=xlUp
Next
End If
End Sub
答案 0 :(得分:0)
特别感谢Leviathan先生给予我如此快速的回应。正如我自己解决问题所以...找到我新编辑的代码
Sub EmptyRowFind()
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String
sourceCol = 1
rowCount = Cells(20, sourceCol).Row
For currentRow = 1 To rowCount
currentRowValue = Cells(currentRow, sourceCol).Value
If IsEmpty(currentRowValue) Or currentRowValue = "" Then
For i = i To currentRow - 2
Rows(1).Select
Selection.Delete Shift:=xlUp
Next
End If
If IsEmpty(currentRowValue) Or currentRowValue = "" Then'==>Now added
Exit For
End If
Next
End Sub
Exlanation: - 当旧代码运行时isempty循环删除行,当它找到空行但又返回第一个for循环(currentrow = 1到rowcount) &安培;它再次运行循环,删除行直到第二个空行。这个过程持续了20次(因为rowcount = 20)。