Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

VBA closing file and new sheet opener

0

Hello, I'll leave my program below. If I want to search a name from a file, in multiple files, when I copy and paste it in the search field, when I hit search, macro starts and closes the file where I got the name from. How can I make it to leave the file opened, is it possible? Because my files are huge and it takes 1-2 minutes to open, so I kinda need them to stay opened:)

And also, can I modify somehow to instead of returning into the same sheet, to open a new one with the name I searched as title? So I can come back at the values if I need to. Thanks!

Answer
Discuss

Discussion

Squishy.

Regarding you first paragraph, are you always searching in the same files (or need to change them throughout the day)?

I don't understand your second paragraph- do you mean start a search from file A (looking at files B, C and D say) then give the results in file A in a new sheet called "TPA_DCB_CU_SCC_ST3" say?

I'll try to look at this later today
John_Ru (rep: 6142) Dec 6, '21 at 5:46 am
1) I change them through the day
2) Start a search from file A (looking at files A,B,C,D etc) and then return results in a new sheet called "TPA_DCB_CU_SCC_ST3" (the name I search). The sheet must be in the "ultimul excel.xlsm" workbook, after the dashboard.
Squishy (rep: 18) Dec 6, '21 at 6:08 am
1) Suggest open a set of files then search but only close set when "Search same files?" is answered "no" (in which case close set and open/ reopen for new set)
2) Overwrite such a sheet if it already exists from a previous search?
John_Ru (rep: 6142) Dec 6, '21 at 6:26 am
I don't quite understand what you mean in 1)
And 2) - no, at every search open a new sheet with their own results, named after the name we searched
Squishy (rep: 18) Dec 6, '21 at 6:28 am
1)  I mean you open some files and do several searches perhaps. That set of files is closed when you close the workbook or say you don't wan to to search through that same set again. Or would you rather open a bunch of large files and pick which of those a particular serach is done on?

2) Okay but you can't do that if you already created a sheet with the same search name (whih is why I asked about overwriting)
John_Ru (rep: 6142) Dec 6, '21 at 6:54 am
1)  I mean you open some files and do several searches perhaps. That set of files is closed when you close the workbook or say you don't want to search through that same set again (then open a new set). Or would you rather open a bunch of large files and pick which of those a particular serach is done on?

2) Okay but you can't do that if you already created a sheet with the same search name (whih is why I asked about overwriting)
John_Ru (rep: 6142) Dec 6, '21 at 6:54 am
1) if I search something from file A, it will search in A,B,C,D etc and it will leave the file A opened, so I can pick another name to search.
2) I wanted it after i hit search, to create a new sheet automatically with the returned data. or do I need to create it manually?
Squishy (rep: 18) Dec 6, '21 at 7:00 am
Squishy, we are straying too far from the Q&A format here and I have other things to do! I'll try to find time to give a modified macro to:

1) open a set of files and keep them open to allow several searches
2) save results to a new sheet (definitely possible as I said, subject to there NOT already being a sheet with that name).
John_Ru (rep: 6142) Dec 6, '21 at 7:16 am
It's ok, sorry, I don't want to be in your way. When you have time and want to, you can help me:) Thanks:)
Squishy (rep: 18) Dec 6, '21 at 7:22 am
Add to Discussion

Answers

0
Selected Answer

Squishy

Please try this but note it might not be perfect yet...

1) In the attached revised file, I've defined a collection Repfiles which allows you to use the same set of files for a few searches or erase that and open a new set. The macros are modified so the search isn't done by opening each file rather it looks at the open files (or at least those listed in RepFiles (which are hidden on opening).

2) This version saves the results to a sheet named like the searchValue (having first deleted any sheets which is that already)

To do this, I've declared RepFiles at a Module level (so it's there after an individual macro has run:

Public RepFiles As Collection

and moved the process of picking files to another sub (where the section in bold builds RepFiles):

Sub FilestoRepFiles()
Dim filePicker As FileDialog
 create collection to hold file names
Set RepFiles = New Collection

Set filePicker = Application.FileDialog(msoFileDialogFilePicker)

With filePicker
    .Filters.Clear
    .Filters.Add "Excel Files", "*.xl*"
    .AllowMultiSelect = True

End With

filePickerButtonClicked = filePicker.Show

If filePickerButtonClicked = -1 Then

    For Each fileselected In filePicker.SelectedItems
        RepFiles.Add fileselected
    Next fileselected

    For n = 1 To RepFiles.Count
        ' open file and hide from view
        Workbooks.Open (RepFiles(n))
        Windows(Dir(RepFiles(n))).Visible = False
    Next n
End If

End Sub

This could be slow if your have very large files.

Then the Search macro becomes this (changes in bold):

Sub Search_Separate_Workbooks()

Dim wbMaster As Workbook
Dim wbSlave As Workbook
Dim ws As Worksheet, wbReport As Worksheet, ReportNm As String

On Error Resume Next
ReportNm = Worksheets("Dashboard").Range("F6")

If RepFiles Is Nothing Then
    Call FilestoRepFiles ' open files and save to collection

    Else:
    r = MsgBox("Click No to close them and define a new set", vbYesNo, "Work with the same set of files?")
    If r = vbNo Then
        Call CloseRepFiles ' close existing file set
        Call FilestoRepFiles ' open files and save to collection
    End If
End If

Application.ScreenUpdating = False

'remove any sheet with same search name
Application.DisplayAlerts = False 'prevent create alert
For Each ws In ThisWorkbook.Sheets
    If ws.Name = ReportNm Then ws.Delete
Next
    Application.DisplayAlerts = True
' add new sheet for results, with search name and set for results
ThisWorkbook.Sheets.Add(After:=Sheets("Dashboard")).Name = ReportNm

Set wbReport = Worksheets(ReportNm)

'Call Clear_data

Set wbMaster = ActiveWorkbook

For n = 1 To RepFiles.Count
        ' search file (removing path using Dir)
        Set wbSlave = Workbooks(Dir(RepFiles(n)))

        Call Data_Search(wbMaster, Worksheets(ReportNm), wbSlave)

Next n

wbMaster.Activate
ActiveWindow.Visible = True

Application.ScreenUpdating = True

End Sub

which calls the DataSearch macro (extract shown below, just the chnages in bold):

Sub Data_Search(wbMaster As Workbook, wbReport As Worksheet, wbSlave As Workbook)
.
.

.
reportDataColumnStart = 1 '### changed

searchValue = wbReport.Name

For Each ws In wbSlave.Worksheets

.

.

        nextRow = wbReport.Cells(Rows.Count, reportDataColumnStart).End(xlUp).Row + 3
        wbReport.Range(wbReport.Cells(nextRow, reportDataColumnStart), wbReport.Cells(nextRow + UBound(datatoShowArray, 1) - 1, reportDataColumnStart + dataColumnWidth)).Value = datatoShowArray

    End If

Next ws

End Sub

There's also a macro to clear up when a new set of files is needed:

Sub CloseRepFiles()

On Error Resume Next
If RepFiles.Count = 0 Then Exit Sub

For n = 1 To RepFiles.Count
    ' close file
    Workbooks(Dir(RepFiles(n))).Close savechanges:=False
Next n
' clear the collection
Set RepFiles = New Collection

End Sub
You'll need to reapply data validation to F4 in Dashboard.

On downside to this file is that any (hidden) open files aren't closed on closing this file (though they will be closed if you close Excel). That's because RepFiles isn't available to a Workbook_BeforeClose event macro (otherwise Excel could neatly close down only the files it is using).

Hope this works for you.

Discuss

Discussion

Forgot to say that your Clear button serves no purpose now (so you can kill the associated macro too) but you could rename that button "Close open files" and call CloseRepFiles, Filter button is another matter....
John_Ru (rep: 6142) Dec 7, '21 at 3:57 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login