Excel VSTO Worksheet unique identifier - vba

I am looking for a way to create unique Worsksheet identifier, which would take into account at least name and content. If any worksheet value is changed then the identifier would change as well. I have checked the GetHashCode() but for some reason each time I call this method on the same sheet it always changes. Any ideas how to create that identifier/hash code
Excel.Worksheet sheet2 = (Excel.Worksheet)Globals.ThisAddIn.Application.ActiveSheet;
var hash = sheet2.GetHashCode();

The CodeName of a worksheet is a unique identifier. VSTO uses the CodeName property for the internal name of the worksheet (probably Sheet1, Sheet2, etc.) when it creates a project with Worksheets in the Workbook.
In the VBA interface this property can be changed in the VBA IDE - it's the name used for the class module belonging to each Worksheet.
The property cannot be changed at run-time, only at design time. While the property may seem to reflect the caption for the worksheet (the name the user sees and can change in the UI), the two are not linked.
For more information, see:
https://msdn.microsoft.com/en-us/library/office/ff837552.aspx?f=255&MSPPError=-2147217396
http://www.cpearson.com/excel/RenameProblems.aspx

Related

using data validation for copying a named chart form another worksheet

I have been working on a dashboard where I can use drop-down lists to select and copy/paste named charts with the same name from other sheets in the entire workbook.
I'm trying a few things but none works:
Activeworkbook.ChartObjects Range("F2").activate
F2 being the cell with data validation.
I think your select isn't working because you need to use the value in your cell as a string referring to the chartobject in the collection. Change the sheet reference below as needed. I have my cell containing the name of the chartobject and the chartobject itself on the same sheet but it sounds like that's different than your setup.
ThisWorkbook.Sheets("Sheet1").ChartObjects(ThisWorkbook.Sheets("Sheet1").Range("F2")).Activate
Which you could then shorten if you have workbook and worksheet variables referring to ThisWorkbook, your worksheet with your chartobject, and your worksheet with your data-validated name cell.
Thank you for that! It solves the first part of the question, namely using the value in the cell as reference.
The second part is more tricky: I have several sheets with named charts in the workbook and I need the command to copy a particular named chart in the entire workbook.
One option is just to put all my charts into a single worksheet. but ideally I would like to keep them in their respective sheets.
Any way this could work?

VBA - Pushing data from one workbook to another, irrespective of file names

Been in a pickle for a while (week or so) here and was hoping someone in this magical community could help me out. There is likely a very easy solution for an experienced individual, which I am not.
First, my goal is as follows: Push data from Workbook A to Workbook B via macro.
Conditions:
Workbook A must be able to be renamed without compromising the macro (it is a tool used in day-to-day functions and saved as a new name each use). Workbook A holds the macros.
Workbook B receives the data. Its name will also change with time, but in this case it needs to be based off written text in a cell from Workbook A (name change about yearly due to versioning) Let's just call it Cell A1 for argument's sake.
There may be additional workbooks open at the same time, related or unrelated.
To keep it simple, I will just post one line in my current macro, but I will be applying this to dozens. This works when I do not rename the files. I likely need help defining variables (strings?) and direction in what functions to use.
Windows("Workbook_B 4.7.5.xls").Activate
'*Workbook B name will ideally be derived from a cell value in Workbook A*
Range("V12") = "='[Workbook_A V1.2.5 .xlsm]SHEET_A '!R8C7"
In this chunk, the goal is to activate Workbook B and copy the value (or formula if it is easier), from cell G8 on Workbook A Sheet A to Workbook B(sheet is already active and sheet names will never change in either workbook).
Cheers!
"Workbook A holds the macros" - in this case you would use
ThisWorkbook
to refer to Workbook A
...and
Workbooks(ThisWorkbook.Sheets("Sheet1").Range("A1").Value)
to refer to Workbook B

Renaming active workbook in VBA

Is there a way to rename an active workbook in VBA? The workbook name (MTD_Truck_October2017.xls) will change every month I run a big macro on, and it references the name at one point. I'd like to rename it at the beginning to say MTD_Truck_Current.xls, then I can reference that name which will then be consistent for every month.
Also, the macro will need to run on several users computers. I originally just saved to my desktop as a certain name, but now that others will be using this, the path to their desktop is different, so I either rename the workbook at the beginning, or I have to change the path in the macro for every user.
Any ideas? Thanks!
The workbook that holds a macro can be referred to as ThisWorkbook. It has a path and Name property. Rather than hold the name of the workbook as anything, use
ThisWorkbook.Name

Excel Reference External Sheet

An excel question for you gurus. I've tried searching high and low and haven't come up with an effective solution.
I'm trying to create a formula that will lookup a value in an external sheet. I'm using the SUMPRODUCT formula and it works perfectly. Formula is below:
=SUMPRODUCT(--('File\Path\[file.xlsx]SheetName!$D$1:$D$1000=$B3), --('File\Path\[file.xlsx]SheetName'!$O$1:$O$1000=$A3), 'File\Path\[file.xlsx]SheetName'!$Q$1:$Q$1000)
The issue I'm running into, however, is that the source file is updated every day. Although the workbook name stays the same, the sheet name changes. A random string gets assigned to the source sheet name each time it is updated. As such SheetName becomes SheetName ase341.
Is there a way to have the formula read the external sheet number instead of the name? I want the formula to update regardless of the sheet name. If there's no way to read the sheet position is there a way to change the sheet name via a formula in an external workbook?
Usage Example
I have a workbook (analysis) and it pulls data from another workbook (source). Source is updated every day with new data. The data in Source is updated by downloading a report from the internet and saving over the old source file. As such, the file name stays the same but whatever is inside the file is always different (including the sheet name). There is always only ever one sheet in the Source with the same number of columns, always in the same position.
There is a really neat way to refer to a block of cells in an external workbook in which the sheetname or even the block address may vary. Say we have:
=SUM('C:\Users\James\Desktop\[Book1.xlsx]Sheet1'!$B$2:$B$9)
however the sheetname may vary. First assign a Defined Name to the block in Book1 (say XXX)
Then we can use:
=SUM('C:\Users\James\Desktop\Book1.xlsx'!XXX)
It does not matter if the sheetname changes, the Defined Name will change with it!
Your issue would be most efficiently solved with VBA, but if you're just getting started this might not be the best route.
You can get the sheetname or filename with just a formula, though:
http://www.ozgrid.com/VBA/return-sheet-name.htm

VBA Names - Where are they referenced?

I have defined a large number of named cells on one worksheet of a workbook. Each name may be used multiple times (or never) on that worksheet or others in the workbook. Is there a way to get a list of the where else in the workbook the names are used?
For example, lets say I define the name Fred for a cell on Sheet1. There is an equation on sheet2, cell B6 that is =Fred*6. How do I get a function to return Sheet2!B6 and any other places that Fred is used?
I hope this is clear enough. :)
The free version of JKP and my Name Manager Addin can show you where a particular name is used. You can download this addin from http://www.decisionmodels.com/downloads.htm
When I wrote the FastExcel extended version of Name Manager I added a Name Map facility that shows you a count of where the names are used (or not) by Worksheet and Other Names. This is done by parsing out the Names from all the formulas on all the worksheets and the formulas in the Names Refers-tos (recursively), using a special-purpose parser and evaluating cases like the use of INDIRECT.