Find determined Shapes and change Color - vba

I have a document full of different shapes (almost 300). I'm trying to only change color of rectangles (almost 120), when a radial button is pressed. Can u guys help me?
My first thought was to use "select case" based on the name of the shape (which contain the word rectangles), but with no sucess.
Something like this:
Private Sub OptionButton4_Click ()
Dim sShapes as Shape
For Each sShapes In ActiveDocument.Shapes
Select Case sShapes.Name
Case InStr( , sShapes.Name, "Rectangle") = 1
sShapes.Fill.ForeColor.RGB = RGB(243, 43, 1)
Case InStr(1, sShapes.Name, "Retângulo") = 0
MsgBox "No"
End Select
End Sub

Thanks to the User Sgdva, i got it.
I'm using the following code.
Private Sub OptionButton4_Click()
Dim sShapes As Shape
For Each sShapes In ActiveDocument.Shapes
Select Case True
Case InStr(sShapes.Name, "Rectangle")
sShapes.Fill.ForeColor.RGB = RGB(243, 43, 1)
Case Else
End Select
Next sShapes
End Sub

Related

How to select multiple shapes by similar name in PowerPoint VBA?

Is there any way to select the multiples shape from slide with the same shape name.
For e.g., I have 5 shapes with the name "Textbox 60". And I want run a macro which select all the shapes from a slide named "Textbox 60". Have used the below code.
ActiveWindow.View.Slide.Shapes.Range("Textbox 60").Select
Here's one approach:
Sub Tester()
SelectByName ActivePresentation.Slides(1), "Textbox1"
End Sub
Sub SelectByName(sld As Slide, nm As String)
Dim s As Shape, first As Boolean
first = True
For Each s In sld.Shapes
If s.Name = nm Then
s.Select first 'Argument determines whether to add to
first = False ' existing selection, or replace it
End If
Next s
End Sub
You should try following #TinMan's suggestion though - that is the "better" way to go.
Activating and Selecting Objects should be avoided whenever possible. You are better of working with the Shapes using a ShapeRange.
Sub Main()
Dim ShapeRange As ShapeRange
Set ShapeRange = FindShapes(ActiveWindow.View.Slide, "Textbox 60")
If Not ShapeRange Is Nothing Then
End If
End Sub
Function FindShapes(Slide As Slide, Pattern As String) As ShapeRange
Dim Results() As Long
ReDim Results(1 To Slide.Shapes.Count)
Dim n As Long
Dim Index As Long
For Index = 1 To Slide.Shapes.Count
With Slide.Shapes(Index)
.Name = "Textbox 60"
If .Name Like Pattern Then
n = n + 1
Results(n) = Index
End If
End With
Next
If n > 0 Then
ReDim Preserve Results(1 To n)
Set FindShapes = Slide.Shapes.Range(Results)
End If
End Function
Note: I rewrote the code to handle multiple shapes with the same name.

How do I select format an active selection of words in a textbox

I'm trying to explore how do I apply some formatting to only few selected words in a textbox but so far unable to accomplish this myself.
Somehow with the code I created below, I can only use it to select all the words in the textbox instead of just a few words I want.
It would be great if anyone can provide me a simpler/ existing codes that can help me solve this please ?
Thanks in advance
Sub ActiveTextRange()
Dim sld As slide
Dim sh As Shape
Dim wordcount As Long, j As Long, x As Long, y As Long, z As Long
wordcount = ActiveWindow.Selection.ShapeRange(1).textFrame.TextRange.Words.Count
With ActiveWindow.Selection.ShapeRange(1)
.textFrame.TextRange.Words(Start:=1, Length:=wordcount).Font.Color.RGB = RGB(230, 0, 0)
End With
End Sub
The following might help. Key to this is being able to track the location of the specific text you want to change in amongst larger chunks of text; my suggestion is to format each bit of text as you add it to the shape. Cheers.
Option Explicit
Sub ActiveTextRange()
Dim vPresentation As presentation
Dim vSlide As Slide
Dim vShape As Shape
Dim vAddThisText As String
' Create a new presentation, add a slide and a rectangle shape
Set vPresentation = Application.Presentations.Add
Set vSlide = vPresentation.Slides.Add(vPresentation.Slides.Count + 1, ppLayoutBlank)
Set vShape = vSlide.Shapes.AddShape(msoShapeRectangle, 10, 10, 600, 300)
' Make the shape white with a 3pt dark red border
vShape.Fill.ForeColor.RGB = rgbWhite
With vShape.Line
.ForeColor.RGB = rgbDarkRed
.Weight = 3
End With
' Setup the shape to be left aligned, font color, top anchored, etc
With vShape.TextFrame
.TextRange.Paragraphs.ParagraphFormat.Alignment = ppAlignCenter
.TextRange.Font.Color.RGB = rgbBlack
.VerticalAnchor = msoAnchorMiddle
.TextRange.ParagraphFormat.SpaceAfter = 6
.TextRange.ParagraphFormat.WordWrap = msoCTrue
End With
' And now format the word red, which is the 7th character and is 3 long
vAddThisText = "Hello Red World"
vShape.TextFrame.TextRange.InsertAfter vAddThisText
With vShape.TextFrame.TextRange.Characters(7, 3)
.Font.Color.RGB = rgbRed
' and change other attributes if needed etc
End With
End Sub
And the output is ...
This colors the second and third words red in a Title placeholder. After Words, the first number is the starting position and the second number is the length:
Sub ColorWords()
Dim objSlide As Slide
Dim objShape As Shape
For Each objSlide In ActivePresentation.Slides
For Each objShape In objSlide.Shapes
If objShape.Type = msoPlaceholder Then
If objShape.PlaceholderFormat.Type = ppPlaceholderTitle Or objShape.PlaceholderFormat.Type = ppPlaceholderCenterTitle Then
With objShape.TextFrame2.TextRange.Words(2, 2).Font.Fill
.Solid
.ForeColor.RGB = RGB(255, 0, 0)
End With
End If
End If
Next objShape
Next objSlide
End Sub
To color a word selection, use:
ActiveWindow.Selection.TextRange.Font.Color.RGB = RGB(Red:=255, Green:=0, Blue:=0)
OK. I think I better understand the ask ... but I'm assuming in this response you're selecting text ... rather than just a shape itself. So you're editing the powerpoint, select some text in a shape, and want to run a macro to format(?) It should be as simple as creating the following in a code module (and then I created a custom access toolbar link to run the macro at the top of PowerPoint to make it quick):
Option Explicit
Sub ActiveTextRange()
ActiveWindow.Selection.TextRange.Font.Color.RGB = rgbRed
End Sub
Before:
Select the text "Red" and run macro:
Btw ... if you want to select just the shape and have some logic choose the text, the concept is a mix of this and my first answer.

Excel VBA - color data labels ("value from cells") according to the font of the source

I have to run many bar charts in excel 2016, each one showing the company performance over the seasons, for a certain country. On top of each bar I'd like to see the %Change in this format [Color10]0%"▲";[Red] -0%"▼". Reason why I added the data labels, and I used the function "value from cells" to show the %Change instead of the amount sold. Now everything is in place, and my percentages are nicely placed on top of the bars, but no way I can color them automatically (positive green and negative red). I tried formatting the labels directly from the format window placed under "numbers", but I discovered it doesn't work at all when the label content is derived using "value from cells".
So I started looking into VBA, but since I'm pretty ignorant about programming, I didn't succeed. I'm looking for a code that changes the data labels of my chart so that they maintain the font of the source (in the source my %Change values are already in the desired format ([Color10]0%"▲";[Red] -0%"▼"). Googling I found different solutions but none worked. I'll post the ones I that look better to me.
Sub legend_color()
Dim SRS As Series
With ActiveChart
For Each SRS In .SeriesCollection
SRS.ApplyDataLabels AutoText:=True, LegendKey:= _False,
ShowSeriesName:=False,
ShowCategoryName:=False,
ShowValue:=True, _ ShowPercentage:=False,
ShowBubbleSize:=False
SRS.DataLabels.Font.ColorIndex = SRS.Border.ColorIndex
Next SRS
End With
End Sub
This one was the only one that actually run, and colored my labels all white. With the following I run into errors.
Sub color_labels()
Dim chartIterator As Integer,
pointIterator As Integer, _seriesArray() As Variant
For chartIterator = 1 To ActiveSheet.ChartObjects.Count
seriesArray=ActiveWorkbook.Sheets("Sheet1").ChartObjects(chartIterator). _Chart.SeriesCollection(1).Values For pointIterator = 1 To UBound(seriesArray)
If seriesArray(pointIterator) >= 0 Then
ActiveWorkbook.Sheets("Sheet1").ChartObjects(chartIterator). _
Chart.SeriesCollection(1).Points(pointIterator).Interior.Color = _RGB(146, 208, 80)
Else
ActiveWorkbook.Sheets("Sheet1").ChartObjects(chartIterator). _Chart.SeriesCollection(1).Points(pointIterator).Interior.Color = _RGB(255, 0, 0)
End If
Next pointIterator
Next chartIterator
End Sub
Sub ArrowColour()
Dim ncars As Integer
ncars = Range("A1").Value
With ActiveSheet.Shapes.Range(Array("Down Arrow 1")).Fill
If ncars > 0 Then
.ForeColor.RGB = RGB(0, 176, 80)
Else
.ForeColor.RGB = RGB(255, 0, 0)
End If
End With
End Sub
Option Explicit
Sub ApplyCustomLabels()
Dim rLabels As Range
Dim rCell As Range
Dim oSeries As Series
Dim Cnt As Integer
Set rLabels = Range("C2:C" & Cells(Rows.Count, "C").End(xlUp).Row)
Set oSeries = ActiveSheet.ChartObjects(1).Chart.SeriesCollection(1)
oSeries.HasDataLabels = True
Cnt = 1
For Each rCell In rLabels
With oSeries.Points(Cnt).DataLabel.Text = rCell.Value.Font.Color =rCell.Font.Color
End With
Cnt = Cnt + 1
Next rCell
End Sub
Thank you very much in advance for all of your help,
Tommaso
If you're just missing the colors then you can format each label using something like:
Sub Tester()
Dim s As Series, dl As DataLabels, d As DataLabel
Dim i As Long, rngLabels
Set s = ActiveSheet.ChartObjects(1).Chart.SeriesCollection(1)
Set dl = s.DataLabels
'Option 1: set label color based on label value
For i = 1 To dl.Count
With dl(i)
.Font.Color = IIf(Val(.Text) < 0, vbRed, vbGreen)
End With
Next i
'Option 2: set label color based on label source cell
' Note use of DisplayFormat to pick up custom
' formatting colors
Set rngLabels = Range("C7:C13")'<< source range for data labels
For i = 1 To dl.Count
dl(i).Font.Color = rngLabels(i).DisplayFormat.Font.Color
Next i
End Sub

VBA error 13 run time

Here is my code for an interactive cart, but i have
error 13 run time
Sub Freeform124_Click()
Dim NomShape As String
NomShape = Application.Caller
For Each Shape In ActiveSheet.Shapes
form.Fill.ForeColor.RGB = RGB(0, 50, 0)
Next Shape
End Sub
The problem with your code is that you use for each Shape loop and then you write form.fill.forecolor.
Probably. Plus the application.caller parsed to string.
Here is a good way to change the color of all objects, if you make the macro clickable from an object.
Option Explicit
Sub Rechteck1_Klicken()
Dim oCaller As Object
Dim shShape As Shape
Set oCaller = ActiveSheet.Shapes(Application.Caller)
oCaller.Fill.ForeColor.RGB = RGB(100, 250, 250)
For Each shShape In ActiveSheet.Shapes
If shShape.Name <> oCaller.Name Then
shShape.Fill.ForeColor.RGB = RGB(110, 50, 0)
End If
Next shShape
End Sub

Convert shape size to cm

I have VBA code for changing size of shapes, but I want to convert the number to be in cm. Any suggestion for how to convert these numbers?
Another question is that I want to change the same size for multiple selected shapes; can you help me with this as well?
Thank you very much!
Sub test()
Dim objHeigh As Integer
Dim objWidth As Integer
Dim oSh As Shape
On Error GoTo CheckErrors
With ActiveWindow.Selection.ShapeRange
If .Count = 0 Then
MsgBox "You need to select a shape first"
Exit Sub
End If
End With
For Each oSh In ActiveWindow.Selection.ShapeRange
objHeigh = oSh.Height
objWidth = oSh.Width
objHeigh = CInt(InputBox$("Assign a new size of Height", "Heigh", objHeigh))
' give the user a way out
If objHeigh = 0 Then
Exit Sub
End If
If objName <> "" Then
oSh.Name = objName
End If
objWidth = CInt(InputBox$("Assign a new size of Width", "Width", objWidth))
' give the user a way out
If objWidth = 0 Then
Exit Sub
End If
oSh.Height = CInt(objHeigh)
oSh.Width = CInt(objWidth)
Next
Exit Sub
CheckErrors: MsgBox Err.Description
End Sub
According to MSDN, the height / width of the corresponding shape properties are specified in points:
Returns or sets the height of the specified object, in points.
Read/write.
and on that page they specifically show an example and referring to the fact, that 1 inch has 72 points
This example sets the height for row two in the specified table to 100
points (72 points per inch).
Therefore I guess it is safe to rely on that fact and just write a function to convert it yourself:
Function ConvertPointToCm(ByVal pnt As Double) As Double
ConvertPointToCm = pnt * 0.03527778
End Function
Function ConvertCmToPoint(ByVal cm As Double) As Double
ConvertCmToPoint = cm * 28.34646
End Function
As far as your question with sizing multiple objects is concerned, I am not sure if I understand your problem fully. I interpreted it in a way so that moving your prompts to the user out of the For loop should give you the desired result (if that is in fact your desired result :)):
objHeigh = CInt(InputBox$("Assign a new size of Height", "Heigh"))
' give the user a way out
If objHeigh = 0 Then
Exit Sub
End If
objHeigh = ConvertCmToPoint(objHeigh)
objWidth = CInt(InputBox$("Assign a new size of Width", "Width"))
' give the user a way out
If objWidth = 0 Then
Exit Sub
End If
objWidth = ConvertCmToPoint(objWidth)
For Each oSh In ActiveWindow.Selection.ShapeRange
If objName <> "" Then
oSh.Name = objName
End If
oSh.Height = CInt(objHeigh)
oSh.Width = CInt(objWidth)
Next