|
VBA中如何表达:if 条件1=条件2 并且 表达式2-10<表达式1<表达式2 Then... 也就是我这个语句中,要条件1=条件2,还要 某个单元格值在某个范围内时 才能继续then之后的语句。
Sub test()
Dim x As Integer, y As Integer
Dim arr(1 To 20, 1 To 4)
For x = 2 To 20000
If Sheets("sheet1").Cells(x, 5) = Sheets("sheet2").Range("E3") _
And Sheets("sheet2").Range("J3") - 10 < Sheets("sheet1").Cells(x, 10) And Sheets("sheet1").Cells(x, 10) < Sheets("sheet2").Range("J3") Then '这个蓝色的整行是否表达有问题,为什么得不到结果,请大师指教啊。。。
y = y + 1
arr(y, 1) = Sheets("sheet1").Cells(x, 5)
arr(y, 2) = Sheets("sheet1").Cells(x, 6)
arr(y, 3) = Sheets("sheet1").Cells(x, 9)
arr(y, 4) = Sheets("sheet1").Cells(x, 10)
End If
Next x
Range("M2").Resize(10, 4) = arr
End Sub
下面这个索性只用了一个And没有限制Sheets("sheet2").Range("J3") - 10 < Sheets("sheet1").Cells(x, 10) ,就可以得到正确的结果。 可是我要限制啊,怎么弄?
Sub test()
Dim x As Integer, y As Integer
Dim arr(1 To 20, 1 To 4)
For x = 2 To 20000
If Sheets("sheet1").Cells(x, 5) = Sheets("sheet2").Range("E3") And Sheets("sheet1").Cells(x, 10) < Sheets("sheet2").Range("J3") Then
y = y + 1
arr(y, 1) = Sheets("sheet1").Cells(x, 5)
arr(y, 2) = Sheets("sheet1").Cells(x, 6)
arr(y, 3) = Sheets("sheet1").Cells(x, 9)
arr(y, 4) = Sheets("sheet1").Cells(x, 10)
End If
Next x
Range("M2").Resize(10, 4) = arr
End Sub
|
|