collect list/folder of linked workbooks in Excel 2010 - excel

I have a workbook which links to a number of other workbooks, which in turn are linked to other workbooks. Is there a simple way to collect all the workbooks referenced by a currently open workbook? I can imagine coding a macro to look at all the cells that hold formulas in a workbook, checking for anything in brackets in the formulas and copying these to a list, but this seems unwieldy and hopefully unnecessary. Does Excel have any tools that help track the workbooks to which a file is linked? I know "trace precedents" is useful within a workbook, and the key combination "ctrl+[" is useful to track linked data, but I can't do this manually for all the cells in the workbook.
Thanks for your help.

This code will give you the same list that you'd get by clicking Edit>Links.
Sub PrintLinkedBooks()
Dim LinkedBooks As Variant
Dim i As Long
LinkedBooks = ThisWorkbook.LinkSources()
For i = LBound(LinkedBooks) To UBound(LinkedBooks)
Debug.Print LinkedBooks(i)
Next i
End Sub
There are also tools out there to find links. I've never used and but Bill Manville's is one that's recommended.

Related

Is there a way to search and replace in macro text in Excel?

Hope you can help me.
I have consolidated a number of workbooks into one. In the sheets, there are macros to ease navigation (jump to macros). However, they are all linking to the old workbooks, so whenever I press a macro in the new workbook, Excel tries to open the old workbook.
Essentially, the macros look like this:
'https://xxx.sharepoint.com/teams/Folder/Shared Documents/General/File2.xlsm'!Jump_X_to_Y'
Instead of:
Jump_X_to_Y
Now, I can manually replace the macros, but it will take me ages.
Is there a way to search and replace, so that I could simply remove all the external Sharepoint links in one-go?
Thanks in advance!
I'm not sure if you mean Hyperlinks. A Hyperlink is a feature where you can click on text and it opens a web-page or another Workbook, or moves focus to another part of the worksheet.
To change a Hyperlink using VBA, you can search the worksheet cells and get the Hyperlinks collection for the whole worksheet. Then you can check and change the Address for each hyperlink as needed.
Sub Example()
Dim HL As Variant
For Each HL In ActiveSheet.Hyperlinks
If InStr(1, HL.Address, "sharepoint", vbTextCompare) <> 0 Then
HL.Address = Split(HL.Address, "!")(1)
End If
Next
End Sub

How do I prevent the Excel "We can't update links" dialogue when formulas pointing to non-existent internal sheets are purposeful?

I am making an Excel 2016 database. Part of the design are large Consolidated Data worksheets that reference cells in monthly data sheets. For example, the Consolidated Data sheet may reference:
='Datasheet Feb2017'!D3
Although many cell references are more complex:
=IF(ISREF('Datasheet Jun2017'!A1),IF(XLOOKUP($A4,'Datasheet Jun2017'!D4:N4,'Datasheet Jun2017'!D6:N6,NA()) = "", NA(), XLOOKUP($A4,'Datasheet Jun2017'!D4:N4,'Datasheet Jun2017'!D6:N6,NA())),NA())
These references work as intended. When the datasheet exists, it displays the appropriate data. When the datasheet doesn't exist, it displays #N/A. I do this so that as I add additional months of data, the consolidated data worksheet automatically updates. A limited number of these references to not-yet-created workbooks are constructed through INDIRECT.
The only problem is that upon opening the workbook I will always get this message:
This workbook contains links to one or more external sources that could be unsafe.
If you trust these links, update them to get the latest data. Otherwise, you can keep working with the data you have.
This is strange because there are no external links. All references are internal. When I select Update:
We can't updates some of the links in your workbook right now. You can continue without updating their values, or edit the links you think are wrong.
I suspect this is referring to the references to not-yet-created worksheets.
This is a macro-enabled workbook.
How can I prevent both of these dialogues on startup?
I found a solution to an adjacent question that seems to work, from user zerala:
I put this around my startup macro:
ThisWorkbook.UpdateLinks = xlUpdateLinksNever
Application.DisplayAlerts = False
'your macro
ThisWorkbook.UpdateLinks = xlUpdateLinksAlways
Application.DisplayAlerts = True

EXCEL 2007 - Need Help creating a button which takes the contents of active worksheet and pastes it in a new worksheet

I have search throughout this site to find a answer to my problem and most of the related solutions are for a far more complicated problem. Here is what I need to have done. I created a simple form in Excel 2007. I am looking for the ability to add a button at the bottom of the form which allows the user to click on the button and copy that worksheet into a new worksheet within the same excel document. Basically just duplicating the active worksheet.
I tried to do it with macros but did not get the desired results, and most of our co-workers still use Excel 2003 so I am not sure if macros will work in the older version of excel. I do not know any VBA which is why I come here in search of help from you all.
So to recap.
One sheet Excel document with a simple form and a command button at the bottom of the active worksheet
The command button "Copy and Paste" that worksheet into a new worksheet within the same excel document
A solution that could work in both Excel 2003 and 2007 if possible. If not, for 2007.
Thanks so much ahead of time for anyone who is willing to help out a Novice Excel User.
Assuming that you know how to add a button here is a simple line of code to duplicate the active worksheet:
Sub Button1_Click()
ActiveSheet.Copy after:=ActiveSheet
End Sub
Maybe something like this (tested in Excel 2003 only):
Dim srcSheet, dstSheet
Set srcSheet = ActiveSheet
Sheets.Add
Set dstSheet = ActiveSheet
srcSheet.Activate
srcSheet.Cells.Select
Selection.Copy
dstSheet.Activate
dstSheet.Cells.Select
ActiveSheet.Paste
You should find this method will work in both Excel 2003 and Excel 2007. In your form, add the following method:
Sub CopySheet(WorkSheetName as String)
Dim WrkSht As Worksheet
Set WrkSht = Sheets(WorkSheetName)
WrkSht.Copy After:=Sheets(WorkSheetName)
Set WrkSht = Nothing
End Sub
From the button click event, call it using:
Sub Button1_Click()
Call CopySheet("WorkSheetToCopyName")
'You could also replace the string name with ActiveSheet if you so wish
End Sub
This will dump a copy of the worksheet in between the current sheet and the next one. I've tested it in Excel 2003 and Excel 2007 and it works in both. It doesn't give the second one a pretty name sadly - it just gets the same name as the source worksheet with (2) put after it.
All the formatting, protection and formulas are copied across too - it's a carbon copy of the first.
I know the question is quite old, but just wanted to note that you (and the user) can do the exact same thing with zero code: right-click on the sheet name at the bottom and select Move or Copy..., then check the Create a copy box and click Ok. Yes, it takes 4 clicks, but it is super easy and avoids code.

Language-independant way to refer to "Sheet"?

A friend of mine works as an analyst for an insurance company, and uses Visual Basic for Applications (VBA) to write scripts.
Since her company has offices in different European countries, she needs to make the scripts as language-independent as possible.
One issue that came out recently is how to handle Excel documents and refer to sheets (eg. Sheet1, Sheet2) without having to translate this term in the major European languages.
Does someone know if Excel provides a list of constants that she could use to access different pieces of information regardless of the localized version of Windows/Excel?
Thank you.
You should never use hard-coded names of worksheets to refer to them in VBA.
Dim s As Worksheet
For Each s In ActiveWorkbook.Sheets
DoSomethingWith s
Next s
or
Set s = ActiveSheet
DoSomethingWith s
or
Set s = ActiveWorkbook.Sheets.Add()
DoSomethingWith s
or
Dim i As Long
With ActiveWorkbook.Sheets
For i = 1 To .Count
DoSomethingWith .Item(i)
Next i
End With
In cell references however, using actual sheet names is inevitable. These references could be built on demand by VBA. Existing references (in cell functions) are kept up to date by Excel automatically.
In the VBA editor, you can rename worksheet objects; this totally separate from the worksheet name displayed on the tab at the bottom of the screen.
I usually rename them something like wksData and wksSummary, and use code like
wksSummary.Range("A1").Value = "abc"
Users can rename the worksheets as they wish, but the VBA reference remains the same.
The name of the worksheet can be retrieved, as in the example below:
MsgBox wksSummary.Name

Excel VBA - Use Module in Workbook B to Update Data in Workbook A

I have 10 XLS's, each of which contain a a few hundred lines of VBA that do the same thing. I want to move this common code into an 11th XLS, and have the other 10 call the code in the 11th XLS. The common code must have access to all of the data and worksheets in the calling XLS. This last requirement does not seem to be addressed by other answers to this question on SO. Can I pass the calling XLS's worksheets in as a parameter, or something similar?
Instead of putting this into a secondary XLS file, I'd recommend creating an XLA file (an Excel Add In).
This is the exact scenario for which XLA was intended. XLA will work the way you intend in this case.
For details on creating an XLA, see this page.
Yes, you can pass references to workbooks, worksheets, ranges, etc. as parameters to any function:
Public Sub CallMe(ByVal oWorkbook as Workbook)
Dim oWorksheet as Worksheet
Set oWorksheet = oWorkbook.Worksheets(1)
' Do stuff...
End Sub
Note that you'll probably have to re-write a lot of the code you copy from the 10 workbooks since they'll be full of implicit references to "this" workbook, such as Worksheets(1) etc. As in the example above, you now need to say oWorkbook.Workbooks(1) instead.

Resources