多个条件语句判断不准确求助!
本帖最后由 xgh5188 于 2013-04-11 15:30:31 编辑 Dim I As Integer: Dim J As Integer: Dim K As Integer
'如果想一次找到所有同类项,只需要把行15注释掉,再使用行17和行30就行了
'不管条件再多,就是并下去就行了
Private Sub CommandButton1_Click()
For I = 1 To Application.CountA(Sheet1.Columns(1)) - 1
Range(Cells(I, 1), Cells(I, 22)).Interior.ColorIndex = 2 '清除底色
Next I
For I = 1 To Sheet2.Application.CountA(Sheet1.Columns(1)) - 1
Sheet2.Range(Sheet2.Cells(I, 1), Sheet2.Cells(I, 22)).Interior.ColorIndex = 2
Next I
I = CInt(Trim(InputBox("请输入行号", "每次一行", 2))) '确定行号,否则所有有同类项的行的底色都被改变 行15
K = 0
'For I = 1 To Application.CountA(Sheet1.Columns(1)) - 1 '行17
For J = 1 To Sheet2.Application.CountA(Sheet1.Columns(1)) - 1
If Sheet2.Cells(J, "E").Value = Cells(I, "F").Value _
Then 'And Sheet2.Cells(J, "F").Value = Cells(I, "E").Value _
'And Sheet2.Cells(J, "F").Value = Cells(I, "N").Value _
'And Sheet2.Cells(J, "J").Value = Cells(I, "J").Value Then
Sheet2.Range(Sheet2.Cells(J, 1), Sheet2.Cells(J, 22)).Interior.ColorIndex = 5 '改变已经找到的符合要求行的底色
Range(Cells(I, 1), Cells(I, 22)).Interior.ColorIndex = 5
K = K + 1 '统计同类行的数量
End If
Next J
If K > 0 Then
MsgBox "在 <" + Sheet2.Name + "> 表中共找到与 <" + Sheet1.Name + "> 第" + CStr(I) + "行数据吻合的数据" + CStr(K) + "条!", vbInformation, "这么难,还是被我找到了!"
Else
MsgBox "没有找到与 <" + Cells(I, "F").Value + "> 匹配的项目!", vbInformation, "很遗憾,我没有找到有价值的线索!"
End If
'Next I '行30
End Sub
此代码是用在EXCEL中对比两个EXCLE表格的,必须同时满足几个条件的才标记颜色跳出来,但是这段代码有点问题,挑出的数据是2个,按道理是条件越多挑出的东西越准确,麻烦哪位大侠看看那里有问题
求教高人!excel表格在我手里,请联系回帖留言! Excel
[解决办法]
I = CInt(Trim(InputBox("请输入行",...
For I = 1 To Application.CountA(Sheet1.Columns(1)) - 1
两个I有冲突,你先把其中的一个改成别的试试。