If I use VBA in Excel to make a line chart with multiple series, and two of the series' data are very similar so that their chart series lines partly overlap, the last one written is in front of earlier ones written.
In the Worksheet_Change event, I want to be able to go back and forth between which chart series line is in front, based on user actions that change the data. Can I do that without deleting and recreating the chart?
Here's how I'm identifiying the series line, for example here's series 2:
Sheet1.ChartObjects("MyChart").Chart.SeriesCollection(2)
Getting TypeName on that returns Series. I see Series in help, but with no information on its properties and methods. I don't see Series in the Object Browser (I'm on Excel 2007). I was able to get a list of properties and methods in the context help dropdown, but I didn't see anything promising in the dropdown.
So, can I bring a chart series to the front/send it to the back, without deleting and recreating the chart?
Here is a simple non-VBA way to highlight one series in a chart.
Below I show X data in column B, Y data for series alpha, beta, and gamma in columns C:E, and extra data in column F.
I have a chart, and below the chart a combo box inserted using Developer tab > Insert > Form Controls > Combo Box. I formatted the control (right click) and set the Input Range to K16:18, and the Cell Link to J16.
I selected F2:F16, with F2 being the active cell. I entered =INDEX(C2:E2,$J$16) in the formula bar, then held Ctrl while I pressed the Enter key. This filled the formula into the entire range. Essentially the formula takes the value from the three cells to the left, based on the Cell Link of the combo box. Since I've selected "beta" in the combo, J16 contains the value 2, and column F shows the values of the second data column.
The chart was made using the entire data range. The first three series were formatted with light gray lines, and the fourth (duplicate) series was formatted with a color that stands out in comparison. All data is visible at once, but only one is highlighted.
What you can do is change all the series line colors to a light color except for one. That one you color dark or bright, ie Red. Then you can repeat the process (assign the macro to a button on/near the chart) to highlight each series in turn.
Sub CycleSeriesColors()
Dim seriesCount As Integer, i As Integer, smod
Static s '"Remember" what series we are on between calls.
ActiveSheet.ChartObjects("Chart 1").Activate
' ActiveChart.PlotArea.Select
seriesCount = ActiveChart.SeriesCollection.Count
s = s + 1
smod = s Mod (seriesCount + 1)
' ActiveChart.ChartArea.Select
If Not smod = 0 Then
With ActiveChart.SeriesCollection(smod).Format.Line
.Visible = msoTrue
.Visible = msoTrue
.ForeColor.RGB = RGB(192, 0, 0)
.Transparency = 0
End With
For i = 1 To seriesCount
If Not i = smod Then 'the series is to backgrounded
With ActiveChart.SeriesCollection(i).Format.Line
.Visible = msoTrue
.Visible = msoTrue
.ForeColor.RGB = RGB(240, 240, 240)
.Transparency = 0.85
End With
End If
Next i
Else
Randomize
For i = 1 To seriesCount
With ActiveChart.SeriesCollection(i).Format.Line
.Visible = msoTrue
.Visible = msoTrue
.ForeColor.RGB = RGB(((i * i) ^ 0.6 * 3), ((i * i) ^ 0.6 * 8), ((i * i) ^ 0.6 * 12))
.Transparency = 0
End With
Next i
End If
End Sub
Related
Win10x64 Office 365 PPT v 16.0.12325.202080 64-bits
I need to show a character with a yellow font color but a black outline, for readability purposes. This character goes inside a Powerpoint table Cell.
The following link has a method that I'm currently using that consists of creating a dummy shape, adding text to it, modify the shape's textframe2 textrange font line properties and then copying it and pasting it back to the table cell.
http://www.vbaexpress.com/forum/archive/index.php/t-43787.html
This was asked 8 years ago, but I'm currently seeing the same behaviour where we can't directly manipulate the textframe2 wordart format of text inside a cell. The program doesn't show an error but doesn't work.
I have given up on trying to modify the textrame2 textrange font line properties directly from VBA.
I have managed to get it to activate the font outline color using
Application.CommandBars.ExecuteMso ("TextOutlineColorPicker")
After it's activated I thought I could modify the textframe2 textrange font line properties, but it still doesn't work.
Is there an Application.CommandBars idMso for changing the font outline color and font outline line width inside a table cell?
Or another other than pasting the formatted text inside a table cell.
Edit:
Adding an image to illustrate what I mean by text color and text outline color and the menu used to show them in red circle:
Edit2
Added another snapshot to exemplify a character inside a cell with black outline and a character inside a cell without an outline
Thanks
Here's an example to access a Table on a given slide and change one cell's attributes. The example slide I'm using looks like this
The code itself creates a function that allows you to select a table from a particular slide, and a single cell within the table and highlight it.
Option Explicit
Sub test()
HighlightTableCell 1, 2, 3
End Sub
Sub HighlightTableCell(ByVal slideNumber As Long, _
ByVal thisRow As Long, _
ByVal thisCol As Long)
Dim theSlide As Slide
Set theSlide = ActivePresentation.Slides(slideNumber)
Dim shp As Shape
For Each shp In theSlide.Shapes
If shp.Type = msoTable Then
Dim theTable As Table
Set theTable = shp.Table
With theTable.Cell(thisRow, thisCol)
With .Shape.TextFrame2.TextRange.Characters.Font.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 255, 0)
.Transparency = 0
.Solid
End With
With .Shape.TextFrame2.TextRange.Characters.Font.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorText1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
End With
End With
End If
Next shp
End Sub
This question should be answered by Microsoft Office developers.
Currently, to overcome this bug-some situation, I think, copying the formatted text outside the table and pasting it into a table cell is the only work-around for this trouble.
As you mentioned, according to John Wilson, one of the most brilliant PowerPoint MVPs(http://www.vbaexpress.com/forum/archive/index.php/t-43787.html), if we copy the text from a textbox or shape that is located outside of the table, the format of the text can be preserved even for the text in a table cell.
Option Explicit
Sub test()
Dim shp As Shape, tshp As Shape
Dim sld As Slide
Dim tbl As Table
Dim r%, c%
If ActiveWindow.Selection.Type = ppSelectionNone Then MsgBox "Select a table first.": Exit Sub
Set shp = ActiveWindow.Selection.ShapeRange(1)
Set sld = shp.Parent
'add a temporary textbox for copying the formatted text into a cell
Set tshp = sld.Shapes.AddTextbox(msoTextOrientationHorizontal, 0, 541, 960, 540)
tshp.Visible = False
Set tbl = shp.Table
For r = 1 To tbl.Rows.Count
For c = 1 To tbl.Columns.Count
'1) cell -> 'tshp'
tbl.Cell(r, c).Shape.TextFrame2.TextRange.Copy
tshp.TextFrame2.TextRange.Paste
'2) outline the text in 'tshp'
With tshp.TextFrame2.TextRange.Font.Line
.Visible = msoTrue
.Weight = 0.2
.ForeColor.RGB = RGB(255, 127, 127)
End With
'3) 'tshp' -> cell
tshp.TextFrame2.TextRange.Copy
tbl.Cell(r, c).Shape.TextFrame2.TextRange.Paste
'// the code below doesn't work
'With tbl.Cell(r, c).shape.TextFrame2.TextRange.Characters.Font.Line
'With tbl.Cell(r, c).shape.TextFrame2.TextRange.Font.Line
' .Visible = msoTrue
' .Weight = 0.5
' .ForeColor.RGB = RGB(255, 127, 127)
'End With
Next c
Next r
'remove the tempoarary textbox
tshp.Delete
End Sub
The above snippet creates a temporary textbox on left-top area of the slide and applies the outlined text format. Then, it copies the content of each cell to the temporary textbox and copy/paste the formatted text back to the cell. By using this method, we can apply the outlined text format to the text in a cell.
I need to fill a cell with different colors as in this picture (3 rows are merged vertically and colors are drawn manually in this picture using 3 rectangular shapes):
The only way I could find to fill part of a cell is using conditional formatting (by setting style as data bar and fill as solid) but it support only one color.
Is this possible with or without VBA?
It is possible.
I have found two ways to do that.
1- Using a black square shaped character (character code 2588 – vba: ActiveSheet.Cells(1, 1) = ChrW(&H2588)) and color them according to percentage. This character fills the cell height and also there is no spacing between them which allows filling a cell completely (Sure you should consider left indent in a cell). Only issue here that you cannot use a lot of characters in one cell; I use 30 of them and scale the number of characters according to 30 (ie. 50% red means 15 red character-2588).
2- It is same as what #Doktor Oswaldo has suggested: Inserting a plot in a cell using cell's position and size in pixels. This method has one big advantage: you can show the ratios exactly. In addition, you can fill a data series with a pattern as well. However if you have a lot of plots, you will sacrifice from Excel performance. For plot settings, I use following VBA code:
'Define var's
Dim src As Range, targetCell As Range
Dim chacha As ChartObject
'Set var's
Set src = Worksheets("Sheet1").Range("B1:B3")
Set targetCell = Worksheets("Sheet1").Range("C2")
'Create plot at the target cell
Set chacha = Sheets("Sheet1").ChartObjects.Add(targetCell.Left, targetCell.Top, targetCell.Width, targetCell.Height)
'Change plot settings to fill the cell
With chacha.Chart
.ChartType = xlBarStacked
.SetSourceData Source:=src, PlotBy:=xlRows
.Axes(xlValue).MinimumScale = 0
.Axes(xlValue).MaximumScale = 100
.Axes(xlCategory).Delete
.Axes(xlValue).Delete
.Legend.Delete
.PlotArea.Top = -50
.PlotArea.Left = -50
.PlotArea.Width = targetCell.Width
.PlotArea.Height = targetCell.Height
.ChartGroups(1).GapWidth = 0
End With
chacha.Chart.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
chacha.Chart.SeriesCollection(2).Format.Fill.ForeColor.RGB = RGB(0, 0, 255)
chacha.Chart.SeriesCollection(3).Format.Fill.ForeColor.RGB = RGB(255, 255, 0)
In the code I modified the series colors manually which can also be automatized. Following is the screenshot of both methods. The Cell "C1" is filled with block characters and "C2" is a chart.
Note: You might get an error at the line ".PlotArea.Top". To solve this issue, please check: Error setting PlotArea.Width in Excel, VBA (Excel 2010)
I am quite new to Excel and so far it has been fun learning it. I have started using Excel for modelling and i have gained enough expertise at it
I now wanted to go a step ahead and do a bit of designing on Excel
I have a specific requirement. I have 10 sheets in my workbook. I want to add some sort of animation on my first sheet. I currently have a button on my sheet 1.
On click of button it unhides all the 9 sheets and allows me to see modelling stuff in them. On clicking one more time it hides all the 9 sheets again.
Now i want to add one more thing to this.
On clicking the button i want a light bulb on the sheet to turn on and display a message (along with unhiding the sheets)
On clicking one more time i want the light bulb to turn off and hide the sheets again,
Can someone help me with this animation.
Thanks,
Sachi
Like I said it is very simple. This is how your Button and Bulb looks like.
Shapes used to create the bulb
Straight Connector
Oval
Cloud
Code
Private Sub CommandButton1_Click()
If CommandButton1.Caption = "Hide" Then
'
'~~> Your code here to Hide the Sheets
'
ActiveSheet.Shapes("Oval 2").Fill.ForeColor.RGB = RGB(255, 255, 255)
CommandButton1.Caption = "Unhide"
ElseIf CommandButton1.Caption = "Unhide" Then
'
'~~> Your code here to Unhide the Sheets
'
ActiveSheet.Shapes("Oval 2").Fill.ForeColor.RGB = RGB(255, 255, 0)
CommandButton1.Caption = "Hide"
End If
End Sub
To simply make the button change a shapes fill to yellow try this:
ActiveSheet.Shapes("SHAPE NAME").Fill.ForeColor.RGB = RGB(255, 255, 0)
The same idea can be applied to change the fill back. I've been playing with actual animation myself a lot lately too, so if you want to get something a bit more fun than the above than try something like the following. (Create a shape called Oval 1 to demo it with, or change the name to your shapes name)
Sub bulb()
steps = 300
timelimit = 0.005
increments = 255 / steps
counter = 0
r = 0
g = 0
Do
DoEvents
counter = counter + 1
r = r + increments
g = g + increments
ActiveSheet.Shapes("Oval 1").Fill.ForeColor.RGB = RGB(r, g, 0)
timeout (timelimit)
Loop Until counter = steps
End Sub
Sub timeout(duration_ms As Double)
Start_Time = Timer
Do
DoEvents
Loop Until (Timer - Start_Time) >= duration_ms
End Sub
Will need tweaking to your needs of course but the possibilities playing with this are endless.
I have a VBA loop that cycles through the selected cells of a PowerPoint table to update the formatting. The following lines work great:
With objTable.Rows(the_row).Cells(the_col).Shape.TextFrame.TextRange.Font
.Size = 12
.Color = RGB(0, 0, 102)
End With
With objTable.Rows(the_row).Cells(the_col).Shape.TextFrame
.VerticalAnchor = msoAnchorMiddle
End With
I'm having trouble finding the syntax to modify the number format (to change the number of decimals, add a comma, etc.) and to change the internal margin of the cell (which I can do manually with a right click -> Format Shape -> Text Box -> Internal Margin). Usually I use the record macro option to get to that detailed syntax, but I'm not seeing that option in PowerPoint.
With objTable.Rows(the_row).Cells(the_col).Shape.TextFrame
'Internal margin:
.MarginLeft = 'value goes here
.MarginRight = 'value goes here
.MarginTop = 'value goes here
.MarginBottom= 'value goes here
'number Format:
.TextRange.text = Format(1000, "#,##0.00") 'replace 1000 with your value
end with
I have seen some examples which uses an Arrow to Indicate Special Points on Excel chart like this. But i want to achieve this using VBA. For example if some point on chart is greater then 90 then it shows an arrow corresponding to that point.
Please suggest on how should I go about it in VBA. Any help would be appreciated.
Update
Apart from just changing the point color is there any other good suggestion to make that point more prominent. Update 2
Right now i am using this code.
For Each oCell In Range("e4:e" & LastRow) 'loop
If oCell.Value < sd13 Then 'rule 13s
Range("V4").Value = "Rule 13s voilated!"
Range("V4:w4").Interior.Color = RGB(255, 0, 0)
ActiveWorkbook.Sheets("LDL-C").ChartObjects("Chart 1047").Chart.SeriesCollection(1).Points(j).MarkerBackgroundColor = RGB(255, 0, 0)
End If
Next
Apart from just changing the point color is there any other good suggestion to make that point more prominent.
Would this help?
With ActiveChart
For i = 1 To .SeriesCollection.Count
a = .SeriesCollection(i).Values
For l = 1 To .SeriesCollection(i).Points.Count
If mymax < a(l) Then
mymax = a(l)
.SeriesCollection(i).DataLabels.Select
Selection.Format.Line.Visible = msoTrue
Selection.Format.Line.Visible = msoFalse
.SeriesCollection(i).Points(l).DataLabel.Select
.SeriesCollection(i).Points(l).Select
.SeriesCollection(i).DataLabels.Select
.SeriesCollection(i).Points(l).DataLabel.Select
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
End With
With Selection.Format.Line
.Visible = msoTrue
.Weight = 2
End With
End If
Next l
Next
End With
SNAPSHOT
Another snapshot
Not sure how to do it with an arrow but here is a way to just change the colour of the point of interest:
With ActiveSheet.ChartObjects(ChartName).Chart.SeriesCollection("NCDs")
For currentPoint = 1 To .Points.Count
If Range("G" & currentPoint + 34).Value = True Then
With .Points(currentPoint).Format
.Fill.BackColor.RGB = RGB(50, 150, 50)
.Fill.ForeColor.RGB = RGB(50, 150, 50)
.Line.ForeColor.RGB = RGB(50, 150, 50)
End With
Else
With .Points(currentPoint).Format
.Fill.BackColor.RGB = RGB(150, 50, 50)
.Fill.ForeColor.RGB = RGB(150, 50, 50)
.Line.ForeColor.RGB = RGB(150, 50, 50)
End With
End If
Next currentPoint
End With
Just change the names and the condition clause...
Also maybe the .Points(currentPoint) object has x,y location properties which you could use to position your arrow. Not sure about that though but it seems like a good starting point.
Yeah, gotta have VBA. Problem with VBA is that someone has to remember to run the procedure, or set up a Worksheet_Calculate event, or whatever, so when the data changes, which it inevitably does, the chart keeps up with the data.
So here's my non-VBA approach, which relies on Excel formulas.
Simple data, supplied by Siddharth in his answer. I've added a column, which I call MAX. The formula in cell C2, copied down to C11, is
=IF(B2=MAX(B$2:B$11),B2,NA())
The first chart plots the regular series of data in a clustered column chart. The second chart has MAX added to it. In the third chart I've changed the Overlap to 100%, so the blue bar covers the corresponding gray bar. Next chart I've added data labels to the MAX series. In the last chart I've formatted the data label to show series name, and the font color =matches the bar color.
So here is the original data and chart (upper) and changed data with changed chart (below). The formulas did it all, no need to somehow rerun the VBA.
What's cool is if I have a tie for first place, I get two labeled blue bars, with no extra effort.
Not a big stretch to add a third series to indicate the MIN.
ActiveChart.FullSeriesCollection(1).Select
With Selection
.MarkerStyle = 8
.MarkerSize = 5
End With
Selection.MarkerStyle = 2
ActiveChart.ChartArea.Select
With Selection.Format.Line