我尝试使用单元格 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
最佳答案
在这一行中:
Call SelectByValue(Range("A2:A41"), 1, Range(F2).Value)
F2需要加引号。它不是代码可以看到的变量:
Call SelectByValue(Range("A2:A41"), 1, Range("F2").Value)
关于vba - 使用单元格值作为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30928097/