Copy sheet using copyToEnd() - spreadsheet

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.

Related

Find Seprate Sheet name from 100 Workbook and paste into the Master File

Respected All,
My question is below
I have a 50 Workbook in a folder and in every workbook there is a separate Sheet name Call "Data". here i would like to loop form multiple workbook throw loop and find seprate "Data" sheet name in the workbook if there is a sheet called "Data" then Copy that sheet and paste into the Master Work book. is it possible in in VBA if possible please guide me how to do this.

how to copy a sheet from external to current workbook and saving that sheet as a new work book

I am working on an excel automation. I have a main workbook which has two sheets i.e. dashboard and data1. I need to fetch a sheet from another workbook(whose name can be anything i.e its not fixed. consider it as filex.xls) placed in the D: drive and paste the entire sheet to the data1 sheet. I need to perform some operations on that sheet then save data1 sheet as a new workbook in C: drive with the same name as the original file followed by modified (i.e. filexmodified.xls)
To import the data:
Workbooks.Open PATH
Worksheets(1).UsedRange.copy data1.cells(1) 'data1 should be a Worksheet Variable
ActiveWorkbook.Close False
For saving as new Workbook:
ThisWorkbook.SaveAs PATH

Reference an excel sheet from another workbook without copying the sheet

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.

Reference Excel Workbook made by Worksheet.copy Method

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.

Renaming a sheet code name through VBA code (Not through properties)

I have a protected workbook A for the user which does not allow the user to copy over a sheet from another workbook B. In workbook A I consider worksheets 1-19 (only have 13 sheets as of now, I allowed 19 for future expansion of the workbook) as "system sheets" and cannot be deleted or modified. Sheets 20-30 are "non system sheets" where the user can delete and modify as needed.
I'm looking to have an import function where the user can import workbook B that contains 1 sheet into workbook A. workbook A would check to see how many "non system sheets" that currently exists and insert the new sheet at the end with a sheet number that is 20+. For example:
Total sheets in workbook A = 18
Total "system sheets" in A = 13 (sheet 1-sheet 13)
Total "non system sheets" in A = 5 (sheet 20-sheet 24)
If I were to import a new sheet workbook A would assign it as sheet 25
I want VBA to change the code name(sheet number) and not the name of the sheet.
I've tried searching for hows, can someone please point me in the right direction on how to do this? thanks!
In the Excel object model a Worksheet has 2 different name properties:
Worksheet.Name
Worksheet.CodeName
the Name property is read/write and contains the name that appears on the sheet tab. It is user and VBA changeable
the CodeName property is read-only
You can reference a particular sheet as Worksheets("Data").Range("A1") where Data is the .Name property or as Sheet1.Range("A1") where Sheet1 is the codename of the worksheet.
edit:
You can change the CodeName Property by accessing the VBA Project Model Components Extensibility.
ThisWorkbook.VBProject.VBComponents(Sheets("Sheetname").CodeName).Name = "Sheet" & Workbook.Sheets.Count
Just be sure to have the programmatic access to visual basic project.
File -> Options -> Trust Center -> Trust Center Setttings -> Macro Settings -> Trust Access to the VBA Project object model.
In your specific example, you could modify the CodeName in Workbook B before the user imports it to Workbook A - because if you copy a sheet to another workbook, it retains its CodeName. Otherwise you have to get into modifying the VBProject itself, e.g.:
<Workbook>.VBProject.VBComponents(<Workbook>.Sheets(<SheetName>).CodeName).Properties("_Codename").value = <CodeName>
I came up with (and tested) the copying solution for a slightly different case, where the workbook into which I'm copying the sheet has its VBAProject password-protected, which means that the above code throws:
50289 : Can't perform operation since the project is protected.