查找最后一个大于 0 的单元格
Find cell before last that is greater than 0
我在 VBA 中有以下代码来查找范围内大于 0 的最后一个单元格:
Set myRange = .Range(.Cells(1, 14), .Cells(1, 23))
count = 0 'Counter
For Each cll In myRange
If cll.Value > 0 Then
count = count + 1
NoZeroDir = cll.Address
End If
Next
它获取该范围内最后一个大于 0 的单元格的地址。
但是,如何从最后一个之前大于 0 的单元格中获取地址?
我正在考虑使用偏移量,但那样我会得到最后一个 > 0 之前的单元格,但这个单元格不能 > 0。
稍微说明一下,作为 示例 我有:
2 3 5 0 1 7 0 8 1 0 1
来自 > 0 的最后一个单元格的地址将是 (1,11),但我希望那个 > 0 之前的单元格,即 (1,9),而不是 (1,10),因为这是 0。
找到倒数第二个数字>0
Option Explicit
Public Sub FindSecondLastValueGreaterZero()
Dim MyRange As Range
Set MyRange = Range("A1:K1")
Const MAXSKIPS As Long = 1 ' skip 1 number that is >0
Dim Skips As Long
Dim iCol As Long
For iCol = MyRange.Columns.Count To 1 Step -1
If MyRange(1, iCol).Value > 0 And Skips < MAXSKIPS Then
Skips = Skips + 1
ElseIf MyRange(1, iCol).Value > 0 Then
Debug.Print "Found at: " & MyRange(1, iCol).Address
Exit For
End If
Next iCol
End Sub
这将从 K 循环开始,直到它找到一个 0
然后一直这样做直到跳过 >0
是 1
并打印地址 I1
作为结果。
由于这是从右到左向后循环,因此它应该比您的代码更快地找到结果(在大多数情况下)。
使用工作表函数的替代方法Filter()
(相对于 MS 365)
基于较新的 WorksheetFunction Filter()
(自版本 MS/Excel 365 起可用) 并使用 OP 的范围指示
=FILTER(COLUMN(A1:K1),A1:K1>0)
您可以通过对广义公式模式的评估从 大于零 (0) 的单元格中获取列号数组。
如果你得到至少两个剩余的列(即上边界 UBound() > 1
),你可以通过 i = cols(UBound(cols) - 1)
获得所需的倒数第二个列号,并可以通过 [=17= 将其转换为地址].
Public Sub SecondLastValGreaterZero()
'a) construct formula to evaluate
Const FormulaPattern As String = "=FILTER(COLUMN($),$>0)"
Dim rng As Range
Set rng = Sheet1.Range("A1:K1") ' << change to your needs
Dim myFormula As String
myFormula = Replace(FormulaPattern, "$", rng.Address(False, False, external:=True))
'b) get tabular column numbers via Evaluate
Dim cols As Variant
cols = Evaluate(myFormula)
'c) get the 2nd last column number of cell values > 0
Dim i As Long
If Not IsError(cols) Then
If UBound(cols) > 1 Then i = cols(UBound(cols) - 1)
End If
'd) display result
If i > 0 Then
Debug.Print "Found at column #" & i & ": " & Cells(1, i).Address
Else
Debug.Print "Invalid column number " & CStr(i)
End If
End Sub
示例结果 VB 编辑器直接 window
Found at column #9: $I
我在 VBA 中有以下代码来查找范围内大于 0 的最后一个单元格:
Set myRange = .Range(.Cells(1, 14), .Cells(1, 23))
count = 0 'Counter
For Each cll In myRange
If cll.Value > 0 Then
count = count + 1
NoZeroDir = cll.Address
End If
Next
它获取该范围内最后一个大于 0 的单元格的地址。 但是,如何从最后一个之前大于 0 的单元格中获取地址?
我正在考虑使用偏移量,但那样我会得到最后一个 > 0 之前的单元格,但这个单元格不能 > 0。
稍微说明一下,作为 示例 我有:
2 3 5 0 1 7 0 8 1 0 1
来自 > 0 的最后一个单元格的地址将是 (1,11),但我希望那个 > 0 之前的单元格,即 (1,9),而不是 (1,10),因为这是 0。
找到倒数第二个数字>0
Option Explicit
Public Sub FindSecondLastValueGreaterZero()
Dim MyRange As Range
Set MyRange = Range("A1:K1")
Const MAXSKIPS As Long = 1 ' skip 1 number that is >0
Dim Skips As Long
Dim iCol As Long
For iCol = MyRange.Columns.Count To 1 Step -1
If MyRange(1, iCol).Value > 0 And Skips < MAXSKIPS Then
Skips = Skips + 1
ElseIf MyRange(1, iCol).Value > 0 Then
Debug.Print "Found at: " & MyRange(1, iCol).Address
Exit For
End If
Next iCol
End Sub
这将从 K 循环开始,直到它找到一个 0
然后一直这样做直到跳过 >0
是 1
并打印地址 I1
作为结果。
由于这是从右到左向后循环,因此它应该比您的代码更快地找到结果(在大多数情况下)。
使用工作表函数的替代方法Filter()
(相对于 MS 365)
基于较新的 WorksheetFunction Filter()
(自版本 MS/Excel 365 起可用) 并使用 OP 的范围指示
=FILTER(COLUMN(A1:K1),A1:K1>0)
您可以通过对广义公式模式的评估从 大于零 (0) 的单元格中获取列号数组。
如果你得到至少两个剩余的列(即上边界 UBound() > 1
),你可以通过 i = cols(UBound(cols) - 1)
获得所需的倒数第二个列号,并可以通过 [=17= 将其转换为地址].
Public Sub SecondLastValGreaterZero()
'a) construct formula to evaluate
Const FormulaPattern As String = "=FILTER(COLUMN($),$>0)"
Dim rng As Range
Set rng = Sheet1.Range("A1:K1") ' << change to your needs
Dim myFormula As String
myFormula = Replace(FormulaPattern, "$", rng.Address(False, False, external:=True))
'b) get tabular column numbers via Evaluate
Dim cols As Variant
cols = Evaluate(myFormula)
'c) get the 2nd last column number of cell values > 0
Dim i As Long
If Not IsError(cols) Then
If UBound(cols) > 1 Then i = cols(UBound(cols) - 1)
End If
'd) display result
If i > 0 Then
Debug.Print "Found at column #" & i & ": " & Cells(1, i).Address
Else
Debug.Print "Invalid column number " & CStr(i)
End If
End Sub
示例结果 VB 编辑器直接 window
Found at column #9: $I