Hi
I currently have a VBA macro where I click on a button and it will copy unformatted data to a new workbook and I have to manually save that new workbook to a specified folder. What I would like to do is instead of manually having to do this before I exit the workbook I would like for it to automatically do this once I click on the exit button. I have tried doing this a few different ways but whatever I do doesn't seem to work. I have a folder named small files that I would like it to be saved in which resides in the same folder I currently have the macro enabled workbooks that I am copying the data from. The following is the code which executes once I click on the button. Rather than this being a manual process I would like it to be automatic as then there will be less chance of me forgetting to copy the data over to the new workbooks.
The entire reason I am doing this is that the workbook I am working in is quite a large file due to formatting, formulas etc which is required to work out the information I need - it comes to approximately 8 megabytes. Being such a large file it takes a few seconds to open and even longer to save and exit. I have another workbook which goes through all of the excel files gathers all of the information I need and consolidates it all into one single file. Going through hundreds of files that take several seconds to open copy the data and close takes a very long time and creating these unformatted smaller files reduces that amount of time by approximately 80% reducing what could take hours down to minutes for much of my work.
Is there a command I can add to my below code that will automatically execute my code below and save it to my folder called "Small Files" once I click the exit button? I will like it to use the same name that the original file is called, if that is possible, and to be able to check if it is already in the folder and ask for it to be overwritten if I need to go back and make any changes to the original file.
I hope I have made sense and that I can get some help on this. Everything I have tried just seems to generate a new file without any data in it
I currently have a VBA macro where I click on a button and it will copy unformatted data to a new workbook and I have to manually save that new workbook to a specified folder. What I would like to do is instead of manually having to do this before I exit the workbook I would like for it to automatically do this once I click on the exit button. I have tried doing this a few different ways but whatever I do doesn't seem to work. I have a folder named small files that I would like it to be saved in which resides in the same folder I currently have the macro enabled workbooks that I am copying the data from. The following is the code which executes once I click on the button. Rather than this being a manual process I would like it to be automatic as then there will be less chance of me forgetting to copy the data over to the new workbooks.
The entire reason I am doing this is that the workbook I am working in is quite a large file due to formatting, formulas etc which is required to work out the information I need - it comes to approximately 8 megabytes. Being such a large file it takes a few seconds to open and even longer to save and exit. I have another workbook which goes through all of the excel files gathers all of the information I need and consolidates it all into one single file. Going through hundreds of files that take several seconds to open copy the data and close takes a very long time and creating these unformatted smaller files reduces that amount of time by approximately 80% reducing what could take hours down to minutes for much of my work.
Is there a command I can add to my below code that will automatically execute my code below and save it to my folder called "Small Files" once I click the exit button? I will like it to use the same name that the original file is called, if that is possible, and to be able to check if it is already in the folder and ask for it to be overwritten if I need to go back and make any changes to the original file.
I hope I have made sense and that I can get some help on this. Everything I have tried just seems to generate a new file without any data in it
VBA Code:
Option Explicit
Sub RunAllMacros()
copySheets
Hidesheet
End Sub
Sub copySheets()
Dim wkb As Excel.Workbook
Dim newWkb As Excel.Workbook
Dim wks As Excel.Worksheet
Dim newWks As Excel.Worksheet
Dim sheets As Variant
Dim varName As Variant
Dim screenUpdateState As String
Dim statusBarState As String
Dim eventsState As String
'------------------------------------------------------------
screenUpdateState = Application.ScreenUpdating
statusBarState = Application.DisplayStatusBar
eventsState = Application.EnableEvents
'turn off some Excel functionality for faster performance
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
'Unhide worksheets
Worksheets("Total Quantities").Visible = xlSheetVisible
Worksheets("VE Total Quantities").Visible = xlSheetVisible
'Define the names of worksheets to be copied.
sheets = VBA.Array("Total Quantities", "VE Total Quantities", "Builder Costings", "Panelling Information")
'Create reference to the current Excel workbook and to the destination workbook.
Set wkb = Excel.ThisWorkbook
Set newWkb = Excel.Workbooks.Add
For Each varName In sheets
'Clear reference to the [wks] variable.
Set wks = Nothing
'Check if there is a worksheet with such name.
On Error Resume Next
Set wks = wkb.Worksheets(VBA.CStr(varName))
On Error GoTo 0
'If worksheet with such name is not found, those instructions are skipped.
If Not wks Is Nothing Then
'Copy this worksheet to a new workbook.
Call wks.Copy(newWkb.Worksheets(1))
'Get the reference to the copy of this worksheet and paste
'all its content as values.
Set newWks = newWkb.Worksheets(wks.Name)
With newWks
ActiveSheet.Unprotect Password:="password"
Call .Cells.Copy
Call .Range("A1").PasteSpecial(Paste:=xlValues)
End With
End If
Next varName
Worksheets("Total Quantities").Range("A1:M295").ClearFormats
Worksheets("VE Total Quantities").Range("A1:M295").ClearFormats
Worksheets("Panelling Information").Range("A1:AA11").ClearFormats
End Sub
Sub Hidesheet()
Excel.ThisWorkbook.Worksheets("Total Quantities").Visible = xlSheetHidden
Excel.ThisWorkbook.Worksheets("VE Total Quantities").Visible = xlSheetHidden
End Sub