Running a VBA script from a button after selecting a chart - vba

I’m running Excel 2010 on Windows 7.
I have four charts on a worksheet. What I want to do is select one of the charts and then click a button to open the ‘Format Axis’ dialogue box. I found the following code online to open the dialogue box. If I select a chart and then run the code from the toolbar (Developer tab, Macros, select macro, press ‘Run’), it works well.
Sub formatXAxis1()
ActiveChart.Axes(xlCategory).Select
Application.CommandBars.ExecuteMso "ChartFormatSelection"
End Sub
The trouble I have is when the VBA script is assigned to a button. If I use a shape as the button, I get “Run-time error ‘91’: Object variable or With block variable not set”. I get the same error if I use an ActiveX Command Button. However, with a Form Control button it works as expected. The only problem with this solution is that it is not possible to change the background colour of the button so it looks out of place against the other macro calling buttons on the worksheet.
I'm guessing that in the first two cases (shape and ActiveX buttons), VBA drops or loses the chart selection when I press the button – even though the chart still appears selected on screen. Is there a fix for this, or am I doing something wrong?

For an ActiveX button, in the Properties set the property
TakeFocusOnClick
to False. That will cause Selection to keep on the chart rather than switch to the button and your code should work. The color can also be changed from the properties box, though you probably already know that.

You need to reference the chart by name. So either have 4 buttons (one for each chart), or ask the user to input the name of the chart, then:
Dim co as ChartObject
Dim c as Chart
Dim NameOfChart as String
NameOfChart = Inputbox("Enter name of chart")
Set co = ActiveSheet.ChartObjects(NameOfChart)
Set c = co.chart
c.Axes(xlCategory).Select
Application.CommandBars.ExecuteMso "ChartFormatSelection"

Related

How to select an ActiveX Option/Radio Buttons in Form Controls

I have a Form Control with ActiveX Radio/Option Buttons in it.
The Form Control name is Side and contains Option/Radio Buttons with names xOption, oOption, and randomSide.
How would I be able to create a Macro that would allow me to set the radio buttons to a certain value upon opening the workBook. Recording a Macro of me clicking options results in a blank Macro. I've Already tried:
ActiveSheet.Shapes.Range(Array("Side")).Select
ActiveSheet.Shapes.Range("xOption").OLEFormat.Object.Value = 1
But this gives me error 1004 and other codes give me error 91. I'm really new to VBA so if I look stupid you know why.
Try something like this, using Worksheets instead of ActiveSheet:
Private Sub Workbook_Open()
Worksheets("your sheet name here").OLEObjects("xOption").Object.Value = 1
End Sub
As you want it to be selected after opening the sheet. Place this on ThisWorkbook.
You may try something like this...
ActiveSheet.OLEObjects("xOption").Object.Value = 1

VBA - Using .onAction on SmartArtNodes

I am new to VBA and trying to use smartArt.Nodes to dynamically generate an organizational chart based on some cell data. I am able to generate the chart with no problems. Now, I would like to be able to display more details of specific nodes of the chart by clicking on them. I am aware that we can convert smartArts into Shapes with the Drawing Tools in Excel and then use .onAction on them like:
ActiveSheet.Shapes(name1).OnAction = "detail"
However, is there a way to achieve the same thing with smartArt.nodes using VBA? Say I have a node called "nodes1" I have tried:
nodes1.Shapes.OnAction = "detail"
or
Dim objShape As Shape
Dim SmartArtNod As SmartArtNode
Set objShape = ActiveSheet.Shapes(1)
Set SmartArtNod = objShape.SmartArt.AllNodes(1)
SmartArtNod.Shapes(1).OnAction = "detail"
and other combinations, but none of them seem to work...
Thank you for your help!
Hard to prove a negative, but I believe this is not possible. Testing with Excel 2013, I can confirm the following do not work:
Trying to trap the Selection of the SmartArt using
Worksheet_SelectionChanged does not fire. If you change the
selection from a cell to the SmartArt object, you will not get an
event. You will get an event however when you lose focus on the
SmartArt and go back to the cell.
You can select a SmartArt object and then debug to check what is selected. The object is of type
Object/Shape and the watch window indicates that it exposes an
OnAction property, but attempting to set this property will throw
an error.
I take the two issues above to mean that is will be very difficult (if not impossible) to get an OnAction event to fire from the SmartArt.

Insert ActiveX Control Into Powerpoint slide

I'd like to insert a custom ActiveX control into a Powerpoint slide. I've created the custom control and registered it, and tested that it works. I can easily add the custom control to a UserForm, but can't add it directly to the slide (as per the other controls under Developer Tab -> Controls).
Is it possible to add the custom ActiveX control directly to the slide?
If not, is it possible to embed the UserForm directly to the slide?
Thanks!
I'm kind of guessing here, but it's worth giving it a shot. When you registered your ActiveX Control, I'm assuming you assigned a Program ID along with it, correct? If you did, then you might be able to add it as a shape object to the slide you specify. When you add a shape, you can add specific types of shapes, including OLEObjects.
For example, in the code below, I add an ActiveX ComboBox Control to Slide 1 in the Active Presentation using PowerPoint VBA. The critical thing to note is that the ProgID identifies the object being inserted.
Sub ActiveXControlAdd()
'Declare your variables.
Dim PPTPres As Presentation
Dim PPTSld As Slide
Dim PPTShp As Shape
'Grab the slide you want it on.
Set PPTPres = ActivePresentation
Set PPTSld = PPTPres.Slides(1)
'Add a shape, but make sure it's an OLEObject. Also, CLASSNAME is the ProgID!
Set PPTShp = PPTSld.Shapes.AddOLEObject(Left:=100, Top:=100, Width:=150, Height:=50, ClassName:="Forms.ComboBox.1")
'Print it out to make sure.
Debug.Print PPTShp.OLEFormat.ProgID
End Sub
Give it a try and see if that maybe fixes the issue.
If I understand, you want to insert ActiveX control to PowerPoint slide:
Go to File > Options > Customize Ribbon
On the second column, tick the "Developer" tab.
After you tick it you should see a "Developer" tab in PowerPoint; click on it.
The 3rd and 4th column is where the ActiveX controls are:
For example:
You want to insert a textbox.
You click on the icon that says "abc", next to the big "A".
Then you place it on your slide just like you would place a shape. Click & drag.

VBA Executing Sub Oddly

I have made this simple invoice control worksheet in excel and I use VBA to make it easier to visualize and add new items. I have made dozens of other VBA programmed Worksheets, and all of them have a "New" active x button, programmed just like this:
Private Sub ButtNew2_Click()
Dim Guia As Worksheet
Dim UltLin As Integer
Set Guia = Workbooks("Notas Fiscais.xlsm").Worksheets("Saída")
UltLin = Guia.UsedRange.Rows.Count + 1
Guia.Application.Goto Reference:="R" & UltLin & "C1"
FormNotasSaida.Show
FormNotasSaida.BoxDataEmiss.SetFocus
End Sub
Simple as that. Just select the first blank line so that the form loads blank. It works fine in any other Workbook. But in this one, if and every time I click this button, after closing the form, the next time (and only once) I load the form again in any possible way (either double clicking an item, pressing the "Show" button or pressing the "New" button again), it loads either blank or showing the last launched item (case you did so).
After closing it, I can click wherever or press the "Show" button whenever, they work fine, as they always have. The problem occurs exclusively once, after pressing the "New" button.
What am I possibly doing wrong, specially knowing that this method works perfectly in all other workbooks?
FormNotasSaida.Show
FormNotasSaida.BoxDataEmiss.SetFocus
Forms are a special kind of class modules with a designer and a predeclared ID attribute. This "predeclared ID" is what's at play here: you're using/reusing the default global instance of the class, and [unless you close it with the X button in the control box,] never unload it so you're always showing the same form.
A best practice is to create a new instance of the form every time you use it:
Set frm = New FormNotasSaida
frm.Show
frm.BoxDataEmiss.SetFocus

How to get selected text in VBA

I have a macro that changes the selected text, and I have it assigned to a button.
It works perfectly when i run it directly from visual basic, but when I click the button, the button gets the focus and my text is no longer selected so the macro change the selected element to (button).
How can I select the text and run the macro by clicking on the button and still have the text selected?
The way to do this is to set the set the TakeFocusOnClick property of the CommandButton to False. Here are is the code I use.
Private Sub CommandButton1_Click()
Dim Sel As Selection
Set Sel = Application.Selection
If Sel.Type <> wdSelectionIP Then
MsgBox Sel.Text
End If
End Sub
Is the button embedded in the document? You may need to put it on a form that loads on top of the Word window or in a menu/toolbar, so that clicking it does not affect the selection in the document itself.
Edit:
I think you can use Application.Selection.Previous to get at what you need. You could use this to restore the selection after the click event, or to act upon that section of the document, or both.
I assume that this is available in previous versions of Word, but have only confirmed its presence in 2007.
You need to change TakeFocusOnClick to "False" in the Button Preferences.