Is it possible to address a shape with VBA code? - excel

I am replacing all my ActiveX controls with Excel shapes, because of this well know problem. Therefore I replaced each ActiveX Button with a rectangular shape assigning a macro to each shape:
My question is if I can address those 'shape buttons' with my vba code. Something simple like change the backgroung color of the "Review Start" button should be possible, right?
I'm thinking of something like:
Activesheet.shapes("Review Start").background.colorindex = 1
(This code is obviously not working)

One way is this. Assign a variable to the shape and then you can access its properties and methods easily. I'm not sure there's a way without using RGB.
By declaring the variable as Shape type, Intellisense will show you the properties and methods. Also you can use the Object Browser (F2).
Sub x()
Dim s As Shape
Set s = ActiveSheet.Shapes("Review Start") 'better to specify a sheet name
With s
.Fill.ForeColor.RGB = RGB(255, 255, 255)
.TextFrame.Characters.Font.Color = vbBlack
.TextFrame.Characters.Text = "Fred"
End With
End Sub

Related

Draw Shape Based on Cell Values

All,
I have code that creates a shape based on based on inputted values in the macro itself. I am wanting to have the values of shape type, width, and height specified by the user (location of shape on the sheet is indifferent to me right now). The user would input the aforementioned numerical values for width and height into the cells and click a button which would output the shape type and size the user wants.
In my case, there will be a drop down box for "rectangle" and "circle". I don't know how to get the code to read those words and convert it '1' and '9', respectively. I may just have the user choose 1 or 9 to create the shape.
I would also like to add text to the center of the shape. Again, I have created a code for this but it is within the macro. I would like to have the code reference a cell value instead. I assume it would be the same as above.
Thank you for any assistance.
Sub AddShape()
Dim s As Shape
Dim ws As Worksheet
Set ws = Sheets("Deck Layout")
'add a shape
Set s = ws.Shapes.AddShape(1, 80, 80, 75, 75)
'make it nearly white
s.Fill.ForeColor.RGB = RGB(245, 245, 255)
'show text within it
s.TextFrame.Characters.Text = "1"
s.TextFrame.Characters.Font.ColorIndex = 2
With s.TextFrame.Characters(0, 0)
s.TextFrame.HorizontalAlignment = xlHAlignCenter
s.TextFrame.VerticalAlignment = xlVAlignCenter
.Font.Color = RGB(0, 0, 0)
End With
End Sub
Since you've already got parts of the answer in the comments, I'll focus on the shape picking.
Have a look at this:
Dim ShapeType As MsoAutoShapeType
Select Case LCase(ws.Range("b1").Value)
Case "rectangle"
ShapeType = msoShapeRectangle
Case "circle"
ShapeType = msoShapeOval
End Select
Set s = ws.Shapes.AddShape(ShapeType, 80, 80, 75, 75)
It will find the value in B1, convert it to lower case and the test it for "rectangle" and "circle" and the set the ShapeType to a corresponding value.
You can use 1 and 9 instead, but that is bad practice. Use the defined constants - it will make your code much easier to read.

Shading selected cell with a certain colour at key press in Excel

I'm a student and I have to learn a lot of stuff/questions, so I came up with an idea and I don't know how to realize it.
So all I am asking is for someone kind to show me how I can program Excel to specifically shade the selected cell with a certain color with a press of a button, as the title suggests.
I want to get in a special mode, where:
I can move around the cells with the arrow keys
by pressing 1 or 2 or 3 or 4, then the selected cell gets shaded with a specific color. (each number for a different color)
The question is, how do I do it? Is something like that possible at all in Excel? I have some background with code, but not even close to a professional. I just did VB Stuff for my GCE A Levels in Computing.
Macros are also possible as I can see but really don't know how to use them.
So here I am, asking this question, hoping someone kind will maybe see it as a challenge and help me realize it. I would invest the time to learn how to do this myself, but with the free time I have now, something like that would take months, just because I'm thorough and I want to learn from the absolute basics.
To enter this special mode you can run a macro to assign new purposes to those keys.
In a module in the VBE Editor, you can add:
Sub EnterSpecialMode()
Call Application.OnKey("1", "color1")
Call Application.OnKey("2", "color2")
Call Application.OnKey("3", "color3")
Call Application.OnKey("4", "color4")
End Sub
Sub ExitSpecialMode()
Call Application.OnKey("1")
Call Application.OnKey("2")
Call Application.OnKey("3")
Call Application.OnKey("4")
End Sub
Sub color1()
Selection.Interior.Color = RGB(255, 0, 0)
End Sub
Sub color2()
Selection.Interior.Color = RGB(0, 255, 0)
End Sub
Sub color3()
Selection.Interior.Color = RGB(0, 0, 255)
End Sub
Sub color4()
Selection.Interior.Color = RGB(255, 255, 0)
End Sub
Self-explanatory, but to enter the special mode you have to run the EnterSpecialMode. And to exit run the ExitSpecialMode sub

VBA: Change the Background of a Created Button

Hello Developers and other, :)
I'm developing a macro to create a lot of button automatically, and I wants to give them a specific background color.
With Worksheets("Sheet 1")
Set r_BtnRange = .Range("O3:O4")
For Each r_Cell In r_BtnRange.Cells
Set b_Btn = .Buttons.Add(r_Cell.Left, r_Cell.Top, _
r_Cell.Width, r_Cell.Height)
With b_Btn
.Caption = "Email End-User"
.OnAction = "Email_EndUser"
.Font.Color = RGB(255, 255, 255)
'.BackColor = 16711680
End With
Next
End With
The .Button.Add return a Button Object, witch has no BackColor Proprety (according to this: https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.button?view=excel-pia and the fact I receive an error when tying to uncomment the line)
please see Here
I could use a Shape to do it, but I would know if there is really any method to do it (btw, had to disable it and modify multiple propriety of it.
Thanks a lot for your answers,
Antoine

How To: Hover Over Shape Shows TextBox

I am trying to make it so when a user hovers over a triangle as shown below, a textbox pops up with certain information pertaining to that triangle.
Triangles are drawn with the following function...
Public Sub drawTriangle(theRow As Integer, theColumn As Integer, Optional myColor As System.Drawing.Color = Nothing)
myColor = System.Drawing.Color.Black
Dim theShape As Microsoft.Office.Interop.Excel.Shape = xlWorkSheet.Shapes.AddShape(MsoAutoShapeType.msoShapeIsoscelesTriangle, (xlWorkSheet.Cells(theRow, theColumn)).Left + 18, (xlWorkSheet.Cells(theRow, theColumn)).Top, 15, 14)
theShape.Fill.ForeColor.RGB = ToBgr(myColor)
theShape.Line.ForeColor.RGB = ToBgr(myColor)
End Sub
I haven't been able to find any examples or good documentation about how I would do this, so I thought I'd ask here. Any and all help is much appreciated!!
According to MSDN, comments can be added to ranges. For this to work for your application, you simply need to select the range that corresponds to your shape, and then call AddComment().
The numeric coordinates of a cell can be used to determine the actual Cell name (i.e. E5) by using code like the following(source):
address = xlWorkSheet.Cells(RowVariable, ColVariable).Address
This can be followed up with:
xlWorkSheet.Range(address).AddComment("This is a comment")

Referencing multiple shapes by name

I'm trying to turn multiple shapes (just standard rectangles created by insert -> shapes) yellow when I click on a command button. I selected all the shapes and named them "rect" The name manager acknowledges that the name exists and it includes all the specified shapes. However, when I try to run this
ActiveSheet.Shapes("rect").Fill.ForeColor.RGB = RGB(255, 255, 0)
I get a runtime error "The item with the specified name wasn't found." The weird thing is that this works perfectly fine with just one shape, but when the name refers to multiple shapes then it doesn't work. Any ideas? Thank you.
Select all the shapes as needed and run the code below
Sub Carmello()
Selection.Group.Select
Selection.Name = "rect"
ActiveSheet.Shapes("rect").Fill.ForeColor.RGB = RGB(255, 255, 0)
End Sub

Resources