我有一列值,我想选择大于0的单元格:
我尝试在网上查找但找不到解决方案。如何使用VBA脚本执行此操作,最好不要编写循环?
谢谢!
答案 0 :(得分:2)
使用Filter。在A1
上方添加额外的行,创建过滤器并过滤掉0
。不需要VBA:)
您还可以创建custom filter。
答案 1 :(得分: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
答案 2 :(得分: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
所以这可以完成你想要的没有循环,但类似于过滤器