Copy one excel file contents to the end of the other excel file - vba

How do i copy one excel file to another excel file using VBA How do i mention the paths of the two excel files?
This is what i found on the internet:
ActiveSheet.Move After:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count)
This is what i saw but im unable to make it can anyone tell me the way i can make it?
I have to copy all the rows and columns of one excel file to the end of the other excel file.
How do I make it?

If you want to move (or copy) worksheets from a workbook to another, you have to set the other workbook in a var. Let me give you an example:
Dim oNewWB as Workbook
Dim oCurWB as Workbook
'Store active workbook in a var
Set oCurWB = ActiveWorkbook
'Open a new workbook
Set oNewWB = Application.Workbooks.Open(<pathToWorkbookHere>)
'Move "Sheet1" from the current workbook to new one
oCurWB.Sheets("Sheet1").Move After:=oNewWB.Sheets(oNewWB.Sheets.Count)
[EDIT] New piece of code as suggested by Maverik
Let's say you want to copy every sheet in your workbook to the new one:
Dim oNewWB as Workbook
Dim oCurWB as Workbook
Dim Sheet as Worksheet
'Store active workbook in a var
Set oCurWB = ActiveWorkbook
'Open a new workbook
Set oNewWB = Application.Workbooks.Open(<pathToWorkbookHere>)
For Each Sheet In oCurWB.Sheets
'Move "Sheet1" from the current workbook to new one
Sheet.Move After:=oNewWB.Sheets(oNewWB.Sheets.Count)
Next Sheet
HTH

Related

Trying to copy specific (or all) sheets from active workbook to another existing workbook

Dim input_Path As String
Dim summary_file As String
input_Path = Sheet1.TextBox1.Text
Workbooks.Open input_Path
summary_file = Sheet1.TextBox3.Text
Workbooks.Open summary_file
Worksheets(Array("Summary", "Top Reasons")).Copy after:=Workbooks(input_Path).Worksheets(Worksheets.Count)
End sub
Trying to copy specific sheets from active workbook to another existing workbook. Both the workbooks are opened through folder picker with variable.
i am unable to copy the sheets.
can someone pls help
Try:
For each worksheet in Workbooks("yourworkbook").worksheets
Your CopyCode
Next Worksheet

VB how to copy a sheet from one workbook to another

I'm having trouble copying a sheet from one workbook to another using visual basic.
My code so far opens up a blank workbook and it opens up another excel file with the sheet I want to copy over. Here's what I got:
'This creates the new workbook
Set NewBook = Workbooks.Add
NewBook.Title = MyRecordset.Fields(1) & "_Tables"
'This creates a sheet named "2-13"
Sheets.Add().name = "2-13"
'This will open up the existing excel file with the sheet i want to copy over
Set XlApp = New Excel.Application
XlApp.Visible = True
XlApp.Workbooks.Open (CurrentProject.Path & "\Charts\" & MyRecordset.Fields(1) & "_Charts")
Set wBook = XlApp.Workbooks(XlApp.Workbooks.Count)
Set wSheet2 = wBook.Sheets("L1")
So, NewBook is the blank excel file and wBook is the excel file that has the Sheet called "L1" that I want to copy over to NewBook in the sheet called "2-13". Any help would be greatly appreciated! Thanks
To copy the contents of one worksheet to another:
' Get contents of worksheet.
wBook.Sheets("L1").UsedRange.Copy
' Paste contents into new sheet.
NewBook.Sheets("2-13").Paste

Excel Copy active sheet and specified sheets to new workbook

im trying to copy the active sheet and 2 specified sheets to a new workbook and then have the macro to continue to run on the new workbook to change a few things before i save it
i want to copy the sheets by the sheets codenames
1st Sheet 3 to copy this will be the "active sheet"
2nd Sheets codename is "DropDown_Sheet_1_VB"
3rd Sheets codename is "Control_Sheet_VB"
Example Code.
Sub Create_Work_Order()
Workbooks.Add
ActiveSheet.Copy 'copy to new workbook
DropDown_Sheet_1_VB.Copy 'also copy to new workbook
Control_Sheet_VB.Copy 'also copy to new workbook
Dim NewWB As Workbook
NewWB.ActiveSheet.Range("A1").Copy 'do stuff on new workbook
NewWB.Save
End Sub
I don't know what is the stuff at the end other than copying A1 range but I put it there too.
You simply need to assign the workbook object (wrk in my sample, NewWB in your sample - variable name doesn't matter) at the beginning and tell the Copy method where to copy. You will see that I specified first parameter - which is :Before - as wrk.Worksheets(1), so copied sheets are copied just before the first worksheet in the new workbook.
Sub Create_Work_Order()
Dim currentWrk As Workbook
Dim wrk As Workbook
Set currentWrk = ActiveWorkbook
Set wrk = Workbooks.Add
currentWrk.ActiveSheet.Copy wrk.Worksheets(1)
DropDown_Sheet_1_VB.Copy wrk.Worksheets(1)
Control_Sheet_VB.Copy wrk.Worksheets(1)
'wrk.ActiveSheet.Range("A1").Copy 'do stuff on new workbook
wrk.Save
' EDIT: I added following to change the functions to refer to the new file
wrk.ChangeLink currentWrk.FullName, wrk.FullName, xlExcelLinks
wrk.Save
End Sub

Create a Copy of Sheets from existing (unknown named) workbook into a new (unknown named) workbook

I have an existing workbook that will be used by multiple users (who will name the workbook uniquely - I can set one Workbook Codename if needed though, though don't know how to do this?).
I need to create a macro that opens a new workbook (which presumably I won't know the name of? as it could be 'Book1', 'Book2', 'Book3' etc?), then returns to the original workbook where the macro is stored, and copies several (can do one at a time if needed) sheets (that I DO know the names of these sheets) and pastes them as new sheets into the new workbook that I created at the start. The macro does not need to Save the file (in fact it's preferable that it doesn't as I want the user to save the new workbook wherever is most convenient for the user).
I have attempted to show what the macro would do, showing the obvious problem that I do not know the names of the workbooks I am creating/copying from/pasting into.
Any help, much appreciated!
Sub CopySheetintoNewWorkbook()
'Macro opens new / blank workbook (name unknown?)'
Workbooks.Add
'Macro goes back to original workbook where macro is saved (of which the name is unknown to the macro - i.e., users can and will change it)'
Windows("UnknownWorkbookName-1").Activate
'Macro goes to a sheet which can be named and will be known, so this is no problem'
Sheets("KnownSheet").Select
'Macro creates a copy of the sheet and pastes it as a new sheet within the new, unknown named workbook'
Application.CutCopyMode = False
Sheets("KnownSheet").Copy Before:=Workbooks("UnknownWorkbookName-2").Sheets(1)
End Sub
We want to copy Sheet1 and Sheet2.
This relies on a tiny trick:
Sub qwerty()
Dim wb1 As Workbook, wbNEW As Workbook
Set wb1 = ActiveWorkbook
Sheets("Sheet1").Copy
Set wbNEW = ActiveWorkbook
wb1.Sheets("Sheet2").Copy after:=wbNEW.Sheets(1)
End Sub
When the first .Copy is performed, a new workbook is created and it becomes the ActiveWorkbook ........the rest is easy.
EDIT#1:
If we have a group of sheets to be copied, then we can create an array of sheet names and loop through the array, copying one sheet at a time:
Sub qwerty()
Dim wb1 As Workbook, wbNEW As Workbook
Dim ary() As String, s As String, i As Long
s = "Larry,Moe,Curly"
ary = Split(s, ",")
Set wb1 = ActiveWorkbook
i = 1
For Each a In ary
If i = 1 Then
Sheets(a).Copy
Set wbNEW = ActiveWorkbook
Else
wb1.Sheets(a).Copy after:=wbNEW.Sheets(1)
End If
i = 2
Next a
wbNEW.Activate
End Sub

EXCEL VBA: Copy Sheet from a workbook to another workbook in different location

I am trying to copy a whole sheet from one Excel file to a sheet in another. Following is the code I wrote which doesn't work. Please suggest changes.
Sub copyallwos()
Dim wkbSource As Workbook
Dim wkbDest As Workbook
Dim shttocopy As Worksheet
Dim wbname As String
Set wkbSource = Workbooks.Open("C:\Users\AV\Documents\New folder\SCADA Wos.xlsm")
Set wkbDest = Workbooks("C:\Users\AV\Documents\New folder\MASTER.xlsm")
'perform copy
Set shttocopy = wkbSource.Sheets("tt")
shttocopy.Copy
wkbDest.Sheets("SCADAWOs").Select
ActiveSheet.Paste
End Sub
Try this under 'perform copy
wkbSource.Sheets("tt").Copy After:=wkbDest.Sheets("SCADAWOs")
You can also insert the sheet before your "SCAD..." sheet, just change After:= to Before:=. Also, if you don't necessarily know a sheet name in the destination workbook, you can use After:=Wkbdest.sheets(sheets.count) which will instert it after the last Worksheet.