简体   繁体   English

excel vba select 负范围

[英]excel vba select negative range

  1. Sort "Column B" <== done with the following code:对“B 列”进行排序 <== 使用以下代码完成:
' sorts %change from cell B2 to the end of the line
Range("B2", Range("B2").End(xlDown)).Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlNo

Data:数据:

A一种 B C C
1 1个 8 8个 2 2个
2 2个 -2 -2 4 4个
3 3个 3 3个 -1 -1
4 4个 -5 -5 8 8个
5 5个 0 0 10 10
6 6个 -33 -33 65 65

Output: Output:

A一种 B C C
6 6个 -33 -33 65 65
4 4个 -5 -5 8 8个
2 2个 -2 -2 4 4个
5 5个 0 0 10 10
3 3个 3 3个 -1 -1
1 1个 8 8个 2 2个
  1. Select negative ranges cells only Select 仅负范围单元格

Now the cursor will be in cell with value -33 (cell B2).现在 cursor 将位于值为 -33 的单元格(单元格 B2)中。 I need to select the ranges with value -33, -5 and -2.我需要 select 值为 -33、-5 和 -2 的范围。 Is there a quick way (builtin formulas) to identify these ranges without evaluating each cell value?有没有一种快速的方法(内置公式)可以在不评估每个单元格值的情况下识别这些范围?

Note: The obtained data will be dynmaic, number of rows is not fixed.注意:获取的数据是动态的,行数不固定。

Select Negative Values in Sorted Column Select 排序列中的负值

Option Explicit

Sub Test()
    With ActiveSheet.Range("A1").CurrentRegion
        .Sort Key1:=.Columns(2), Order1:=xlAscending, Header:=xlYes
        With .Columns(2).Resize(.Rows.Count - 1).Offset(1)
            On Error Resume Next
            .Resize(Application.CountIf(.Cells, "<0")).Select
            On Error GoTo 0
        End With
    End With
End Sub

Focussing on your main issue to select only the negative cells (ie after sorting - there are number of answers at SO:-) you might try:专注于 select 的主要问题,仅关注阴性细胞(即排序后 - SO 处有许多答案 :-) 你可以尝试:

With Sheet1          ' << the sheet's Code(Name)
    Dim lastRow As Long
    lastRow = .Range("B" & .Rows.Count).End(xlUp).Row

    Dim srchRow As Long
    srchRow = .Evaluate("Match(False,B2:B" & lastRow & "<0,0)")
    'Debug.Print srchRow
    
    Dim rng As Range
    Set rng = .Range("B2", .Cells(srchRow, "B"))
    rng.Select
End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM