我有一列值,我想选择大于 0 的单元格:
我尝试在网上查找,但找不到解决方案。如何使用 VBA 脚本执行此操作,最好不编写循环?
谢谢!
或另一列(此处为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
也许是这样的:
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