VB how to copy a sheet from one workbook to another - vba

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

Related

Pasting into separate workbook must I save the workbook to get the path first

I have a large macro I am amending for my purpose. The writer of the macro was more skilled than me. The macro at present runs formulas on data gathered from websites and other spreadsheets.
All I want to do is to have "Red Flagged" ranges copied and pasted into a the same new workbook. Can this be done without having to save the new work book?
Here was my initial idea:
Sub CreateNewWB()
With Application
.SheetsInNewWorkbook = 1
.Workbooks.Add
.Sheets(1).Name = "Late"
End With
Set ptrToLate = Application.ActiveSheet.FullName
' MsgBox ("This workbook has name" & Application.ActiveWorkbook.Name)
'MsgBox ("This workbook has Full name" & Application.ActiveWorkbook.FullName)
' MsgBox ("This workbook has path name" & Application.ActiveWorkbook.Path)
' MsgBox ("This workbook has Code name" & Application.ActiveWorkbook.CodeName)
End Sub
At the very top of the VBA code I had put
Dim ptrToLate as String
with the intentions of being able to copy and paste using the new workbooks name as a destination, but I get the error: "Object doesnt support this property or method"
1) Is there a way to append to an unsaved workbook?
2)presently the codename of the new workbook is "thisWorkbook" this confuses me because I thought that thisworkbook referred to the workbook the macros itself is written in
Thank you in advance for your help
All you need to do is set the new workbook to an object and then you can reference it without the path. The variable wrkb will now reference your destination new workbook.
Dim wkb As Workbook
'Adding New Workbook
Set wrkb = Workbooks.Add(1) 'this creates anew workbook with only 1 sheet
wrkb.Sheets(1).Name = "Late"
Workbook("Red Flagged.xlsx").Sheets("redflagged").Range("A1:D2").Copy wrkb.Sheets("Late").Range("A1")
To the question in the title: VBA when pasting into separate workbook must I save the workbook to get the path first?
The answer is YES
If you open a new workbook and write this in the VBEditor, you would see that there would an empty MsgBox. If you save it and then run the code again, the path would be shown:
Public Sub TestMe()
MsgBox ThisWorkbook.Path
End Sub

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

VBA - Changing Copying from a Directory to copying from a server

Apreciate any help and excuse me if my terminology is incorrect.
This is a basic macro that opens a file from loacation A and copies the specified content,
It then pastes the content into the current Workbooks, specified worksheet and cell, that has run this macro.
My question is around the "FileName.csv". This is currently scheduled to be dumped in Location A "V:\Dir1\SubDir1\" periodically.
How would I go about Retrieving this file, "FileName.csv", if I started scheduling it to be dumped in loacation B "http://172.1.2.3/Dir1/SubDir1/FileName.csv" a server of some sort?
I would obviusly just like to edit the existing macro to allow for this change.
Sub CopyCSVFile1()
'workbook to copy from
WBToCopy = "FileName.csv"
'workbook path to copy from
WBpthToCopy = "V:\Dir1\SubDir1\"
'workbook to paste to
'WBToPasteTo = "ResourcesV1.xlsm" not needed here as pasting to active workbook
'workbook sheet to paste to
WBSheetToPasteTo = "Raw1"
''workbook path to paste to
'WBPthToPasteTo = "N:\Engineering\Network Performance\Capacity\SG_GG\SGSN Resources\" ' not needed here as pasting to active workbook
'range to select to copy
RangeToSelectToCopy = "A3:B149"
'cell to paste to
CellToPasteTo = "A3" ' need to work this out before assignment
Dim Merged As Object
Dim Data As Object
Set Data = ActiveWorkbook
'debug.print "ActiveWorkbook.Path = " & ActiveWorkbook.Path
Debug.Print "ActiveWorkbook.Path = " & Data.Path
Sheets(WBSheetToPasteTo).Select ' this is the sheet where you want to paste to
Workbooks.Open Filename:=WBpthToCopy & WBToCopy, local:=True
Set Merged = ActiveWorkbook ' this assigns the current active workbook to merged whish is the one I want to copy from
Range(RangeToSelectToCopy).Select ' this value just for this example should be A4 normally
Selection.Copy
Data.Activate ' this activates the Data workbook which happens to be the workbook where this macro resides
Range(CellToPasteTo).Select ' select where I want to past my data
ActiveSheet.Paste ' paste the data
Application.DisplayAlerts = False
Merged.Close 'SaveChanges = False
Application.DisplayAlerts = True
End Sub
This worked for me:
Sub CopyCSVFile1()
Dim wb As WorkBook, WBToCopy As String, WBpthToCopy As String
'workbook to copy from
WBToCopy = "test.csv"
'workbook path to copy from
WBpthToCopy = "http://127.0.0.1/testpages/"
'open the source workbook
Set wb = Workbooks.Open(WBpthToCopy & WBToCopy)
'...
'do something with wb...
'...
wb.Close False
End Sub

Save a named sheet from one workbook to a new workbook in the same folder

I would like to be able to
take a sheet called "data" in a given workbook called
"original_data.xlsm",
copy it conditionally (using Autofilter or
something else), say only those rows where column C was "dog"
Create a new workbook, in the same folder as the original book, called dog.xlsm and save the copied stuff into
a new sheet called "dog data".
Then repeat with a different filter.So for example copy and
autofilter if column C was "cat" and create a workbook "cat.xlsm", in the same folder as the original book, with a sheet called "cat_data" containing some filtered data.
I've been making incorrect attempts for three days now and would appreciate some help. Here is what I have done so far.
Workbooks.Add
Set wb = ActiveWorkbook
GetBook = ActiveWorkbook.Name
wb.Sheets("data").SaveAs Workbooks(GetBook).Path & "\dog"
Workbooks("dog.xlsx").Worksheets("Sheet1").UsedRange.AutoFilter Field:=3, Criteria1:="=dog"
But it's not working. :(
Looks like you're trying to set wb to "original_data.xlsm", but your first line is making the new workbook the active workbook.
Workbooks.Add
Set wb = ActiveWorkbook
See if this helps.
Sub sheetCopy()
Dim wbS As Workbook, wbT As Workbook
Dim wsS As Worksheet, wsT As Worksheet
Set wbS = ThisWorkbook 'workbook that holds this code
Set wsS = wbS.Worksheets("Data")
wsS.Copy
Set wbT = ActiveWorkbook 'assign reference asap
Set wsT = wbT.Worksheets("Data")
wsT.Name = "Dog Data" 'rename sheet
wbT.SaveAs wbS.Path & "\dog.xlsx" 'save new workbook
wsT.UsedRange.AutoFilter Field:=3, Criteria1:="=dog"
End Sub

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

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