I am trying to write a macro to copy worksheets into a new Workbooks using the .Copy(MSDN) method and then save and email these newly created files out.
To do this I will need a reference to the newly created worksheet in my macro. I haven't found a way to do it directly with the copy and am hesitant to always look for Book1.xlsx.
Is there a way to grab the most recently opened workbook or easily compare before and after collections of workbooks?
You can tell the worksheet Copy method to place the sheet Before/After a sheet in another workbook. So create a new workbook and then copy your sheet to before the first sheet in the new workbook.
Dim newBook As Workbook
Set newBook = Workbooks.Add
Workbooks("source_book.xlsx").Worksheets("sheet_name").Copy Before:=newBook.Worksheets(1)
You've then got a valid workbook reference to the book that holds the copy of the sheet.
Oh alright then.
Dim origBook As Workbook, newBook As Workbook
Set origBook = Workbooks.ActiveWorkBook
yourcode..yourcode..yourcode.Copy
Set newBook = Workbooks.ActiveWorkBook
Something like that.
Related
I am trying out Keikai Spreadsheet. After importing an excel file, I wanted to copy a sheet using copyToEnd() but it deletes my original sheet. Am I doing it wrong?
Workbook srcBook = spreadsheet.imports("book1", new File(BOOK_FOLDER, "book1.xlsx"));
srcBook.getWorksheet().copyToEnd(spreadsheet.getWorkbook());
Currently,spreadsheet.import() will change active Workbook to the just imported book; i.e. in this case book1.xlsx. So the original sheet is not deleted but just not active ( invisible).
Therefore, the srcBook actually equals to spreadsheet.getWorkbook() after importing. When you copy the srcBook.getWorksheet() to spreadsheet.getWorkbook(), you copy a sheet in the same book. You will see 2 duplicate sheets in the active book.
The workaround is:
final Workbook book = spreadsheet.getWorkbook();
Workbook srcBook = spreadsheet.imports("book1.xlsx", new File(BOOK_FOLDER, "book1.xlsx"));
srcBook.getWorksheet().copyToEnd(book);
spreadsheet.setActiveWorkbook(book.getName());
System.out.println("currentBook: spreadsheet.getWorkbook():" + spreadsheet.getWorkbook().getName());
In the future version, spreadsheet.imports() should not change active Workbook automatically unless it is the only workbook that bound to the application.
I want to copy all the data and paste it in new workbook. With the below coding, I am able to paste all the values but it is creating two workbooks and pasting the data in one workbook.
I want to create only one new workbook and past the data. Not sure as to what went wrong.
On Error Resume Next
ThisWorkbook.Sheets(2).Copy
Dim wkb As Workbook
Set wkb = Workbooks.Add
wkb(1).PasteSpecial xlPasteValues
On Error GoTo 0
The Copy method has a different meaning when applied to a Worksheet and when applied to a Range:
The Copy method of a Worksheet creates a copy of the sheet; in the absence of any parameter, the copy is placed in a new workbook.
The Copy method of a Range puts a copy of the Range on the clipboard, from where you can then Paste it somewhere else.
So in your case, the statement
ThisWorkbook.Sheets(2).Copy
already makes a copy the Worksheet into a new workbook.
If you want to create the new workbook explicitly then you should copy the used range to the clipboard:
ThisWorkbook.Sheets(2).UsedRange.Copy
It's not the answer, but let me do it instead of yourself:
On Error Resume Next
ThisWorkbook.Sheets(2).Copy
Dim wkb As Workbook
Set wkb = Workbooks.Add
wkb(1).PasteSpecial xlPasteValues
On Error GoTo 0
.
I'm very new to VBA macros and have taught myself some code but I'm struggling with my current piece of work and can't find the answer I am looking for.
I want to copy a range of cells (B3:N21) from one workbook to another "master" workbook - which seems simple enough - but I would like it to copy into a blank/new worksheet in the Master copy every time the Macro is run.
The range contains formulas, I would only need the values copied to the Master workbook.
Any help with this would be greatly appreciated.
Thanks
Worksheets("Sheet1").Range("C1:C5").Copy
Worksheets("Sheet2").activate
Worksheets("Sheet2").Range("D1:D5").PasteSpecial _
Operation:=xlPasteSpecialOperationAdd
End With
I think you only need paste special, this is an example
try this
Option Explicit
Sub main()
Dim masterWb As Workbook
Dim mySht As Worksheet
Set mySht = ThisWorkbook.ActiveSheet '<~~ assuming you're copying values from active worksheet of the workbook the macro resides in
' beware: if you start the macro while the active sheet is not the one you want, this will lead to unespected results
Set masterWb = Workbooks("Master") '<~~ Change "Master" with whatever name your master workbook must have
' beware: we're assuming "Master" workbook is already open, otherwise this line will throw an error
With masterWb.Worksheets.Add
.Range("B3:N21").Value = mySht.Range("B3:N21").Value
End With
End Sub
mind the comments
the code above can be reduce to a much less verbose (and self explanatory, too) one like follows
Sub main2()
Workbooks("Master").Worksheets.Add.Range("B3:N21").Value = ThisWorkbook.ActiveSheet.Range("B3:N21").Value
End Sub
where apply the same comments of the lengthy code, which is:
assuming you're copying values from active worksheet of the workbook the macro resides in
beware: if you start the macro while the active sheet is not the one you want, this will lead to unespected results
change "Master" with whatever name your master workbook must have
beware: we're assuming "Master" workbook is already open, otherwise an error would be thrown
Im wondering if it's possible to reference an excel sheet from another work book without making a copy of that sheet?
The situation : I have some very large worksheets filled with various data, but i don't want to keep a copy of them open in my workbooks because while each workbook uses the same data source, they're slightly different.
I have a vba routine that takes this data and creates input files for other codes, vba expects this data to be available on the defined sheet names.
Is it possible to make either excel or vba to know that when i request worksheet("Example_1") it instead knows that i mean example_1 from a different workbook?
Thanks
Yes, it is possible.
You need to add those lines to your code:
Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet
Set wkb = Excel.Workbooks("name_of_workbook.xlsx")
Set wks = wkb.Worksheets("Example_1")
Now, every time you want to refer to a range from this other workbook, you need to add wks. before, i.e.:
'Printing value in cell.
wks.Range("A1") = "x"
'Selecting range.
call wks.Range(wks.Cells(1,1), wks.Cells(2,2)).Select
=SUM('C:\test\[test.xlsx]sheet_name'!A1:A25)
is an example of a formula which references sheet sheet_name in workbook C:\test\text.xlsx.
Note that when the other workbook is opened, the formula automatically changes to
=SUM([test.xlsx]sheet_name!A1:A25)
and then when it is closed, the formula will change back.
I am writing a macro that will copy and paste information form one workbook into another workbook in excel 2010. The workbook that the data is in is the same workbook as the macro. I have made VBA create a new workbook to paste the data in. How do I assign the new workbook that VBA has just created to a variable.
Thanks For Any Help
You haven't mentioned exactly how you create the workbook, but you can set a reference to the new Workbook object in the same statement that creates it.
Example:
Option Explicit
Sub AddWorkbook()
Dim oWb As Workbook
Set oWb = Workbooks.Add
'Do something with the new workbook
Debug.Print oWb.FullName
Set oWb = Nothing
End Sub
try seeing names of all workbooks by iterating over Workbooks. I think the name of newly created workbook will "Workbook1" until there is already no other unnamed workbook. So basically newly created workbook is still not unnamed.