Manipulating excel "autoshapes" with VBA - vba

I am trying to write a macro in VBA (Excel) that is assigned to a Checkbox. Whenever the checkbox is clicked, an "autoshape" will change its "order" from "Send to Back" to "Send to Front".
Basically, I am trying to create a dashboard with multiple panels, so that users can access information without moving between sheets. Each panel will have a rectangular autoshape as its background and the components of the panel will be "grouped" within the autoshape.
Can this be done? I would greatly appreciate any ideas into writing the code.
Thanks,

I'm not quite following your larger goal, but in order to bring a shape to the front use this:
If MyCheckBox.Value = True Then
MySheetName.Shapes("MyShapeName").ZOrder msoBringToFront
End If
You should select your desired ZOrder movement from the MsoZOrderCmd enumeration and your code should be in the Change event routine for your checkbox control.
EDIT:
You could also refer to the shape by its index number. For example:
MySheetName.Shapes(0).ZOrder msoBringToFront
Also, to get the name of a shape, either click it and look in the Name Box in the upper left corner of Excel (below the toolbars), or iterate through all the shapes like so:
Sub Macro1()
Dim MyShape As Shape
For Each MyShape In Sheet1.Shapes
Debug.Print MyShape.Name
Next MyShape
End Sub

Related

How to show/hide data from a table in Powerpoint?

I'm trying to have several slides with tables, each table has 3 columns, the last column is the "reference value" and I want it to be hidden during the presentation and show it only by pressing a button or a hyperlink, each row individually. I think it's possible because I'm really new at coding and I have managed to do it by changing the cell's text format from white (which is the table's background color) to red, but I can only do it for all the tables at once, and I need individual values on each one of them. (I hope I'm making myself clear). This is what I have done so far:
Sub format()
Dim s As Slide
Dim oSh As Shape
Dim oTbl As Table
For Each s In ActivePresentation.Slides
For Each oSh In s.Shapes
If oSh.HasTable Then
Set oTbl = oSh.Table
With oTbl.Cell(2, 3).Shape.TextFrame.TextRange
.Text = "4500-9000"
.Font.Size = 12
.Font.Color = vbRed
End With
End If
Next
Next s
End Sub
But this will change the same cell on every table I have, I want it to change specific cells in specific tables one by one, since they all have different valued. I know I could do this with animations, but I'd rather do it this way.
EDIT: It would be great if, instead of pressing a button, I could get the data by hovering the pointer over the empty cell, and have it hidden away again when I hover the cursor off the cell.
In any case, whenever I do any change to the presentation during slideshow, the change will still be there at the end, which means It would only work once and then I would have to fix and hide all the values again, is there a way to restore the changes done during the presentation when it ends?
you could adpt your sub and call it from another procedure by passing a reference to the table and cell you want to process like this:
Sub FormatTableCell(oTbl As Shape, lRow As Long, lCol As Long)
With oTbl.Cell(lRow, lCol).Shape.TextFrame.TextRange
.Text = "4500-9000"
.Font.Size = 12
.Font.Color = vbRed
End With
End Sub
For example:
With ActivePresentation
FormatTableCell .Slides(1).Shapes("MyTable"), lRow:=1, lCol:=1
End With
Regarding the triggers for hover over and hover out, this is a tricky aspect of PowerPoint. There IS a trigger to run code when hovering over a shape but there is NOT a trigger for hovering out of a shape. To achieve the latter, you could put a transparent rectangle in the back layer of your slide and use that to spoof the hover out trigger by linking a hover over macro to it. Write your code and use the Insert / Action / Mouse Over function to trigger your VBA procedure with a signature like this:
Public Sub FormatThisTable(oTbl As Shape)
Note that this method only passes the shape (a table in your case) and not the cell the mouse is hovering over. The only way I can see you could achieve that would be to use a lot of very complex Windows APIs to detect the mouse cursor position relative to the table's on-screen coordinates.
Alternative approaches could either be to ungroup the table to a set of separate shapes or create cover shapes for each cell you need to show/hide and manage their visibility properties using the mouse in/out technique above.
For the last point, you will need to use application level events which requires code in a class module and this a good article to show you how:
http://www.pptfaq.com/FAQ00004_Make_your_VBA_code_in_PowerPoint_respond_to_events.htm
Step 1
Launch PowerPoint and open the PPTX file that contains the rows you want to hide. Click the appropriate slide in the Slides pane to the left of the screen.
Step 2
Double-click the spreadsheet on the slide, which will allow you to edit it. Select the rows that you want to hide. Click the “Home” tab and locate the “Cells” section. Click the “Format” option, which will display a list of available features.
Step 3
Place the pointer over the “Hide & Unhide” listing in the “Visibility” section. Click the “Hide Rows” option to hide the selected rows. Click outside the spreadsheet to return to the PowerPoint slide.

Select linked cell, cell next to checkbox, or get the checkbox name

I have a document full of Checkboxes and I dont want to write specific VBA code for each checkbox because the file size needs to stay relatively small. What I am trying to do with my code is when the checkbox is checked, it automatically selects the cells next to it(not hard coded in using "Range") and then perform the rest of the programed VBA function.
How do I either get the name of the checkbox, select the linked cell, or select the cell next to the checkbox using some kind of "offset" property? I am completely stumped!
Thanks for your help in advance.
Use Form Controls instead of ActiveX Controls for Check Box.
Following code will not be work with check box from ActiveX Controls. Also, you need to assign macro to the checkbox, simply trying to run this code from VBEditor will give error.
Assuming all the checkboxes are on same sheet, select all your checkboxes and assign them same macro, something like this
Sub checkBoxHandler()
Dim shp As Shape
Set shp = ActiveSheet.Shapes(Application.Caller)
MsgBox shp.Name 'Name
MsgBox shp.TopLeftCell.Offset(1).Address ' 1 Rows below checkbox
ActiveSheet.Range(shp.ControlFormat.LinkedCell).Select ' Select linked cell.
Set shp = Nothing
End Sub
here Application.Caller helps VBA to identify which checkbox is being clicked.

Get Picture from Worksheet into Excel Userform

I am looking to view an image from the worksheet in an Image control on a userform.
This image will change based on the value on a combobox. I have inserted (Using: Insert -> Pictures) a couple of images into "Sheet1" of my workbook and named them "Picture1" & "Picture2".
I have created the below UserForm:
Form http://im56.gulfup.com/msKyqi.png
And this is the code that I am trying to use in order to load the images from the sheet, but unfortunately, this is not working at the moment.
Private Sub ComboBox1_Change()
UserForm1.Image1.Picture = LoadPicture(Worksheets("Sheet1").Shapes(ComboBox1.Value))
End Sub
Private Sub UserForm_Initialize()
UserForm1.ComboBox1.Clear
UserForm1.ComboBox1.AddItem "Picture1"
UserForm1.ComboBox1.AddItem "Picture2"
UserForm1.ComboBox1.Value = "Picture1"
UserForm1.Image1.Picture = LoadPicture(Worksheets("Sheet1").Shapes(ComboBox1.Value))
End Sub
Every time I run this code I get the below error:
Error http://im43.gulfup.com/YoWvTp.png
Please advise.
I figured it out!
As I am using a UserForm there is a workaround to the issue.
Instead of having the images in the worksheet to then try and load them in the form I tried having them in the UserForm in the first place, here is how.
Create a frame on your userform:
Frame http://im88.gulfup.com/Moy8I6.png
Set the visible property of the frame to "False":
Visible http://im88.gulfup.com/sAIQqh.png
Insert your images by adding a picture control and loading the images, you can add as many images as you need:
Images http://im88.gulfup.com/oas0EQ.png
Name the images:
Name http://im88.gulfup.com/cIO317.png
Drag all the images one over the other into the frame, (you can then move the frame into a corner so it doesn't bother you:
Drag http://im88.gulfup.com/1fOSut.png
Move Away http://im88.gulfup.com/Q1fzKd.png
Next create a picture control, this is what you will use to display the picture based on a selection:
Form View http://im88.gulfup.com/X1UVRB.png
In this example, I am going to use a combobox for the selection. Now insert the below code in to the form which is pretty straight forward:
Private Sub ComboBox1_Change()
' Image1 is the name of the created picture control
UserForm3.Controls.Item("Image1").Picture = UserForm3.Controls.Item(UserForm3.ComboBox1.Value).Picture
End Sub
Private Sub UserForm_Initialize()
UserForm3.ComboBox1.AddItem "Argentina"
UserForm3.ComboBox1.AddItem "Brazil"
UserForm3.ComboBox1.AddItem "Chile"
End Sub
As you will see, the frame with the pictures is Hidden, and the image is changing inside the picture control based on a selection:
Result http://im88.gulfup.com/MSqyHF.png
I think it's the better way to go as opposed to exporting the images from the worksheet to a Temp folder and then loading them back into the picture controls.
The LoadImage() function expects a filename (which can be fully qualified with a drive letter and path). You are getting a type mismatch because it wants a string, and you are giving it an image object.
There is, as far as I know, no simple way to put an image that resides in the current application into an image control. The (hackish) workaround that I know about is to export the image to a file, and then import that same file using LoadImage().
This is the same path you have to go down if you want to embed a chart that updates dynamically into a userform. You export the chart as an image (e.g., a JPEG), and then use LoadImage() to pull the image back into the image control.
I know this post is ancient, but I found my way here. I came up with a slightly different solution to this problem. I have about 30 pictures I need to load based on a combo-box selection. First, all combo-box options (for discussion "XX") are saved to a separate worksheet which is "Very hidden" from the user and loaded into the combobox on userform activation. On the userform a frame was added, and within the frame 30 image-boxes all overlapping perfectly were placed. Each image-box was carefully named "Img_XX" where XX is the simple two-letter identifier.
With this setup it is now possible to iterate through each "Control" (the image boxes) in the Frame and hide them all, except the one with a name that matches the combo-box value. The code in the userform module, within the Combobox_Change() function, looks something like this:
Private Sub ComboBox_Change()
Dim SearchValue as String
SearchValue = me.Combobox.value
Dim Ctrl as Control
For each Ctrl in Me.TestFrame.Controls
If Ctrl.Name Like "img_" & SearchValue Then
Ctrl.visible = True
else
Ctrl.Visible = False
End If
next Ctrl
End Sub
I hope this helps, let me know what you think. :)

Is it possible to use VBA to hide/show a text box on a Custom Layout in a PowerPoint 2010?

Is it possible to use VBA to hide/show a text box on a Custom Layout in a PowerPoint 2010? I would like to hide/show a specific text box that is on the custom layout of each slide at the click of a button and am not sure of the best way to go about doing that.
Any help, much appreciated.
Suppose you have a shape named Rectangle 6 on the third layout of the first slide master.
Sub Example()
Dim oSh As Shape
' Get a reference to the shape
Set oSh = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(3).Shapes("Rectangle 6")
' Set its visible property to false
oSh.Visible = False
End Sub
Generally, yes, though setting some shapes (title placeholder on the master, for example) won't necessarily make the title text on individual slides disappear. "Slide master" can mean different things in different PPT versions. Which are you targeting, and do you want to hide a shape on ALL masters/layouts in the presentation or just some?

Is it possible to identify the shape ID that triggers a mouseover on a powerpoint presentation?

Is it possible to identify the shape ID that triggers a mouseover on a powerpoint presentation.
What I want to do is have a series of shapes on a slide with titles, and when the user mouseovers a shape, I want to use VBA to show more details about the shape in another part of the page, possibly in a separate help text box. I could then use the selected shapes ALt Text to display in the help box. What i can't see is how to identify what shape has triggered the mouseover macro. Is this possible? If I can identify the shape that triggered the action, I could then get info about that shape.
The Shape object in PowerPoint does not allow you to interact with its events as you normally would with other objects in VBA code. The only two 'events' that are exposed are not actually VBA events. They are a special class called Actions, and they are 'ppMouseOver' and 'ppMouseClick'. If you know all the information about your shapes and their corresponding information beforehand, you could write some code to do what you want, but it would all be hard-coded, which is probably not what you want.
You could run a routine something like this when the presentation starts:
Sub SetActionsRoutine()
Shape1.ActionSettings(ppMouseOver).Action = ppActionRunMacro
Shape1.ActionSettings(ppMouseOver).Run = "showInformation1"
Shape2.ActionSettings(ppMouseOver).Action = ppActionRunMacro
Shape2.ActionSettings(ppMouseOver).Run = "showInformation2"
...
...
End Sub
And then whenever you did a MouseOver of those shapes, one of the following routines would run.
Sub showInformation1()
myTextBox.Text = Shape1.AlternativeText
End Sub
Sub showInformation2()
myTextBox.Text = Shape2.AlternativeText
End Sub
This is very limited and requires that you write a subroutine for every shape on your slide. Probably not the approach you want, but again, with PowerPoint, your options are very limited.
New to StackOverflow, late to answer, but you can do this:
Sub RespondToShape(oSh as Shape)
MsgBox "You clicked " & oSh.Name
End Sub
Assign every shape you want to interact with an Action setting of Run Macro and choose RepondToShape as the macro.