0

我有一列值,我想选择大于 0 的单元格:

例子

我尝试在网上查找,但找不到解决方案。如何使用 VBA 脚本执行此操作,最好不编写循环?

谢谢!

4

3 回答 3

2

使用过滤器。在上面添加一个额外的行A1,创建一个过滤器并过滤掉0。不需要VBA :)

您还可以创建自定义过滤器

于 2013-05-14T08:50:45.033 回答
1

或另一列(此处为B列,但可以是任何列)和SpecialCells函数

Sub selectCells()

Dim sFormula As String
Dim lLastRow As Long

lLastRow = Rows(ActiveSheet.Rows.Count).End(xlUp).Row

sFormula = "=IF(A:A>0,NA(),"""")"

With Range("B1:B" & lLastRow)
    .Formula = sFormula
    .SpecialCells(xlCellTypeFormulas, xlErrors).Offset(0, -1).Select
    .clear
End With

End Sub

所以这可以在没有循环的情况下完成你想要的,但类似于Filter

于 2013-05-14T10:36:39.753 回答
1

也许是这样的:

Option Explicit

Sub Main()

    Dim ws As Worksheet, i&, arr() As Long, rng As Range
    Set ws = ThisWorkbook.Sheets("Sheet1")

    ReDim arr(0)
    Call nonZeroSelection(ActiveSheet, i, arr, rng)

End Sub

Sub nonZeroSelection(ByRef ws As Worksheet, ByRef i&, ByRef arr() As Long, rng As Range)
    Dim lr&, ix$
    lr = ws.Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To lr
        Set rng = ws.Range("A" & i)
            If rng.Value = "0" Then
                ReDim Preserve arr(UBound(arr) + 1)
                arr(UBound(arr) - 1) = i
            End If
        Set rng = Nothing
    Next i
    ReDim Preserve arr(UBound(arr) - 1)
    For i = LBound(arr) To UBound(arr)
        ix = ix & arr(i) & ":" & arr(i) & ","
    Next i
    ix = Left(ix, Len(ix) - 1)
    ws.Range(ix).Select
End Sub
于 2013-05-14T10:22:47.787 回答