VBA Code to run the code in new workbook?

mrnassaro

New Member
Joined
Jan 23, 2015
Messages
27
Office Version
  1. 2016
Platform
  1. Windows


I have a code that I want to run when I click on the button, but I want the results go into completely new sheet that the user can save. is there a line of code to help?

VBA Code:
Sub mergeFiles()

    'Merges all files in a folder to a main file.
   
    'Define variables:
    Dim numberOfFilesChosen, i As Integer
    Dim tempFileDialog As FileDialog
    Dim mainWorkbook, sourceWorkbook As Workbook
    Dim tempWorkSheet As Worksheet
   
    Set mainWorkbook = Application.ActiveWorkbook
    Set tempFileDialog = Application.FileDialog(msoFileDialogFilePicker)
   
    'Allow the user to select multiple workbooks
    tempFileDialog.AllowMultiSelect = True
   
    numberOfFilesChosen = tempFileDialog.Show
   
    'Loop through all selected workbooks
    For i = 1 To tempFileDialog.SelectedItems.Count
       
        'Open each workbook
        Workbooks.Open tempFileDialog.SelectedItems(i)
       
        Set sourceWorkbook = ActiveWorkbook
       
        'Copy each worksheet to the end of the main workbook
For Each tempWorkSheet In sourceWorkbook.Worksheets
If sourceWorkbook.Sheets(tempWorkSheet.Name).Visible = True Then
tempWorkSheet.Copy after:=mainWorkbook.Sheets(mainWorkbook.Worksheets.Count)
End If
Next tempWorkSheet
     
        'Close the source workbook
        sourceWorkbook.Close
    Next i
    End Sub
 

Attachments

  • 1.PNG
    1.PNG
    6.3 KB · Views: 11

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
...but I want the results go into completely new sheet that the user can save.

I'm assuming that by "new sheet" you mean "new workbook"

VBA Code:
'Assign this macro to your button
Sub mergeFiles()
   
    'Merges all files in a folder to a main file.
   
    'Define variables:
    Dim numberOfFilesChosen, i As Integer
    Dim tempFileDialog As FileDialog
    Dim mainWorkbook, sourceWorkbook As Workbook, ResultsWorkbook As Workbook
    Dim tempWorkSheet As Worksheet
   
    Set mainWorkbook = Application.ActiveWorkbook
    Set tempFileDialog = Application.FileDialog(msoFileDialogFilePicker)
    Set ResultsWorkbook = Application.Workbooks.Add
   
    'Allow the user to select multiple workbooks
    tempFileDialog.AllowMultiSelect = True
   
    numberOfFilesChosen = tempFileDialog.Show
   
    'Loop through all selected workbooks
    For i = 1 To tempFileDialog.SelectedItems.Count
       
        'Open each workbook
        Set sourceWorkbook = Workbooks.Open(Filename:=tempFileDialog.SelectedItems(i))
       
        'Copy each worksheet to the end of the main workbook
        For Each tempWorkSheet In sourceWorkbook.Worksheets
            If tempWorkSheet.Visible = True Then
                With ResultsWorkbook
                    tempWorkSheet.Copy after:=.Sheets(.Sheets.Count)
                End With
            End If
        Next tempWorkSheet
       
        'Close the source workbook
        sourceWorkbook.Close SaveChanges:=False
    Next i
   
    ResultsWorkbook.Close SaveChanges:=True
End Sub
 
Upvote 0
Solution
You could try something like this.

It creates a new workbook by copying the very first sheet to be copied without specifying a destination, it then copies every subsequent worksheet being copied to that new workbook.
VBA Code:
Sub mergeFiles()

'Merges all files in a folder to a main file.

'Define variables:
Dim numberOfFilesChosen, i As Integer
Dim tempFileDialog As FileDialog
Dim mainWorkbook, sourceWorkbook As Workbook
Dim tempWorkSheet As Worksheet

    Set tempFileDialog = Application.FileDialog(msoFileDialogFilePicker)

    'Allow the user to select multiple workbooks
    tempFileDialog.AllowMultiSelect = True

    numberOfFilesChosen = tempFileDialog.Show

    'Loop through all selected workbooks
    For i = 1 To tempFileDialog.SelectedItems.Count

        'Open each workbook
        Workbooks.Open tempFileDialog.SelectedItems(i)

        Set sourceWorkbook = ActiveWorkbook

        'Copy each worksheet to the end of the main workbook
        For Each tempWorkSheet In sourceWorkbook.Worksheets
           
            If sourceWorkbook.Sheets(tempWorkSheet.Name).Visible = True Then

                ' if it's the first worksheet to be copied copy it to a new workbook and set that workbook as mainWorkbook
                ' else copy it to mainWorkbook
                If mainWorkbook Is Nothing Then
                    tempWorkSheet.Copy
                    Set mainWorkbook = ActiveWorkbook
                Else
                    tempWorkSheet.Copy after:=mainWorkbook.Sheets(mainWorkbook.Worksheets.Count)
                End If
            End If
        Next tempWorkSheet

        'Close the source workbook
        sourceWorkbook.Close
    Next i
   
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,004
Messages
6,122,656
Members
449,091
Latest member
peppernaut

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