0

我有一个需要维护至少 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,否则(运行代码)。

4

2 回答 2

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

于 2013-08-21T14:12:34.297 回答
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
于 2013-08-21T14:40:43.613 回答