Add 3 steps to merge VBA code

heathball

Board Regular
Joined
Apr 6, 2017
Messages
132
Office Version
  1. 365
Platform
  1. Windows
I am trying to add steps to my main merge VBA code, and I cannot find the solution.


this step to apply to the active workbook... (delete 3 sheets with names)
VBA Code:
Sheets("Sheet1").Delete
Sheets("Sheet2").Delete
Sheets("Sheet3").Delete



I need this next part to apply to ALL SHEETS in the active workbook

these 2 steps (add auto filter for row 1, freeze top row)
VBA Code:
Rows("1:1").Select
    Selection.AutoFilter
    With ActiveWindow
        .SplitColumn = 0
        .SplitRow = 1
    End With
    ActiveWindow.FreezePanes = True




I have tried various things, and none are working.

Any help would be appreciated.


My main code is below



VBA Code:
Sub mergeFiles_v669()
    '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
            tempWorkSheet.Copy after:=mainWorkbook.Sheets(mainWorkbook.Worksheets.Count)
        Next tempWorkSheet
      
        'Close the source workbook
        sourceWorkbook.Close
    Next i
  
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
If you want to apply that code to all worksheets in the workbook, you can do that like this:
VBA Code:
Sub filter_and_freeze_top_row()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        ws.Activate
        Rows("1:1").AutoFilter
        With ActiveWindow
            .SplitColumn = 0
            .SplitRow = 1
        End With
        ActiveWindow.FreezePanes = True
    Next ws
End Sub
 
Upvote 0
If you want to apply that code to all worksheets in the workbook, you can do that like this:
VBA Code:
Sub filter_and_freeze_top_row()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        ws.Activate
        Rows("1:1").AutoFilter
        With ActiveWindow
            .SplitColumn = 0
            .SplitRow = 1
        End With
        ActiveWindow.FreezePanes = True
    Next ws
End Sub
thank you!
I can apply that seperately, which is great.

Is there a way to add the deleting of the 3 sheets, into the main merge code?
 
Upvote 0
I don't see why not. Your code is pretty simple. You should just be able to add it into your code.
Are you having some sort of issue with it?
 
Upvote 0
I don't see why not. Your code is pretty simple. You should just be able to add it into your code.
Are you having some sort of issue with it?
I am not sure exactly where it should go. I assume it needs to be at the start of the code. But there may be a step i am missing.

its the 'active workbook.' part that seems not to work so far

Edit - it would not go at the start of the code, as all sheets cannot be deleted, so its a question of where it goes, and also how.
 
Last edited:
Upvote 0
I don't see why not. Your code is pretty simple. You should just be able to add it into your code.
Are you having some sort of issue with it?
the debug is yellow here
ActiveWorkbook.Sheets("Sheet1").Delete
 
Upvote 0
Why don't you try it and see how it works (that is often the best way to learn)?
If you cannot figure it out, post the code you tried, and we can help you fix things up.
 
Upvote 0
the debug is yellow here
ActiveWorkbook.Sheets("Sheet1").Delete
I don't see that line of code in anything you have posted so far.

What exactly is the error message you getting?
Can you post your entire procedure?
 
Upvote 0
I don't see that line of code in anything you have posted so far.

What exactly is the error message you getting?
Can you post your entire procedure?
Thanks Joe,

The debugger is highlighting....ActiveWorkbook.Sheets("Sheet1").Delete

the only error message was checking the debugger.



VBA Code:
Sub mergeFiles_v669_deletesheets()
    '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
            tempWorkSheet.Copy after:=mainWorkbook.Sheets(mainWorkbook.Worksheets.Count)
        Next tempWorkSheet
     
        'Close the source workbook
        sourceWorkbook.Close
     
    ActiveWorkbook.Sheets("Sheet1").Delete
    ActiveWorkbook.Sheets("Sheet2").Delete
    ActiveWorkbook.Sheets("Sheet3").Delete
 

    Next i
 
End Sub
 
Upvote 0
Did you mean to put the sheet deletion rows AFTER you have closed the Workbook (look at the line of code just above it)?
If you want to delete these three sheets in each workbook, logically speaking, that code would need to occur BEFORE you close each workbook.
 
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