求excel 宏代码 , 如何将我一个excel文件中含有某一特定内容的所有sheet表都罗列出来

具体如下 图所示
我文件中有很多的sheet表,比如HW47 HW69 HW87等等,比如我想查找包含”中国人“内容的sheet表,而HW47 HW69里都有“中国人”,就在汇总表里显示HW47 HW69。谢谢各位大侠了,多给分。

'如果结果放在 "汇总表"的A列
sub test()
for each sh in sheets
rw = 1 '从A1开始显示

if sh.name<>"汇总表" then
if f1(sh) then
sheets("汇总表").range("A" & rw) = sh.name
rw = rw +1
end if

end if
next
end sub

function f1(sh as worksheet) as boolean
on error goto l_end
a = sh.range("1:65536").find("中国人",lookin:=xlvalues,lookat:=xlpart)
f1=true
exit function
l_end:
f1 = false
end function

'执行 test 就可以了追问

不好意思,我还没说清楚,查找的内容不一定是 “中国人”,可能是其他,能否出来一个对话框,让我输入,或者我把查找的内容放sheet“汇总表”的B1单元格内,您看可否写代码。

追答

Sub test()
Dim finstr As String
finstr = InputBox("请输入要查询的字符")
Dim sh As Worksheet
rw = 1 '从A1开始显示
For Each sh In Sheets

If sh.Name "汇总表" Then
If f1(sh, finstr) Then
Sheets("汇总表").Range("A" & rw) = sh.Name
rw = rw + 1
End If

End If
Next
End Sub

Function f1(sh As Worksheet, str1) As Boolean
On Error GoTo l_end
a = sh.Range("1:65536").Find(str1, LookIn:=xlValues, lookat:=xlPart)
f1 = True
Exit Function
l_end:
f1 = False
End Function

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-04-30

Sub 查找()


Dim i As Integer
Dim k As Integer
Dim x
Dim m As String
Dim s As Worksheet
Dim 汇总 As Worksheet

Set 汇总 = ThisWorkbook.Worksheets("汇总表")
k = 1

For i = 1 To ThisWorkbook.Sheets.Count
 Set s = ThisWorkbook.Sheets(i)
 
 If s.Name = "汇总表" Then
 Else
  For Each x In s.UsedRange
   If x Like "*中国人*" Then
    汇总.Cells(k, 1) = s.Name
    k = k + 1
    Exit For
   End If
  Next
 End If
Next

MsgBox "查找完成"

End Sub


'其实Ctrl+F查找里面,选项中范围改成工作薄,也能查找

追问

不好意思,我还没说清楚,查找的内容不一定是 “中国人”,可能是其他,能否出来一个对话框,让我输入,或者我把查找的内容放sheet“汇总表”的B1单元格内,您看可否写代码。

追答

If x Like "*中国人*" Then

把中国人改成你需要查找的内容就行了。如果是要模糊查找的话,两边都加一个*

相似回答