Excel Template has reference invalid links that I can't break - excel

I am in the process of creating a new version of the Template. Excel 2019 Windows 10 64 bit. I have already gone through the exercise of creating a new template and copying the worksheets one at a time. There were a number of references that I found and deleted. Many of them were in the Names dictionary. I am now down to 2 that should not have been present in the first place because they are only to be used in the workbooks that are created from the templates.
I have tried and exhausted all of the samples in questions that I have found. It is distilled into the following code:
`Public Sub FindLinks()
Dim i As Integer
Dim olinks As Object
Dim v() As Variant
Dim strLink As String
v = ActiveWorkbook.LinkSources(xlExcelLinks)
For i = 1 To UBound(v)
strLink = v(i)
Debug.Print i, "Link = "; strLink
' Now break the link
ActiveWorkbook.BreakLink Name:=strLink, Type:=xlLinkTypeExcelLinks
Next i
End Sub`
The above example finds two links and displays them but will not break the links - just does nothing.
Anyone have a notion of what I could try next?

The standard "Break Links" route does not capture links that are referenced through:
Named ranges
Conditional formatting
Data validation
Graph sources
Pivot Table sources
If you have any of these components or functionalities, look through their corresponding interfaces (e.g. the Name Manager for named ranges, etc.) and check for linked content.

SIE_Vict0ria - Thank you. I had actually fixed all the references in the Name Manager, but I discovered the two references that were "invalid" were in combo-boxes. I deleted them from the combo boxes and voila, no more external references at all. So the problem is fixed.
One thing I do not know, however, is why those references were bogus. The spreadsheets being references were just where they were supposed to be and were shared and marked full control to everyone.
I did not find any documentation on why a reference would be marked as "invalid".
I hope this helps others when they are chasing this kind of problem.

Related

Creating a Dynamic Range that works for Excel to Save Name Ranges to a Static Web Page

I have a excel document that has several name ranges that are currently save to static web pages. I have recently tried to conver them to dynamic ranges, and have learned the dynamic named ranges work great within the context of the excel sheet itself, but fail once excel tries to save them as the static web page.
For example, I have a range
YardTabletLists!$EC$1:$EE$101
sometimes the content is exceeds the bounds or I have empty rows on my webpage.
the dynamic alternative is
=OFFSET(YardTabletLists!$EC$1,0,0,COUNTA(YardTabletLists!$EC$1:$EC$10000)+1,3)
If i refernce this name range anywhere within the sheet, it works, but when AutoPublish does it thing, I get the following error.
Error Notice
I was thinking of trying to conver the Dynamic range back to a static range somehow, and then direct the name rage to that Cell.... i.e.
Name range is directed to =E4, and E4 contains YardTabletLists!$EC$1:$EE$101, but I get the feeling that will give me the same issue.
Thanks to those who read this.
Not familiar with static webpage feature.
But possible to define and use “table” instead of “named range”?
With “table” Excel internally handles the correct range whenever the table size is modified.
Seems the static webpage feature supports also “filtered ranges”. Could this be a work-around, e.g. to filter out blank lines (while using max line number as static range)?
Indirect references (your example with E4) are a little tricky in Excel. I only used it once to define a list for a dropdown.
Muss be supported by the feature and requires “special” syntax
Update:
I have found some examples for VBA updating name ranges, and one excellent example that appears to answer the question:
https://excelchamps.com/vba/named-range/#Resizing_a_Named_Range_using_VBA_Dynamic_Named_Range
Sub vba_named_range()
Dim iRow As Long
Dim iColumn As Long
iRow = ActiveSheet.Range("A1").End(xlDown).Row
iColumn = ActiveSheet.Range("A1").End(xlToRight).Column
ActiveSheet.Range("myRange") _
.Resize(iRow, iColumn).Name = "myRange"
End Sub
for some reason the program hangs up at
ActiveSheet.Range("myRange") _
.Resize(iRow, iColumn).Name = "myRange"
does anyone see perhaps a syntax error I am missing?
Thank you :)

Is it possible to add Repeating Section Content Control section with VBA?

I have created Word template with Repeating Section Rontent Control (RSCC) containing other Content Controls. Also I have excel workbook with information which should go to mentioned Word template. The thing I am trying to do is to create macro which would fill Word template with information from selected rows in Excel workbook (each row to new RSCC section).
I have quite good idea how to do that, except one thing- I cannot figure out how to write macro which would add another section to Repeating Section Content Control.
I am adding illustrations of what I am trying to do:
Problem is, that I cannot find code to do the same thing with VBA. I have tried recording process, but recorded macro is empty (?!).
Looking for answer I have found this thread in StackOverflow, it ask similar question to mine, but it was more or less unanswered to my understanding. Comment in this thread forwarded to old thread in Microsoft forum, but I did not find solution to this problem (or at least I did not understand clearly how should I approach it).
Since one thread is almost 5 years old, another 2 years old. My question is it even possible to add another section to RSCC with VBA? Maybe somebody found a way to do this in the past year or so?
The Word object model has a collection and object for a repeating section content control: RepeatingSectionItems and RepeatingSectionItem. The latter has two insert methods, to insert before or after the RepeatingSectionItem.
Here's a sample that shows how to reference a repeating section content control in a document, get the first or last item and insert a new one after it.
Sub AddRepeatingSection()
Dim cc As Word.ContentControl
Dim repCC As Word.RepeatingSectionItem
Set cc = ActiveDocument.SelectContentControlsByTitle("RepCC").Item(1)
Set repCC = cc.RepeatingSectionItems.Item(1)
'Or to get the last one:
'Set repCC = cc.RepeatingSectionItems.Item(cc.RepeatingSectionItems.Count)
repCC.InsertItemAfter
End Sub

Excel doesn't release a shape's creation name

I’m posting this in the hope that it might help someone else, as none of my searches revealed any information regarding this.
I ran into this while building a procedure that would copy an imbedded form shape, and paste it on the same sheet, then run a renaming scheme that would facilitate referencing either of these shapes
This should be simple enough, but I was running into an intermittent problem. Eventually I came to realize that Excel appears to “remember” the shape’s original name at the time of its creation, and keeps it flagged as being in use, releasing it only when the shape is deleted. So if you rename the shape, you can never return it to the original name. E.g., if you rename “Button 1” to “Btn1”, you can never change it back to “Button 1”. Interestingly, you can now reference the shape by either name, “Button 1” or “Btn1”.
In the macro below, assuming “Button 1” is the creation name, s.Name will end up as “Btn1”, even though it looks like it would return to “Button 1”.
Sub RenameShape()
Dim s As Shape
Dim nm As String
Set s = ActiveSheet.Shapes("Button 1")
nm = s.Name
s.Name = "Btn1"
s.Name = nm
End Sub
I have since come across a couple of links that talk about the shape’s “internal” and “external” names – I’m assuming the “internal” name is the inaccessible one that Excel won’t forget, while the “external” name is the exposed name that one normally sees.
I’d be interested to hear from anyone who can shed more insight into this.
As the main purpose of this post was to pass along information, I'm going to make this an answer so it can be closed.

Macro to compare dates from different file

I am an old man trying to compare dates from two different files in Excel.
My code is:
Dim i As Integer
For i = 1 To 7
IF Data_for_Processing.xls (“SOLARLOG”). Cells (i,”A”).Value = Day_Conversion_chart.xls (Sheet1).Cells (i+2, “B”) Then
Cells(7+I, “B”)=”Equal”
Else: Cells(7+i, “B”) = “NotEQ”
End If
Next i
Will anyone help?
First of all, I would recommend following #simoco 's advice - Reading the documentation will provide the tools for solving future problems and give you the basics. As well as that, I would recommend using vba help. And another very useful tool for trying commands could be recording macros and analyzing them later on the editor.
So, first you need the code to be inside a macro. It will look like this (I chose the name TestMacro):
Sub TestMacro()
'Code here.
End sub
You should take into account that when your macro is running, it does so from the sheet you are working in, so any partial references to cells, etc. will refer to the book you are in and the sheet you are in when you run the macro. It is possible to select another sheet or book, and if you do so manually or on the code, references will be applied on that selected book or sheet.
What I call partial references here are those that read simply "ThisCell" instead of "ThisBook.ThisSheet.ThisCell". I would say that using partial references, though, is appropriate in a vast majority of cases.
The way your code is organized, be careful to run it from the workbook where you want the data to be in the end, let's say, in your 'main' workbook, so that the final values will be written there..
Another comment: whenever you want to use another file, this file must be open (as far as I know), while you are using it. In your code, you don't open any file, so the macro will not work if you don't open ALL referenced workbooks manually prior to running the macro.
When you want to reference something inside something, you mostly use ".". Please read the documentation - You will get a better idea of how this works. For this example:
Book.Sheet.Cell.Value is the structure you are using.
A book can be referenced as Workbooks("Name.xls") if it is open.
A sheet or worksheet can be referenced as Sheets("Name") or Worksheets("Name"), and also with numbers, like Sheets(1) or Worksheets(1). The differences can be seen on vba editor help. I chose Sheets(...) for your example.
Be careful with the symbols. I guess this was probably my problem, but I have to mention it just in case: When I copied your code, instead of double quotes (""), I got something different, that Excel would not recognize. If for any reason you are using different symbols, Excel might not understand.
A cell can be referenced in various ways too. The Range object is used extensively, and if you want to use "A1", "C44", etc., it's probably better to go for it. I like using Cells(,) as you did, when possible. As far as I know, this works nice with numbers (Cells(1,2), for example), which may be very convenient too. I kept this on your code, changing "A" and "B" and writing 1 and 2, respectively.
With all these changes incorporated:
Comments:
'NOTICE THAT ALL WORKBOOKS MUST BE OPEN.
'["A"] changed to [1]
'[Sheet1] changed to [1]
'["B"] changed to [2]
'Data_for_Processing.xls(“SOLARLOG”).Cells(i, 1).Value
'becomes Workbooks("Data_for_Processing.xls").Sheets(“SOLARLOG”).Cells(i,1).Value
'Day_Conversion_chart.xls(1).Cells(i + 2, 2).Value
'becomes Workbooks("Day_Conversion_chart.xls").Sheets(1).Cells(i+2,2).Value
'["B"] changed to [2]
And one possible solution:
Sub TestMacro()
Dim i As Integer
For i = 1 To 7
If Workbooks("Data_for_Processing.xls").Sheets("SOLARLOG").Cells(i, 1).Value _
= Workbooks("Day_Conversion_chart.xls").Sheets(1).Cells(i + 2, 2).Value Then
Cells(7 + i, 2) = "Equal"
Else: Cells(7 + i, 2) = "NotEQ"
End If
Next i
End Sub
This worked on my Excel example - I hope it is of some help.
Regards.

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

Resources