EXCEL VBA 数据表多条件查询 如条件为空白,继续判读其它条件而不是去判读数据中为空白的单元格.

在条件单元格不为空白的情况下就可实现条件计算个数功能.
目前问题是假设某个条件为空白计算出来结果就是没有.
所以想实现的功能是在某个条件为空白的情况下程序能够继续进行其它条件的计算,而不是去判读数据中为空白的单元格.
代码如下:
Private Sub CommandButton1_Click()
With Sheet1
C1 = .Range("B1")
C2 = .Range("B2")
C3 = .Range("B3")
C4 = .Range("B4")
C5 = .Range("B5")
.Range("C3") = ""
End With
For h = 10 To 1000
If Sheet1.Cells(h, "A") >= C1 And Sheet1.Cells(h, "A") <= C2 And Sheet1.Cells(h, "B") = C3 And Sheet1.Cells(h, "C") = C4 And Sheet1.Cells(h, "D") = C5 Then
a = a + 1
Sheet1.Range("C3") = a
End If
Next h
End Sub

第1个回答  2012-02-16
If Sheet1.Cells(h, "A") >= C1 And Sheet1.Cells(h, "A") <= C2 and ( Sheet1.Cells(h, "B") = C3 or Sheet1.Cells(h, "C") = C4 or Sheet1.Cells(h, "D") = C5 )追问

这样的话数据如果某条件为空白,其它条件不为空白,那么a=a+1就会出现错误!还是不可行!

第2个回答  2012-02-17
Private Sub CommandButton1_Click()
With Sheet1
C1 = .Range("B1")
C2 = .Range("B2")
C3 = .Range("B3")
C4 = .Range("B4")
C5 = .Range("B5")
.Range("C3") = ""
End With
For h = 10 To 1000
If ((Sheet1.Cells(h, "A") >= C1 And Sheet1.Cells(h, "A") <= C2) Or Sheet1.Cells(h, "A") = "") _
And (Sheet1.Cells(h, "B") = C3 Or Sheet1.Cells(h, "B") = "") And (Sheet1.Cells(h, "C") = C4 Or Sheet1.Cells(h, "C") = "") _
And (Sheet1.Cells(h, "D") = C5 Or Sheet1.Cells(h, "D") = "") Then
If Sheet1.Cells(h, "A") & Sheet1.Cells(h, "B") & Sheet1.Cells(h, "C") & Sheet1.Cells(h, "D") <> "" Then
a = a + 1
Sheet1.Range("C3") = a
End If
End If
Next h
End Sub
第3个回答  2012-02-16
Sheet1.Cells(h, "A") >= C1改为:
Sheet1.Cells(h, "A") >= C1 and Sheet1.Cells(h, "A") <>""
以此类推,该其他的就行了。追问

If Sheet1.Cells(h, "A") >= C1 And Sheet1.Cells(h, "A") <= C2 ‘此段为日期范围查询不可以更改,
按照你的方法对后面的代码Sheet1.Cells(h, "B") = C3 And Sheet1.Cells(h, "C") = C4 And Sheet1.Cells(h, "D") = C5 Then,进行尝试咯下好像不行,因为判断了数据条件,在区判断数据中不为空的值他理论上应该还是返回条件判断的值!

追答

(Sheet1.Cells(h, "B") = C3 and Sheet1.Cells(h, "B")"")
将每段用括号括起来试一下,就像我写的那样

追问

还是不行 当条件是空白的时候它就不计算咯 显示空白的

追答

(Sheet1.Cells(h, "B") = C3 or C3="")
这样改试一下

第4个回答  2012-02-19
Private Sub CommandButton1_Click()
With Sheet1
C1 = .Range("B1").Value
C2 = .Range("B2").Value
C3 = .Range("B3").Value
C4 = .Range("B4").Value
C5 = .Range("B5").Value
.Range("C3") = ""
End With
For h = 10 To 1000
If ((Sheet1.Cells(h, "A").Value >= C1 Or Len(C1) = 0) And (Sheet1.Cells(h, "A").Value <= C2 Or Len(C2) = 0)) And (Sheet1.Cells(h, "B").Value = C3 Or Len(C3) = 0) And (Sheet1.Cells(h, "C").Value = C4 Or Len(C4) = 0) And (Sheet1.Cells(h, "D").Value = C5 Or Len(C5) = 0) Then
a = a + 1
Sheet1.Range("C3").Value = a
End If
Next h
End Sub本回答被提问者采纳

相关了解……

你可能感兴趣的内容

大家正在搜

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网