【发布时间】:2019-04-04 09:44:06
【问题描述】:
我正在寻找一些代码来搜索范围并返回该范围内符合特定条件的每个单元格的行号并列出这些行。
以前我只需要第一个值,所以一直在使用代码:
Dim Criteria1 As Single
Dim Criteria2 As Single
Dim Required As Integer
Dim Range1 As Range
Dim GearNeg1 As Integer
SetColumn = 24
Set Range1 = Sheets("X").Range("A2:BT72").Columns(SetColumn).Cells
Criteria1 = Sheets("X").Range("P111").Value
Criteria2 = Sheets("X").Range("Q111").Value
For Each Cell In Range1
If Cell.Value < Criteria1 And Cell.Value > Criteria2 Then
Required = Cell.row
Exit For
End If
Next
我一直在尝试添加一个 for 循环以将满足条件的值的所有行值返回到列表中。但是我很挣扎,似乎每次都只能达到第一个值。
【问题讨论】:
-
你为什么这样做:Set Range1 = Sheets("X").Range("A2:BT72").Columns(SetColumn).Cells ? SetColumn 是否有所不同?此外,请使用 Long 而不是 Integer 以避免潜在的溢出。
-
感谢您的建议,是的,setcolumn 确实因问题而异