|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub 查找物料号search_mm()
'子程序 查找物料号search_mm()
Dim lastRow As Long
'定义变量lastRow作为data工作表当前行号
lastRow = 2
Dim c As Integer
'定义变量c作为结果区域当前行号
c = Range("H3") + Cells(Rows.Count, 7).End(xlUp).Row
Dim find_num As Integer
'定义变量find_num用于统计搜索结果总数量
Dim rng As Range
Set rng = Range("R5:R7")
Dim keywords(2) As Range
Set keywords(0) = Range("R5")
Set keywords(1) = Range("R6")
Set keywords(2) = Range("R7")
If rng.Count >= 3 Then
'R5:R7单元格值数量大于等于3才执行搜索
For Each cell In rng
'For Each循环遍历R5到R7单元格
Dim search As String
'定义变量search获取当前单元格值
search = cell.Value
Dim search1
'定义变量search1为Like运算符所需的通配符搜索条件
search1 = "*" & search & "*"
Do While Sheets("data").Cells(lastRow, 1).Value <> ""
'Do While循环逐行搜索data工作表
If Sheets("data").Cells(lastRow, 1).Value Like search1 Or _
Sheets("data").Cells(lastRow, 2).Value Like search1 Or _
Sheets("data").Cells(lastRow, 6).Value Like search1 Or _
Sheets("data").Cells(lastRow, 7).Value Like search1 Then
find_num = find_num + 1
'搜索结果数量加1
If Sheets("data").Cells(lastRow, 5).Value <> "否" Then
Cells(c, 7).Font.ColorIndex = 3
Cells(c, 8).Font.ColorIndex = 3
Cells(c, 16).Font.ColorIndex = 3
Cells(c, 17).Font.ColorIndex = 3
Else
Cells(c, 7).Font.ColorIndex = 0
Cells(c, 8).Font.ColorIndex = 0
Cells(c, 16).Font.ColorIndex = 0
Cells(c, 17).Font.ColorIndex = 0
End If
Cells(c, 7).Value = Sheets("data").Cells(lastRow, 1).Value
Cells(c, 8).Value = Sheets("data").Cells(lastRow, 2).Value
Cells(c, 9).Value = Sheets("data").Cells(lastRow, 3).Value
Cells(c, 10).Value = Sheets("data").Cells(lastRow, 4).Value
Cells(c, 13).Value = Sheets("data").Cells(lastRow, 6).Value
Cells(c, 16).Value = Sheets("data").Cells(lastRow, 5).Value
Cells(c, 17).Value = Sheets("data").Cells(lastRow, 7).Value
c = c + 1
End If
lastRow = lastRow + 1
Loop
Next
'Else
'MsgBox "R5:R7单元格中值少于3个,请检查!"
'End
End If
MsgBox "本次共计查出 " & find_num & " 条记录"
End Sub
哪位大神能帮忙修改一下代码,目标实现功能是,R列:R5-R604每个单元格都能搜索查找相应的内容,谢谢!
|
|