使用单元格值

我试图使用单元格F2中的值作为范围select的最大值在一个更大的数字范围内。

我已经得到了下面,但得到一个编译错误:variables未定义。

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 

在这一行中:

 Call SelectByValue(Range("A2:A41"), 1, Range(F2).Value) 

F2需要引用。 这不是代码可以看到的variables:

 Call SelectByValue(Range("A2:A41"), 1, Range("F2").Value) 

TessellatingHeckler有更好的答案 ,但值得注意的是,如果使用括号而不是Range则可以避免使用引号。

 Call SelectByValue( [A2:A41], 1, [F2].Value) 

这种语法通常是不鼓励的,因为括号会导致模糊的结果,在运行时会造成混乱。