根据行数防止行消除

时间:2013-08-21 13:46:25

标签: excel vba conditional

我有一个需要维护至少17行内容的电子表格。 B列列出了行号。我用宏录制器开发了一些代码:

Columns("B:B").Select
Selection.Find(What:="1", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False).Activate
ActiveCell.Select
Range(Selection, Selection.End(xlDown)).Select

我希望做的是:如果所选范围内的单元格数小于18,则退出sub,否则(运行代码)。

2 个答案:

答案 0 :(得分:1)

我不确定这是否是您所寻找的,但由于知识有限,这是我能想到的最好的结果:

Dim c As Range
For Each c In Selection
    If c.Value < 18 Then
        Exit Sub
    Else
        'Run Code (Your code here)
    End If
Next c

干杯,
kpark

答案 1 :(得分:1)

这就是诀窍

Columns("B:B").Select
 Selection.Find(What:="1", After:=ActiveCell, LookIn:=xlValues,  LookAt:= _
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False).Activate
    ActiveCell.Select
    Range(Selection, Selection.End(xlDown)).Select
Dim c As Range
Set c = Selection
If WorksheetFunction.CountA(c) < 18 Then
msg77 = MsgBox("Cannot Delete the Row since there are fewer than 18 Rows", vbOKOnly, "Sorry, I Cannot Serve your Request")
Exit Sub
Else
Selection.Copy
End If