我在下面的代码
中收到错误 If Rng1.Value >= 0 And Rng2.Value <= 999999
然后
基本上我只想在值大于0且小于099999的情况下计算公式。
任何帮助都会得到满足。
mycal()
是由我创建的用于计算X的函数。
Sub mycal()
Dim thecountX As Integer
Dim Rng1 As range, Rng2 As range
Set Rng1 = range("C1:C25")
Set Rng2 = range("B1:B25")
If Rng1.Value >= 0 And Rng2.Value <= 999999 Then
'Here We are calculating value of X and storing in varilable "thecountX".
thecountX = (range("C10") * range("C8") - range("C9") * range("B13")) /
range("C10") + range("B13")
Else
MsgBox "Give value between 0 and 999999999999"
End If
'这里我们从变量“thecountX”显示X的值。 MsgBox“伯爵是”&amp; thecountX
答案 0 :(得分:2)
错误的原因是Rng1.Value
和Rng2.Value
都返回大小为(25,1)的二维数组。 >=
和<=
运算符不适用于数组。
请改用:
If WorksheetFunction.Min(Rng1) >= 0 And WorksheetFunction.Max(Rng2) <= 999999 Then
或者您可以遍历数组,如下所示:
Dim thecountX As Integer
Dim Rng1 As Range, Rng2 As Range
Dim v1 As Variant, v2 As Variant
Dim i As Long, n As Long
Set Rng1 = Range("C1:C25")
Set Rng2 = Range("B1:B25")
v1 = Rng1.Value
v2 = Rng2.Value
n = Rng1.Rows.Count
For i = 1 To n
If v1(i, 1) < 0 Or v2(i, 1) > 999999 Then
MsgBox "Give value between 0 and 999999999999"
Exit For
Else
thecountX = (Range("C10") * Range("C8") - Range("C9") * Range("B13")) _
/ Range("C10") + Range("B13")
End If
Next i
答案 1 :(得分:0)
我认为您无法从Range中获取值。您可以获得该范围内数字的总和(平均值等)值。 你想要实现这样的目标吗?
Sub mycal()
Dim Rng1 As Currency
Dim Rng2 As Currency
Dim thecountX As Double
Rng1 = Application.WorksheetFunction.Sum(Range("C1:C25"))
Rng2 = Application.WorksheetFunction.Sum(Range("B1:B25"))
If Rng1 >= 0 And Rng2 <= 999999 Then
thecountX = ((Range("C10").Value * Range("C8").Value) - (Range("C9").Value * Range("B13").Value)) / (Range("C10").Value + Range("B13").Value)
MsgBox ("The count is" & thecountX)
End If
End Sub