我有以下Excel电子表格:
col2 col3 col4 col5 col6
row2 2 0 1004 0 200
row3 0 0 0 0 0
row4 0 0 0 0 57
row5 1 22 67 11 303
row6 0 0 0 0 0
row7 1 2 3 4 5
row8 3 3 3 3 3
我想打印或标识在其后全为零的行的名称。对于上面的示例,输出应为:
row3
row6
,因为这是第一个(名称)单元格之后仅有全零的行的名称。
下面是我如何找到这些名称(第1列的子集)的代码。
// Setting the variables for the above example
common_char = 0
num_rows = 8;
num_columns = 6;
boolean show = true;
for x in range (2 , num_rows):
for y in range (2 , num_columns):
if (cell[x,y] != common_char):
show = false
if (show == true):
print cell[x,1]
show = true
end
我应该如何在Excel中实现它?
谢谢您的帮助。
答案 0 :(得分:0)
尝试
Option Explicit
Public Sub Test()
Dim common_char As Long, num_rows As Long, num_Columns As Long, iRow As Range, loopRange As Range
common_char = 0
num_rows = 7 '8
num_Columns = 5 '6
With ActiveSheet
Set loopRange = .Cells(2, 2).Resize(num_rows, num_Columns)
For Each iRow In loopRange.Rows
If Application.WorksheetFunction.CountIf(iRow, common_char) = num_Columns Then
MsgBox iRow.Address
End If
Next iRow
End With
End Sub
这适用于如下所示的数据布局,因此我从num_rows和num_columns中都删除了1,以说明实际数据范围。