Create new workbook with search results

ChaosPup

New Member
Joined
Sep 27, 2021
Messages
48
Office Version
  1. 365
Platform
  1. Windows
Hi everyone, I'm using the code below to search through 3 (closed) workbooks in a folder for user defined text. It works great, but at the moment it creates a new sheet with the search results and I'd prefer to create a new workbook with the search results. I've tried changing the definition of xOut to Workbooks (and Workbooks.Add) but I cant seem to get it to work - any suggestions?

VBA Code:
Sub SearchCARs_CAPAs()
    
    Dim xFso As Object
    Dim xFld As Object
    Dim xStrSearch As String
    Dim xStrPath As String
    Dim xStrFile As String
    Dim xOut As Worksheet
    Dim xWb As Workbook
    Dim xWk As Worksheet
    Dim xRow As Long
    Dim xFound As Range
    Dim xStrAddress As String
    Dim xFileDialog As FileDialog
    Dim xUpdate As Boolean
    Dim xCount As Long
    
    On Error GoTo ErrHandler
    
    xStrPath = "P:\01 Departments\Quality\01 Personnel Directories\Andy Murray\SUPERCEDED SEARCH"
        
    xStrSearch = Sheets("SEARCH").Range("D8")
    xUpdate = Application.ScreenUpdating
    Application.ScreenUpdating = False
    Set xOut = Worksheets.Add
    xRow = 1
    With xOut
        .Cells(xRow, 1) = "Workbook"
        .Cells(xRow, 2) = "Text in Cell"
        Set xFso = CreateObject("Scripting.FileSystemObject")
        Set xFld = xFso.GetFolder(xStrPath)
        xStrFile = Dir(xStrPath & "\*.xls*")
        Do While xStrFile <> ""
            Set xWb = Workbooks.Open(Filename:=xStrPath & "\" & xStrFile, UpdateLinks:=0, ReadOnly:=True, AddToMRU:=False)
            For Each xWk In xWb.Worksheets
                Set xFound = xWk.UsedRange.Find(xStrSearch)
                If Not xFound Is Nothing Then
                    xStrAddress = xFound.Address
                End If
                Do
                    If xFound Is Nothing Then
                        Exit Do
                    Else
                        xCount = xCount + 1
                        xRow = xRow + 1
                        .Cells(xRow, 1) = xWb.Name
                        .Cells(xRow, 2) = xFound.Value
                    End If
                    Set xFound = xWk.Cells.FindNext(After:=xFound)
                Loop While xStrAddress <> xFound.Address
            Next
            xWb.Close (False)
            xStrFile = Dir
        Loop
        .Columns("A:D").EntireColumn.AutoFit
    End With
    MsgBox xCount & " entries have been found"
ExitHandler:
    Set xOut = Nothing
    Set xWk = Nothing
    Set xWb = Nothing
    Set xFld = Nothing
    Set xFso = Nothing
    Application.ScreenUpdating = xUpdate
    Exit Sub
ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi ChaosPup
All you need to do is change this line
VBA Code:
Set xOut = Worksheets.Add

to:
VBA Code:
Set xOut = application.Workbooks.Add.Sheets("Sheet1")
 
Upvote 0
Hi ChaosPup
All you need to do is change this line
VBA Code:
Set xOut = Worksheets.Add

to:
VBA Code:
Set xOut = application.Workbooks.Add.Sheets("Sheet1")
Hi, thanks for replying. I tried this, but I get a 'Subscript out of Range' error. It does create the new workbook, but doesn't populate it
 
Upvote 0
Hi, thanks for replying. I tried this, but I get a 'Subscript out of Range' error. It does create the new workbook, but doesn't populate it
My bad, I was trying to rename the new sheet when I created the workbook.
 
Upvote 0
Hi ChaosPup
All you need to do is change this line
VBA Code:
Set xOut = Worksheets.Add

to:
VBA Code:
Set xOut = application.Workbooks.Add.Sheets("Sheet1")
This works perfectly. I'm maybe pushing my luck here, but do you know if there's a way I could make the output a hyperlink to the result?
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top