根据行数防止行消除

我有一个电子表格需要保持至less17行的内容。 列B列出了行的编号。 我用macroslogging器来开发一些代码:

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,否则(运行代码)。

我不知道这是你在找什么,但有限的知识,这是最好的,我可以拿出:

 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

这是诀窍

 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