我正在尝试将单元格F2中的值用作较大数字范围内的范围选择的最大值。
我已经达到了以下目的,但是我收到了编译错误:变量未定义。
Option Explicit
Sub SelectByValue(Rng1 As Range, MinimunValue As Double, MaximumValue As Double)
Dim MyRange As Range
Dim Cell As Object
'Check every cell in the range for matching criteria.
For Each Cell In Rng1
If Cell.Value >= MinimunValue And Cell.Value <= MaximumValue Then
If MyRange Is Nothing Then
Set MyRange = Range(Cell.Address)
Else
Set MyRange = Union(MyRange, Range(Cell.Address))
End If
End If
Next
'Select the new range of only matching criteria
MyRange.Select
End Sub
Sub CallSelectByValue()
'Call the macro and pass all the required variables to it.
'In the line below, change the Range, Minimum Value, and Maximum Value as needed
Call SelectByValue(Range("A2:A41"), 1, Range(F2).Value)
End Sub
答案 0 :(得分:3)
在这一行:
Call SelectByValue(Range("A2:A41"), 1, Range(F2).Value)
F2需要引用。它不是代码可以看到的变量:
Call SelectByValue(Range("A2:A41"), 1, Range("F2").Value)
答案 1 :(得分:1)
TessellatingHeckler has the better answer,但值得注意的是,如果使用括号代替Range
,则可以不使用引号。
Call SelectByValue( [A2:A41], 1, [F2].Value)
通常不鼓励这种语法,因为括号会导致模糊的结果,这可能会在运行时造成混乱。