在文件夹中的所有文件中搜索大量字符串

马科西斯知道

我有一个文件夹,里面有我们公司、利物浦、曼彻斯特等所有分支机构的受密码保护的工作簿(相同的密码)。

每个工作簿中有一个简单的表格,显示销售数据、销售编号、姓名、电子邮件地址、股票代码等,有时在每个工作簿的 3 个工作表上

然后我有一个主工作簿,其中包含一个股票代码列表。

我需要能够创建一个宏来搜索 F 行中的所有工作簿,如果它找到任何股票代码的匹配值,请复制该行并将其粘贴到主工作簿中标题为工作表与它所在的文件名同名,数据行贯穿始终。

我有类似的东西搜索一个关键短语并返回一行,但我需要它来搜索一整行字符串中的任何字符串。

Sub STBP()
    Application.ScreenUpdating = False
    Dim desWS As Worksheet, srcWS As Worksheet, wkbSource As Workbook, response As String, LastRow As Long
    Set desWS = ThisWorkbook.Sheets("Sales to be Processed")
    response = InputBox("Please enter the search string.")
    If response = "" Then Exit Sub
    Const strPath As String = "C:\Users\marc.delaney\Documents\TestSave\" 'change folder path to suit your needs
    ChDir strPath
    strExtension = Dir(strPath & "*.xls*")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            LastRow = desWS.Range("C" & Rows.Count).End(xlUp).Row
            Set srcWS = .Sheets("Sales To Be Processed")
            srcWS.Unprotect Password:="cgeod18"
            With srcWS.Cells(7, 2).CurrentRegion
                .AutoFilter Field:=12, Criteria1:="=*" & response & "*"
                desWS.Range("A" & LastRow + 1) = wkbSource.Name
                srcWS.AutoFilter.Range.Offset(1, 0).Copy desWS.Cells(desWS.Rows.Count, "B").End(xlUp).Offset(1, 0)
            End With
            .Close SaveChanges:=False
        End With
        strExtension = Dir
    Loop
    Columns.AutoFit
    Application.ScreenUpdating = True
 End Sub
维塔利·普鲁沙克

正如我在评论中所写的那样,您可以将您的任务分解为多个较小的任务并按部分进行搜索。

这是上面所说的一个例子 - 你的问题一步一步:
1. 取一个文件夹,里面有文件;
2. 将每个工作簿一一打开;
3.查看打开的工作簿中的每个工作表(涵盖以前任务的链接(2d post));
4. 查看该工作簿每张纸上的特定范围;
5. 如果搜索匹配,则复制一行。

这是我的答案。我试图在评论中解释每一步,但如果仍有不清楚的地方 - 在评论中提问。
代码前的一些注释(代码中都有注释):
- 主工作簿名称中的新工作表可能有问题
- 您必须决定是要为每个源工作表还是每个源工作簿使用一个目标表

代码(可能看起来太长太复杂,但这主要是由于注释):

Sub STBP()
Dim FileItem As Object
Dim oFolder As Object
Dim FSO As Object
Dim BrowseFolder As String

Dim checkRange As Range, r As Range ' check range and a variable for loop through it

Dim masterBook As Workbook  ' a master workbook
Dim sourceBook As Workbook  ' a source workbook
Dim sht As Worksheet        ' variable to loop through sheets
Dim targetSheet As Worksheet    ' sheet to copy found data to

Dim searchPattern As String    ' a string for search

' get the stockcode and check that it is entered
searchPattern = Application.InputBox(prompt:="Please enter the search string.", Title:="Enter the stockcode", Type:=2)
If searchPattern = "" Then Exit Sub

' refactor search pattern
searchPattern = "*" & searchPattern & "*"


' if the code is entered - start using memory for needed stuff
Set masterBook = ThisWorkbook
Set FSO = CreateObject("Scripting.FileSystemObject")

    ' get the folder with File Dialog - don't need to hard code that
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select the folder with source files"
        If Not .Show = 0 Then
            BrowseFolder = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With

' switch off screen blinking
Application.ScreenUpdating = False

Set oFolder = FSO.GetFolder(BrowseFolder)

' loop through each file in folder
For Each FileItem In oFolder.Files
    ' if this is an excel file
    If FileItem.Name Like "*.xls*" Then
        ' open it for processing
        Set sourceBook = Workbooks.Open(BrowseFolder & Application.PathSeparator & FileItem.Name)

        ' loop through each sheet
        For Each sht In sourceBook.Sheets
            ' unprotect sheet and set a search range (column "F")
            With sht
                .Unprotect Password:="cgeod18"
                Set checkRange = Range(.Cells(1, 6), .Cells(Rows.Count, 6).End(xlUp))
            End With

            ' loop through each cell in column "F"
            For Each r In checkRange
                ' check whether cell value contains the pattern
                If r.Text Like searchPattern Then
                    ' create new sheet in master workbook if there is a match
                    ' and a new target sheet for this workbook is not created yet
                    If targetSheet Is Nothing Then
                        Set targetSheet = masterBook.Sheets.Add(after:=masterBook.Sheets(masterBook.Sheets.Count))
                        ' rename target sheet to "Workbook_name-Sheet_name" to avoid same sheets naming
                        targetSheet.Name = FileItem.Name & "-" & sht.Name
                    End If

                    ' copy data
                    ' check whether first row has data
                    If targetSheet.Cells(Rows.Count, 1).End(xlUp).Row = 1 And targetSheet.Cells(Rows.Count, 1).End(xlUp).Value = "" Then
                        r.EntireRow.Copy Destination:=targetSheet.Cells(1, 1)
                    Else
                        r.EntireRow.Copy Destination:=targetSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
                    End If
                End If
            Next
            ' protect the sheet back
            sht.Protect Password:="cgeod18"

            ' use one target sheet per one source sheet
            ' uncomment next line if this is what you need and remove same line below
            'Set targetSheet = Nothing
        Next
        ' close the source workbook
        sourceBook.Close SaveChanges:=False
    End If

    ' use one target sheet per one source workbook
    ' remove this line if you'd like to use one target sheet per one source sheet
    Set targetSheet = Nothing
Next

' set screen updating to normal state
Application.ScreenUpdating = True
End Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在文件夹中的所有文件中搜索字符串

在文件夹及其子文件夹内的所有文件中搜索字符串的vba

批处理文件以搜索文件夹中的所有.csv文件以查找字符串

在文件夹及其所有子文件夹的所有文件中搜索并替换另一个子字符串

shell脚本:通过搜索文件夹中的所有文件来查找字符串?

如何使用Bash搜索路径中包含给定子字符串的所有文件/文件夹?

Powershell更新所有子文件夹中每个文件中的字符串

如何在也有zip文件的文件夹中搜索字符串

将包含字符串的所有文件从多个子文件夹移动到父文件夹中

替换文件夹中所有文件中的字符串

如何获得否。与文件夹中所有文件中的字符串匹配的行数

重命名文件夹中的所有文件,删除重复的字符串部分

用powershell替换文件夹中所有文件中的字符串

Powershell替换文件夹中所有文件中的字符串

cmd-在文件夹中的所有文件中搜索字符串-找到时返回文件名

如何替换文件夹中Word文档中所有出现的字符串

读取文件夹中的所有JSON并获取其字符串

在战争中的所有文件中搜索字符串

使用 Python 在文件夹和子文件夹中的每个文件中搜索字符串

如何删除文件名中包含特定字符串的文件夹和所有子文件夹中的文件?

VBA - Excel - 在文件夹中的多个文件中搜索多个字符串

在所有.txt文件中搜索字符串

在特定扩展名的文件中搜索字符串后提取文件夹名称

在文件夹bash中以字符串开头的所有文件名上调用脚本

Bash脚本,找到当前文件夹中包含特定字符串的所有文件

如何在所有文件夹和文件名中替换字符串

ExcelVBA-替换指定文件夹中所有文件名中的字符串

在Windows文件夹和子文件夹中动态和递归搜索字符串

删除文件夹中的所有图像,但标题中带有定义字符串的图像除外?