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

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.

Related

Copy sheet using copyToEnd()

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.

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.

Coping a worksheet without it being activated

I'd like to copy a worksheet at the end of my workbook without it becoming active.
I use this code to copy my "Template" sheet at the end:
ThisWorkbook.Sheets("Template").Copy after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
But then my new sheet "Template(2)" become the active sheet.
I'd like to remain on "Template" even after the copy.
Is it possible ?
Is it possible?
It's not. .Copy activates the new sheet, that's just how it is. However nothing stops you from (re-)activating the original sheet after:
With ThisWorkbook.Worksheets("Template")
.Copy after:=ThisWorkbook.Workheets(ThisWorkbook.Worksheets.Count)
.Activate
End With
Notice I'm using the Worksheets collection here. The Sheets collection can contain non-worksheet objects, such as a Chart sheet, which is typically not what you're looking for - the Worksheest collection only contains actual Worksheet object.
Note that finding your template sheet in the ThisWorkbook.Worksheets collection everytime you need to use it is not a necessity, and makes your code more frail than it needs to be.
Each Excel object (VBA object actually) has a (Name) property (on top of the Name property which refers to whatever the worksheet is labelled as) that you can edit in the Properties pane (press F4 in the editor) - that name must be a legal VBA identifier, and what VBA does with it is pretty nifty: it declares a global-scope object variable with that name, so if you name your "Template" sheet TemplateSheet, then you can do this:
With TemplateSheet
.Copy after:=ThisWorkbook.Workheets(ThisWorkbook.Worksheets.Count)
.Activate
End With
And so on for every "static" worksheet (i.e. sheets that aren't generated by code). By referring to worksheets by their Name property (the tab label), your code will start failing as soon as a user decides to name the tab something else. By referring to the corresponding global-scope identifier, you can label the worksheet tab whatever you like, the code won't care at all.

Non-existent Excel Worksheet, but Formulas and Defined names still work?

I have an Excel file that is referencing a non-existent worksheet. But the formulas and stuff still work just like nothing is broken (no "#REF" appear). How is this possible?
E.g. Worksheet A! has a VLOOKUP formula referencing a Defined Name in Worksheet B! which is a table range. Except Worksheet B! is nowhere to be found. Yet, the formula still works even as other variables in the formula are updated.
Some additional info:
1) All workbook / worksheet / macros are unprotected
2) There are no hidden worksheets
3) When you open the VBA editor, under MS Excel Objects, the other "ghost" worksheets appear. If you right click on them, you can "View Code". But "View object" option is blanked out. But there are no VBA code for these "ghost" worksheets
4) When you open name manager, the defined names still show Worksheet B! as if it was still there and nothing was wrong.
Anyone knows how this happened?

Vb.net Updating excel formula with references to other workbooks

I am trying to update some formulas from one workbook, to another workbook. Everything is working great until I run into a formula that has a reference to another workbook. For example a formula like this =IF(ISERROR(W!Var1),0,W!Var2) It will prompt me to open this workbook, I am assuming so that it can evaluate the formula. So my question is this. Is there a way for me to handle these situations on the fly, so if there is a workbook reference needed it will prompt me and then save it to memory? Because if I have more than one cell that contains these formulas it will prompt me to open the referenced workbook for every cell that contains the link. Alternatively, is there a way that I can just push my formula into the cell without having excel evaluate it?
So in my code I have this line which works for any value that doesn't contain a workbook reference. TheRange.RefersToRange.FormulaR1C1 = RangeFormula
Any help is greatly appreciated.
I understand that you refer to Worksheets (each of the "tabs" in a given Excel file), the Workbook is the whole file. The popping-up message appears when the referred Worksheet cannot be found. Example: range.Value = "=sheet5!A3" (in a Workbook having just "sheet1", "sheet2" and "sheet3"). If you want to avoid this message (although bear in mind that the Worksheet is not there and thus the calculations will be wrong anyway), you can write:
excelApp.DisplayAlerts = False
Where excelApp is the Excel.Application you are currently using.