vba中多列查询excel~,返回行号
A B C
1 aa bb cc
2 aaa bbb ccc
3 aaaa bbbb cccc
条件是 列A 给定值aaa,列B 给定值bbb 列C给定值ccc,结果应该是第二行,但用VBA
该如何实现这个查询并返回行号?
[解决办法]
Private Sub CommandButton1_Click()
Dim iIndex As Long '行号
Dim strRangeA As String 'A列
Dim strRangeB As String 'B列
Dim strRangeC As String 'c列
For iIndex = 1 To 65535
strRangeA = "A " + CStr(iIndex)
strRangeB = "B " + CStr(iIndex)
strRangeC = "C " + CStr(iIndex)
If (Sheet1.Range(strRangeA).Value = "aaa " And Sheet1.Range(strRangeB).Value = "bbb " And Sheet1.Range(strRangeC).Value = "ccc ") Then
MsgBox iIndex
Exit Sub
End If
If (Sheet1.Range(strRangeA).Value = " ") Then
Exit Sub
End If
Next
End Sub
已调试
[解决办法]
刚下班比较匆忙
完善一点
Sub test()
Dim IsExist
IsExist=0
Set r = Selection
For n = 1 To r.Rows.Count
For j = 1 To r.Columns.Count
a = r.Cells(n, j).Value
If a=range( "D1 ").Value Then
msgbox range( "D1 ").Value & "在第 " & n & "行 "
IsExist=1
End If
Next j
Next n
if IsExist=0 then
msgbox "找不到查询数据 "
end if
End Sub
在选中部分查找range( "D1 ")的数据, 如有显示其所在行数.