Update hyperlinks in shapes when file location changes - vba

I have a process flow diagram that uses various excel shapes to visually represent a data production process from start to finish, I.e. from data input to analytic environment to data output to submission file. I have used vba to hyperlink many of the shapes in the diagram to another sheet in the workbook (using thisworkbook.fullname) that contains definitions for abbreviations contained in the text of each shape, eg C1 is listed in a shape, the hyperlink takes you to the definition tab cell where C1 is defined as control point one. The hyperlinks work when the xlsm workbook is in my home location where i saved the file but they do not work if I save the file to another location (they try to open my original workbook). Is it as easy as changing the hyperlink addresses to thisworkbook.filename and dropping the path to make this work? do i need to create a macro that will automatically look up the old hyperlink address and replace it with the new address of current file location for every shape in the workbook containing a hyperlink. The path could change in the future, so want it to be relative and not fixed - for example if I save the file to share point and another user saves a copy to their home directory, I still want the links to work for them in either location. Some hyperlinks go to "sheet1" some to "sheet2" for example, but sheets 1 & 2 are both located in the same workbook. Please help!

ActiveWorkbook.Name solves the problem, replace ThisWorkbook.FullName with it and hyperlink works in other directories.

Related

Preserve settings in the file

I am creating my excel add-in that saves the current file as csv into user-specified folder. I would like my program to ask for the folder path the first time the program is launched and to remember that folder in the future. I am wondering is there any way to preserve the data within the program? I figured that I could write the path into .txt-file but that feels a little hack-like solution and would clutter the addin folder.
I use the GetSetting and SaveSetting functions in my VB 6 apps. Rather than cover them in detail, take a look at this excellent web page that illustrates how to use in with Excel
Excel Tips From John Walkenbach
Create a Worksheet, and store the values in cells. Then in the VBA Editor find the Worksheet in the Project Explorer (Ctrl + R) and set "Visible" to "2 - xlSheetVeryHidden" in the Properties Pane (F4) so that it is not readily visible to users.
You can then set/retrieve the data in with code in the format SheetName.Cells(row,column).Value, e.g.
MyPath = Sheet1.Cells(1,2).Value 'Get data from cell B1
Sheet1.Cells(2,2).Value = NewPath 'Set data in cell B2
There are multiple ways to approach this. Besides the hidden sheet approach, already described, you can
Write the information to a CustomXMLPart. This is a xml file stored in the workbook's ZIP file where information can be stored.
For something as short and simple as a file path, you could also use a CustomDocumentProperty

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

XML sheet names not seeming to agree with vba worksheet names

I have a large bloated workbook.
I've made the file a zip and opened the zip to inspect the xml files. Then I've looked at the worksheets folder in the zip to see this:
If I then look in the vba editor I can see this:
But "Sheet25" in the vba is a tab for notes with only 5 lines of text on it so these names are not the same.
Is there an easy way for me to map between the two different identifiers? e.g. what Sheet is sheet25.xml?
The simplest check is to open sheet25.xml. You will see the matching codeName in this line at the top:
<sheetPr codeName="Sheet22"/>
EDIT:
If you don't want to open the xml file (because it's too big), there is another way you can trace the mapping of the xml sheet filenames to the codeNames of the sheets in the workbook.
Open up the xl/_rels/workbook.xml.rels document. You should see lines like this:
<Relationship Target="worksheets/sheet3.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Id="rId3"/>
<Relationship Target="worksheets/sheet2.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Id="rId2"/>
<Relationship Target="worksheets/sheet1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Id="rId1"/>
Link the xml sheet name to its resource ID on the far right, e.g.,
worksheets/sheet2.xml => Id="rId2"
Then open up the xl/workbook.xml document, where you get:
<sheet r:id="rId1" sheetId="1" name="one"/>
<sheet r:id="rId2" sheetId="3" name="Sheet3"/>
<sheet r:id="rId3" sheetId="4" name="Sheet1"/>
Link the resource ID to the sheet name, e.g.,
r:id="rId2" => name="Sheet3"
And then from the VBE editor:
Sheet3 (Sheet3)
Match the name to the tab name inside the parentheses to get the codeName:
Sheet3 <= (Sheet3)
Done.
I think that the sheet number of the xml file is just the order of the sheet in the workbook counting from the left at the point at which you saved the file. To find it you can open your spreadsheet and unhide all sheets. Alternatively you can use the formula =SHEET() in a worksheet to identify its number.
Note that this is different to the sheetcode in VBA which is a permanent sheet number that you see if you open the project explorer in VBA. The sheetcode will not change if you move it around.

Importing data into a master worksheet

I have a doubt about my program to the VBA/EXCEL
I made a file called "MASTER WORKSHEET" with various data from a client, my goal is: To create a VBA to open any file in Excel and that I import chosen cells to fill out all data of the "MASTER WORKSHEET", for example, all excel file I open with the button "IMPORT DATA", it will automatically capture only those cells that I choose, but my program it captures but is with configuration problems on VALUE or NUMBER.
If your question is simply "how do I copy a cell from one place to another", an example would be:
Workbooks("Input Workbook").Worksheets("Input Worksheet").Range("J1").Copy(Workbooks("Master Workbook").Worksheets("Master Worksheet").Range("A4"))
If you just want to copy the value, without the formula and formatting, you could use
Workbooks("Master Workbook").Worksheets("Master Worksheet").Range("A4").Value = Workbooks("Input Workbook").Worksheets("Input Worksheet").Range("J1").Value
Obviously, the workbook, sheet, and range names need to be changed to your particular situation.

Change Path to Picture Links in Excel

I have manually pasted a large number of linked pictures into a 2010 Excel spreadsheet using insert picture -> select picture location --> link to file. These pictures are part of a report. I update the pictures using R each quarter, and my report automatically updates. Perfect.
I now need to change the directory where the plots are kept, and need to update the links. As there are around 200 of them (its a big report), I want to do this in VBA. Whilst I can loop through the pictures ok (ActiveSheet.Pictures), I can't seem to find the links/address. Any idea how I can see the underlying file location so I might change it - the reference has to be stored somewhere (note - these don't seem to be stored as hyperlinks).
Any idea how I can see the underlying file location so I might change it - the reference has to be stored somewhere
Create a new folder
Paste a copy of the .xlsx or .xlsm excel file
Uncompress the file with a zip tool (i'm using 7-Zip)
Delete the .xlsx or .xlsm file (optional)
Now we have all the component parts of the original file as plain text xml files and folders
Inside the folder xl\drawings\ _rels there are files named as drawing2.xml.rels, drawing3.xml.rels, ...
It seems that each file corresponds to a sheet and stores the paths to images in this format:
Target="file:///C:\Users\myusername\Documents\MyImageFolder\My%20Image%20Name.png"
Change the paths with a text editor
Compress all the contents of the folder to a .zip
Change the extension to the original .xlsx or .xlsm
These steps could be automated with VBA, AutoIt, etc., here some references:
An example with AutoIt and 7-zip
http://www.jkp-ads.com/Articles/Excel2007FileFormat.asp
http://www.jkp-ads.com/Articles/Excel2007FileFormat02.asp
Ron de Bruin zip examples with VBA
Read and change multiple XML files in Excel (2007) VBA
Excel uses the Formula Bar as the link in this case, just the same as it would link between ranges in two different worksheets. When I select a linked picture, the formula below populates in the formula bar:
=[TrialWB.xlsm]Sheet1!$C$3:$E$6
You can access the Shape's formula using the code below and inserting your picture's specific name:
ActiveSheet.Pictures("Picture Name").Formula = "=[TrialWB.xlsm]Sheet1!$C$4:$E$6"
In updating the links, you'll have to change the file path in the formula. This might look like:
ActiveSheet.Pictures("Picture Name").Formula = "='C:\Reports2015\[TrialWB.xlsm]Sheet1'!$C$4:$E$6"
changing to
ActiveSheet.Pictures("Picture Name").Formula = "='C:\Reports2016\[TrialWB.xlsm]Sheet1'!$C$4:$E$6"
This question may be of some further assistance for accessing formulas:
Excel: create image from cell range
And here's a useful Microsoft page for formula file path editing: https://support.office.com/en-us/article/Create-an-external-reference-link-to-a-cell-range-in-another-workbook-c98d1803-dd75-4668-ac6a-d7cca2a9b95f