Setting chart title only works while debugging in Excel 2016 - excel

The problem can be summarized like this:
This problem only occurs if I run the code, not when I step through the chrt.HasTitle-line. In that case, it correctly creates a title and I can edit it however I want.
I'll first give the relevant source code. This simply includes the code that distinguishes Excel 2003 from newer versions (since Excel 2003 crashes on the 'newer' code and vice versa).
Sub EnableChartTitle(chrt As Chart)
If Application.Version = "11.0" Then
EnableChartTitle_2003 chrt
Else
EnableChartTitle_Post2003 chrt
End If
End Sub
Sub EnableChartTitle_2003(chrt As Chart)
chrt.HasTitle = True
End Sub
Sub EnableChartTitle_Post2003(chrt As Chart)
chrt.SetElement msoElementChartTitleAboveChart
End Sub
The use of chrt.SetElement previously bypassed this exact issue, but now it seems to be back with Office 2016. Using the old chrt.HasTitle results in the same issue with 2016.
This code worked perfectly for all versions of Office until Office 2016. Now it suddenly refuses to enable the title unless I step through the code which is of course not the intended use.
Anyone knows what is up with this and a possible fix? VBA really gets on my nerves with this kind of stuff and it's very hard to Google too. This is where I got the previous solution from.
Here is the code that creates the chart:
Dim chrt As Chart
RI.rSheet.Activate
Set chrt = Charts.Add.Location(xlLocationAsObject, RI.rSheet.Name)
Then its location is set, any series that were auto-added are removed and new data is added using chrt.SeriesCollection.Add <range>.
Notice: The exact same code does work in another script. This is because here, there is only one series added. When setting the name of the series, Excel automatically enables the title. In this script, multiple series are added and as soon as the second set of data is added, the title is automatically removed again. Then it won't enable it again anymore.

I found an answer. You have to tell VBA twice what he has to do before he listens. Changing the code simply to
Sub EnableChartTitle_Post2003(chrt As Chart)
chrt.SetElement msoElementChartTitleAboveChart
chrt.SetElement msoElementChartTitleAboveChart
End Sub
fixed the problem.
This answer was not at all satisfactory...

Related

Copying Editable Excel chart to Word Error 4605

I have a macro that is intended to copy a chart from excel into a word document in the same way as manually copy and pasting using "Keep Source Formatting and Embed Workbook." Below is the code that, to my understanding, should accomplish this.
Set reductionChart = graphWorksheet.ChartObjects("reduction")
reductionChart.Copy
masterReport.Paragraphs.Last.Range.PasteAndFormat wdChart
graphWorksheet is the worksheet that contains the graphs, masterReport is the word document.
The issue I am having is error 4605 command not available on the PasteAndFormat line. I happened to discover that manually copying the graph then running the line worked without issue. Thinking that maybe right clicking copied in a different way than .copy so I record a macro of the action and ended up with:
ActiveSheet.ChartObjects("Reduction").Activate
ActiveChart.ChartArea.Copy
Even substituting this in the error still occurs. What is happening here?
After some additional testing I am thinking that possibly when using .copy the chart is sort of only stored within excel and not the clipboard so when paste and format looks for something it see an empty clipboard and has an error, but right clicking copy stores it to the clipboard hence why is available still even after I run ActiveChart.ChartArea.Copy again.
I've attempted to create new workbook with a single sheet and chart. Also tried using late binding instead on the off chance that did something. This is the full code still giving the same issue
Sub test()
Dim masterWord As Object
Dim masterReport As Object
Set masterWord = CreateObject("Word.Application")
Set masterReport = masterWord.Documents.Add
masterWord.Visible = True
ThisWorkbook.Worksheets(1).ChartObjects("Chart 1").Copy
masterReport.Paragraphs.last.Range.PasteAndFormat wdChart
End Sub
After much testing this is the best workaround I'm managed to figure out.
reductionChart.Chart.ChartArea.Copy
masterReport.Paragraphs.Last.Range.PasteAndFormat wdFormatOriginalFormatting
masterReport.InlineShapes(masterReport.InlineShapes.Count).LinkFormat.BreakLink
wdFormatOriginalFormatting and wdPasteDefault both work and don't seem to make a difference to the outcome as far as I have seen.
This has one issue that I've found which is that on ActiveDocument.Fields.Update an information box will popup warning that the linked file is unavailable. This will occur for each unlinked item. I attempted to use Application.DisplayAlerts = False but this did not prevent the popup. This may come back to bite me, but I simply removed this line as it was unnecessary for my purposes.

Excel Add-in, when to check for updates?

I've made a useful Excel VBA add-in that I'm almost ready to publish. I only have one problem. I want the add-in to check for updates whenever it is ran, and to notify the user if they're using an old version.
I have written a public sub that will check my website for the update using internet explorer automation. It is working fine when I run it manually.
My issue is, when should I call the CheckForUpdate() sub? I've tried putting it in Workbook_Open(), but the issue I've found is that if it takes too long, it blocks Excel from loading properly.
Is there a standard way that add-in writers check for updates? Why does Excel not load up a workbook when I have code in Workbook_Open()?
Thanks!
Edit: Here is my entire code. If this is added to an add-in workbook, and then the add-in is activated, I can't open any Excel files.
Private Sub Workbook_Open()
Call CheckForUpdate
End Sub
Public Sub CheckForUpdate()
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
ie.Visible = False
ie.navigate "https://example.com/checkforupdate"
Do While ie.readystate <> 4: DoEvents: Loop
Set HTML = ie.document
If HTML.DocumentElement.innerHTML = "update is available" Then
MsgBox "An update is ready. Go get it!"
Else
MsgBox "no update"
End If
Set ie = Nothing
End Sub
Update: I've tested on three computers now. 2 of them have Excel 2013 and the problem happens. One has Excel 2016 and it doesn't have the problem. I want to support all Excel versions, so I still need a work around.
Ok, I ran your code and reproduced the issue, with Excel 2013. I didn't test on any other version.
It looks like what happens is if your code runs before a workbook is visible, it causes problems. I think it has more to do with the IE part, than anything else.
I tried using Application.OnTime Now + TimeValue("00:00:15"), "CheckForUpdate". The problem with that is that if the user starts excel and out waits the timer before opening a workbook, you have exactly the same problem. I suspect the persons suggesting asynchronous VBScript will have exactly the same issue, but in a much more complicated way.
So let's go simple. You asked for a better place to put your code than Workbook_Open(). There is one.
Here's what worked for me:
Start a new add-in project. Add your CheckForUpdates function to a new module. Add the following code to ThisWorkbook.
Private WithEvents App As Application
Private Sub App_WorkbookActivate(ByVal Wb As Workbook)
CheckForUpdate
End Sub
Private Sub Workbook_Open()
Set App = Application
End Sub
With this code, your update procedure will run when a workbook is activated. It appears to not cause problems there.
Please let me know if that works. Good luck.

Excel 2016 VBA - Problems upgrading from Excel 2013

I developed several userforms and macros for a project in Excel 2013, but when I try to test them in Excel 2016, I get multiple errors on basic VBA functions.
For example I get an "Object doesn't support this property or method" error from the following code:
Private Sub ShowImpact()
FormImpact.Show
End Sub
Another example, the following code gives me an error "Can't find project or library" on the [RIMS_tbl] table reference. This is a named table in the document:
With FormImpact.cboIndustry
.ColumnCount = 11
.ColumnWidths = "0;50;0;0;0;0;0;0;0;0;0"
.RowSource = "=RIMS!" & [RIMS_tbl].Address
.BoundColumn = 1
.TextColumn = 2
End With
As another example, I get the same error message on "Format" method in the following code:
Private Sub txtConLand_AfterUpdate()
txtConLand = Format(txtConLand.Value, "#,##0")
End Sub
Lastly, I have a label that I'm using as a background on the form, which works fine in Excel 2013, but for some reason it covers all of the other controls in 2016, and I can't send it to the back.
I'm really hoping that there is some setting in 2016 that will fix all of these problems, because I can't figure out why so many things would break between these two versions. Thanks for your help!
As mentioned in the comments above, the solution to this problem was in eliminating missing references between different version numbers. In the Visual Basic interface, go to Tools->References. Check for missing references in all versions supported. Thanks #TimWilliams and #Stefan for the solution to this problem.
I never did figure out how to fix the problem of the z-order for the background label. Even setting the v-order for that label programmatically didn't work. But I did come up with a workaround. First I made a small white bitmap file. Then under the page settings, I set the background picture to be the bitmap file. Then I set set the "PictureSizeMode" property "fmPictureSizeModeStretch," which stretched the bitmap to fit the entire page. Finally, I added a border around the entire form.
I had the same problem when migrating an VBA powered Excel from Excel 2010 to 2016. Deselecting the ATLEntityPicker 1.0 Type Library from the References list solved it.

Unexpected results from a Worksheet.Activate event

I’m running excel 2010 on windows 7.
The following macro does what you would expect, ie (1) inserts a new worksheet, (2) adds a rectangle, and (3) activates cell A1.
Sub addSheetAndButton()
Dim buttonSheet As Worksheet
Set buttonSheet = ThisWorkbook.Sheets.Add
buttonSheet.Shapes.AddShape(msoShapeRectangle, 100, 100, 100, 50).Select
buttonSheet.Range("A1").Activate
End Sub
My problem is when I try to run it with a Worksheet.Activate event, for example:
Private Sub Worksheet_Activate()
Call addSheetAndButton
End Sub
This time (1) a new worksheet is not inserted, (2) the rectangle is added to the worksheet associated with the activate event, (3) cell A1 is not activated and (4) the rectangle remains activated.
What am I doing wrong? Any help would be greatly appreciated.
Thanks very much for trying this. I’m sorry it has taken me a while to get back, but I’ve been experimenting with this. I have discovered that when I exit and restart Excel, the addSheetAndButton macro works as expected under the Worksheet.Activate event. However, I have a different macro that uses another Worksheet.Activate event to update some charts. On what seemed to be a random basis, this would create a “run time error 6” - an overflow that resulted from trying to assign an out of bounds value to a variable.
I never got this error if I bypassed the Worksheet.Activate event/macro and ran the chart update macro by hand. After resetting VBA/Excel from the “run time error 6”, the addSheetAndButton macro behaved exactly as I described in my original post. The only way to cure it was to exit and restart Excel.
I think (hope) that I have traced the source of the run time error back to a line in my chart update macro where I had selected rather than activated a worksheet before reading data from it – though I am surprised at the result. It is worth pointing out that once Excel entered the “not as expected” state, the on-screen message from the following two lines was the name of the previous active sheet and not “Sheet1”.
Worksheets(“Sheet1”).Activate
Msgbox ActiveSheet.Name
Again, the only apparent way to cure this was to exit and restart Excel.
Thanks again for your help.

"Object doesn't support this action" for a drop down menu in Excel 2011 (Mac OS X)

I have a big Excel Workbook made with Office 2010 with some VBA code. Everything seems to work fine apart the drop down menus. Precisely, they work, graphically, but
Me.Shapes("Drop Down 1").ControlFormat
throws an "Object doesn't support this action" error (I am sure that "Drop Down 1" is the correct name, etc.), precisely, it gets referenced correctly (e.g. shape = Me.Shapes(1) works) but it doesn't seem to like ControlFormat. Google doesn't help much; any suggestions?
I'm quite new to VBA so there might be some trivial debugging witchcraft I'm not aware of.
EDIT: I tried creating a new workbook with a dummy dropdown menu and selecting the values whilst recording a macro but it gives no result (it's like the menu never existed).
I know this can sound frustrating and Stupid at the same time but for Excel 2011, change the line from
Me.Shapes("Drop Down 1").ControlFormat
to
Worksheets("Sheet1").Shapes("Drop Down 1").ControlFormat
For example
This will work in Excel 2010 but not in Excel 2011
Sub Sample()
With Me.Shapes("Drop Down 1").ControlFormat
.AddItem "Sid"
End With
End Sub
It will give you the error that you mentioned.
SCREENSHOT
For Excel 2011, you will have to use (Fully qualify the object)
Sub Sample()
With Worksheets("Sheet1").Shapes("Drop Down 1").ControlFormat
.AddItem "Sid"
End With
End Sub
SCREENSHOT
Note: Replace Sheet1 above with the relevant sheet name.

Resources