ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 630|回复: 3

[求助] 求助运行这个代码如何实现搜索

[复制链接]

TA的精华主题

TA的得分主题

发表于 2022-11-24 10:04 | 显示全部楼层 |阅读模式
'module 1

Public d

Public findString

Sub button_Click()

    Application.ScreenUpdating = False

    ActiveSheet.UsedRange.ClearContents

   rem Cells(1, 1) = "stringFind"

   findString=cells(1,1)

    Set d = CreateObject("scripting.dictionary")

    Getfd (ThisWorkbook.Path) 'ThisWorkbook.Path是当前代码文件所在路径,路径名可以根据需求修改

    Application.ScreenUpdating = True

    If d.Count > 0 Then

        ThisWorkbook.Sheets(1).[a2].Resize(d.Count) = WorksheetFunction.Transpose(d.keys)

        ThisWorkbook.Sheets(1).[b2].Resize(d.Count) = WorksheetFunction.Transpose(d.items)

    End If

End Sub

Sub Getfd(ByVal pth)



    Set Fso = CreateObject("scripting.filesystemobject")

    Set ff = Fso.getfolder(pth)

    For Each f In ff.Files

        Rem 具体提取哪类文件,还是需要根据文件扩展名进行处理

        If InStr(Split(f.Name, ".")(UBound(Split(f.Name, "."))), "xl") > 0 Then

            If f.Name <> ThisWorkbook.Name Then

                Set wb = Workbooks.Open(f)

                For Each sht In wb.Sheets

                    If WorksheetFunction.CountA(sht.UsedRange) > 1 Then

                        arr = sht.UsedRange

                   rem     For j = 2 To UBound(arr)

                   rem         d(arr(j, 1)) = d(arr(j, 1)) + arr(j, 2)

                  rem      Next j

                                  rem handle each data of a sheet

call handle_find(arr,pth,f)

                    End If

                Next sht

                wb.Close False

            End If

        End If

    Next f

    For Each fd In ff.subfolders

        Getfd (fd)

    Next fd

End Sub

sub handle_find(arr,pth,f)

For r = 1 To UBound(arr)

For j = 1 To UBound(arr, 2)

If arr(r, j) = findString Then

rem need test

d(pth & "\" & f.name)=""

Exit For

End If

end sub


四、遍历当前文件夹及所有子文件夹下的文件.zip

63.6 KB, 下载次数: 8

TA的精华主题

TA的得分主题

发表于 2022-11-24 13:49 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
这个,让版主来吧,版主精通。

TA的精华主题

TA的得分主题

 楼主| 发表于 2022-11-24 14:14 来自手机 | 显示全部楼层
ykcbf1100 发表于 2022-11-24 13:49
这个,让版主来吧,版主精通。

就是版主liu的代码,改了一下,想实现多文件搜索。

群里帮忙运行了一下,排除了一些 ~开头的,加了 next

发现 arr = sht.UsedRange,在 子文件夹 文件 1.21.xlsx 只有a1单元格有值,不能返回数组arr(1,1),是空的,其他还没发现。

arr=
'module 1
Public d
Public findString
Sub button_Click()
    Application.ScreenUpdating = False
    ActiveSheet.UsedRange.ClearContents
   rem Cells(1, 1) = "stringFind"
   findString=cells(1,1)
   findString="test"
   rem here fixed value for test
    Set d = CreateObject("scripting.dictionary")
    Getfd (ThisWorkbook.Path) 'ThisWorkbook.Path是当前代码文件所在路径,路径名可以根据需求修改
    Application.ScreenUpdating = True
    If d.Count > 0 Then
        ThisWorkbook.Sheets(1).[a2].Resize(d.Count) = WorksheetFunction.Transpose(d.keys)
        ThisWorkbook.Sheets(1).[b2].Resize(d.Count) = WorksheetFunction.Transpose(d.items)
    End If
End Sub
Sub Getfd(ByVal pth)
   
    Set Fso = CreateObject("scripting.filesystemobject")
    Set ff = Fso.getfolder(pth)
    For Each f In ff.Files
        Rem 具体提取哪类文件,还是需要根据文件扩展名进行处理
        If InStr(Split(f.Name, ".")(UBound(Split(f.Name, "."))), "xl") > 0 and not (left(f.Name,1)="~") Then
            If f.Name <> ThisWorkbook.Name Then
                Set wb = Workbooks.Open(f)
                For Each sht In wb.Sheets
                    If WorksheetFunction.CountA(sht.UsedRange) > 0 Then
                        arr = sht.UsedRange
                   rem     For j = 2 To UBound(arr)
                   rem         d(arr(j, 1)) = d(arr(j, 1)) + arr(j, 2)
                  rem      Next j
                                  rem handle each data of a sheet
call handle_find(arr,pth,f)
                    End If
                Next sht
                wb.Close False
            End If
        End If
    Next f
    For Each fd In ff.subfolders
        Getfd (fd)
    Next fd
End Sub


sub handle_find(arr,pth,f)
For r = 1 To UBound(arr)
For j = 1 To UBound(arr, 2)
If arr(r, j) = findString Then
rem need test
d(pth & "\" & f.name)=""
Exit For
End If
next
next
end sub



TA的精华主题

TA的得分主题

 楼主| 发表于 2023-3-10 11:53 | 显示全部楼层
create temp table aa as
select *,row_number() over (order by random()) 序 from 员工名单随机分组后进行排班 where rowid<19 order by random();
create temp table bb as
select '班次'||((序-1)/6+1)  班次,'星期'||((序-1)%6+1) 星期,员工名单 from aa;
//select rowid,* from bb where rowid<19;
create temp table cc as
select row_number() over () old_rowid,员工名单 `班次4` from 员工名单随机分组后进行排班 where rowid>=19;
//select * from cc;
cli_create_two_dim~bb~班次~员工名单;

select * from (select row_number() over (order by random()) old_rowid,* from bb_two_dim ) a left join cc on a.old_rowid=cc.old_rowid order by 星期;


这道题不容易,记录一下
https://club.excelhome.net/forum.php?mod=viewthread&tid=1656116&fromguid=hot&extra=&mobile&_dsign=e883fff0


Screenshot_2023-03-10-11-49-21-561_cn.uujian.browser.jpg
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-9-30 02:17 , Processed in 0.032593 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表