Excel VBA MarkerStyle - vba

I am trying to change series marker style into crosses X.
I have created test script in new excel file with:
ActiveChart.SeriesCollection(1).Select
Selection.MarkerStyle = xlMarkerStyleX
and it works fine. I have:
Worksheets("RecordID Chart").ChartObjects(1).Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(graph + 2).Name = lo.ListColumns("RecordID").DataBodyRange.Rows(1)
ActiveChart.SeriesCollection(graph + 2).XValues = tesPercentage
ActiveChart.SeriesCollection(graph + 2).Values = testError
ActiveChart.SeriesCollection(graph + 2).MarkerStyle = xlMarkerStyleX
ActiveChart.SeriesCollection(graph + 2).MarkerSize = 5
ActiveChart.SeriesCollection(graph + 2).Format.Line.ForeColor.RGB = myCol & graph
ActiveChart.SeriesCollection(graph + 2).Select
Selection.Format.Line.Visible = msoFalse
Does anyone have an idea?
Actually, it is cross, but it is not displayed as a cross:
So I believe it has some problems with Marker Fill or Marker Line Color... I am still confused.

Assume this will serve the purpose:
ActiveChart.SeriesCollection(1).Select
With Selection
.MarkerStyle = xlMarkerStyleX
.MarkerSize = 7
.MarkerBackgroundColorIndex = xlColorIndexNone
.MarkerForegroundColorIndex = xlColorIndexAutomatic
End With

It looks like your marker fill color is the same as the marker foreground color. You'll need to change it.

Related

How to draw and format a line?

I am trying to draw a line in a PowerPoint presentation. I cannot get the desired combinations of colour, weight, zorder and to name it.
I have two ways to draw a line.
The first:
Set oLine = MyDocument.Shapes.AddLine(MyShape.Left + MyShape.Width, MyShape.Top + (MyShape.Height * 0.5) - 5, MyDocument.Shapes(lineName).Left, MyDocument.Shapes(lineName).Top + (0.5 * MyDocument.Shapes(lineName).Height)).Line
With oLine
.ZOrder (msoSendToBack)
.Line.Weight = 7
.Fill.ForeColor.RGB = RGB(0, 0, 0)
.Name = "Line"
End With
With this only the zorder works. The colour of the line is a shade of blue (which I believe was used earlier in the PowerPoint).
The second:
With MyDocument.Shapes.AddLine(MyShape.Left + MyShape.Width, MyShape.Top + (MyShape.Height * 0.5), MyDocument.Shapes(lineName).Left, MyDocument.Shapes(lineName).Top + (0.5 * MyDocument.Shapes(lineName).Height)).Line
.ForeColor.RGB = RGB(0, 0, 0)
.Weight = 5
End With
This gives the desired colour and weight, but I cannot find a way to zorder the shape nor name it.
How can I achieve all of my desired combinations?
Lines don't use a fill, only 2D shapes.
It's easier to find errors if you break long commands (like the one starting with Set oLine) into smaller chunks until you get it working. You omitted information about the MyShape from which you're getting positions, but here is a revised version of your code that runs:
Sub FormatLine()
Dim oLine As Shape
Set oLine = ActivePresentation.Slides(1).Shapes.AddLine(BeginX:=10, BeginY:=10, EndX:=40, EndY:=40)
With oLine
.ZOrder (msoSendToBack)
.Line.Weight = 7
.Line.ForeColor.RGB = RGB(0, 0, 0)
.Name = "Line3"
End With
End Sub

How to determine the .Left value of a control?

I'm building my first user interface in VBA on Microsoft Access.
I am trying to get the .Left variable to show up in the drop down selection (library?).
The only thing that pops up is LeftPadding, which I'm pretty sure that isn't what I need. Why am I not able to declare the Left position of the rectangles?
Is there another type of variable that I should be using to declare the position of rectangles?
My follow up issue, if I'm doing that correctly, is about a nested If statement. I'm trying to calculate whether a newly visible rectangle's position + its dimensions exceeds the Left position of an already visible rectangle, and if so, position it elsewhere.
Dim ctl As Control
For Each ctl In [Forms]![frmBuilder]
If Left(ctl.Name, 3) = "box" And Box1.Visible = True Then
If ctl.Visible = True Then
NextCaseNum = Int(Right(ctl.Name, (Len(ctl.Name)) - 3) + 1)
NextCasePosition = (ctl.lef + ctl.Width) + 1440 / 60
NextCaseName = "box" & NextCaseNum
Else
CurCaseLeft = ctl.Left
CurCaseWidth = ctl.Width
CurCaseHeight = ctl.Height
With ctl
.Top = UprightBottom - HInch
.Left = NextCasePosition
.Width = WInch
.Height = HInch
.Visible = True
End With
If CurCaseLeft + CurCaseWidth > Upright2.Left Then
With Beam1
.Top = (((5.5 + 6) * 60) + Box1.Top) / 1440
.Left = Upright1.Left
.Height = (5.5 * 60) / 1440
.Width = ((4 * 60) / 1440) + Upright2.Left - Upright1.Left
.Visible = True
End With
End If
I think the problem lies with CurCaseLeft and CurCaseWidth, because I don't know how to define them in the function due to the current box's ctl.Left not showing up.
Do I have to separate the nested If statement in to a different function and call that function from the current function?
Try to be more explicit:
Dim ctl As Control
Dim rct As Rectangle
For Each ctl In [Forms]![frmBuilder]
If Left(ctl.Name, 3) = "box" And Box1.Visible = True Then
If ctl.Visible = True Then
Set rct = ctl
NextCaseNum = Int(Right(rct.Name, (Len(rct.Name)) - 3) + 1)
NextCasePosition = (rct.Left + rct.Width) + 1440 / 60

How continue normal line in dashed line chart (same line) in vb.net?

I want my normal line to continue in dashed line in char. I have chart from above:
Green line i want to continue on 2017/18 to be green and dashed but continue from the same line.
My code for this is that:
Dim OpenApu As New Series
Dim ClosedApu As New Series
Dim TargetWorkingAverage As New Series
OpenApu.Name = "Sum of APU units open"
ClosedApu.Name = "Sum of APU units closed"
TargetWorkingAverage.Name = "Target Working Average"
OpenApu.ChartType = SeriesChartType.Line
ClosedApu.ChartType = SeriesChartType.Line
TargetWorkingAverage.ChartType = SeriesChartType.Line
OpenApu.Color = System.Drawing.Color.Green
OpenApu.BorderWidth = 3
ClosedApu.Color = System.Drawing.Color.Blue
ClosedApu.BorderWidth = 3
TargetWorkingAverage.Color = System.Drawing.Color.Black
TargetWorkingAverage.BorderWidth = 3
TargetWorkingAverage.BorderDashStyle = ChartDashStyle.Dash
TargetWorkingAverage.ChartType = SeriesChartType.StepLine
' this i put my values from chart
ClosedApu.Points.AddXY(DateTime.Now.Year & "/" & i + 1, TotalCost2)
What i try, at final line to change style of line in dashed but change style for all line.
OpenApu.Color = System.Drawing.Color.Blue
OpenApu.BorderWidth = 3
OpenApu.BorderDashStyle = ChartDashStyle.Dash
OpenApu.ChartType = SeriesChartType.StepLine
ClosedApu.Color = System.Drawing.Color.Green
ClosedApu.BorderWidth = 3
ClosedApu.BorderDashStyle = ChartDashStyle.Dash
ClosedApu.ChartType = SeriesChartType.StepLine
OpenApu.Points.AddXY(DateTime.Now.Year & "/" & i + 1, 5000)
ClosedApu.Points.AddXY(DateTime.Now.Year & "/" & i + 1, 6000)
When try to change style in dahsed doesn't work, all my line is dashed but i want to continue un dahsed not all my line to be dashed.

Dynamic checkbox events through commandbutton

I am currently programming a sheet which visualizes data sets in graphs. Because the user of this sheet will not need all the graphs, I would like to let them choose the ones needed through a UserForm. Since the amount of data sets is variable, the UserForm will have the same amount of checkboxes as there are datasets.
The Userform code is as follows.
Private Sub UserForm_Initialize()
Dim chkBoxA As MSForms.CheckBox
Dim chkBoxB As MSForms.CheckBox
Dim lblBox As MSForms.Label
Dim cnt As Control
Amount = Sheet4.Range("C4").Value 'Amount of datasets
For i = 1 To Amount
Set lblBox = Me.Controls.Add("Forms.label.1", "Label" & i)
lblBox.Caption = "Set" & i
lblBox.Left = 5
lblBox.Top = 8 + ((i - 1) * 40)
Set chkBoxA = Me.Controls.Add("Forms.CheckBox.1", "A" & i)
chkBoxA.Caption = "Graph a"
chkBoxA.Left = 55
chkBoxA.Top = 5 + ((i - 1) * 40)
Set chkBoxB = Me.Controls.Add("Forms.CheckBox.1", "B" & i)
chkBoxB.Caption = "Graph b"
chkBoxB.Left = 55
chkBoxB.Top = 20 + ((i - 1) * 40)
Next
CommandButton1.Left = 20
CommandButton1.Top = 40 + ((Amount - 1) * 40)
CommandButton1.TabIndex = Amount * 3 + 1
Me.Height = 220
Me.ScrollBars = fmScrollBarsVertical
Me.ScrollWidth = Me.InsideWidth * 9
For Each cnt In Me.Controls
If cnt.Top + cnt.Height > Me.ScrollHeight Then
Me.ScrollHeight = cnt.Top + cnt.Height + 5
End If
Next
End Sub
When the UserForm is filled in (graphs are chosen by clicking on the options), the user will press CommandButton1. An event should then be run to show the correct graph, but for the simplicity I am first testing if a MsgBox will show up. Unfortunately the MsgBox does not show up.
Private Sub CommandButton1_Click()
'Will fix this with a loop
If A1 = True Then
MsgBox ("TestA1")
End If
If B1 = True then
MsgBox ("TestB1")
End If
If A2 = True then
MsgBox ("TestA2")
End If
Unload Me
End Sub
I am stuck on this part. The checkboxes do show up on the UserForm and they are clickable, but the commandbutton only shuts down the sub (Unload Me). I would like to see the MsgBox show up when I select the corresponding option and click the commandbutton. Any help on getting this to work is appreciated!
You are referencing 'A1' in the sub, but that variable does not exitst at compile time, because you add them dynamically. What you need to do is loop the controls, to check the names. Best practice is to put the checkboxes in a frame, to be able to group them.
Add a frame to the userform and name it 'checkboxframe'
And then instead of:
For i = 1 To Amount
Set lblBox = Me.Controls.Add("Forms.label.1", "Label" & i)
lblBox.Caption = "Set" & i
lblBox.Left = 5
lblBox.Top = 8 + ((i - 1) * 40)
Set chkBoxA = Me.Controls.Add("Forms.CheckBox.1", "A" & i)
chkBoxA.Caption = "Graph a"
chkBoxA.Left = 55
chkBoxA.Top = 5 + ((i - 1) * 40)
Set chkBoxB = Me.Controls.Add("Forms.CheckBox.1", "B" & i)
chkBoxB.Caption = "Graph b"
chkBoxB.Left = 55
chkBoxB.Top = 20 + ((i - 1) * 40)
Next
you would need to do:
With Me.checkboxframe
For i = 1 To Amount
Set lblBox = .Controls.Add("Forms.label.1", "Label" & i)
lblBox.Caption = "Set" & i
lblBox.Left = 5
lblBox.Top = 8 + ((i - 1) * 40)
Set chkBoxA = .Controls.Add("Forms.CheckBox.1", "A" & i)
chkBoxA.Caption = "Graph a"
chkBoxA.Left = 55
chkBoxA.Top = 5 + ((i - 1) * 40)
Set chkBoxB = .Controls.Add("Forms.CheckBox.1", "B" & i)
chkBoxB.Caption = "Graph b"
chkBoxB.Left = 55
chkBoxB.Top = 20 + ((i - 1) * 40)
Next
End With
And to add the checkboxes to the frame, use something like:
For Each ctr In UserForm1.frame("checkboxframe").Controls
If TypeName(ctr) = "CheckBox" Then
If ctr.Value = True Then
'do something usefull here
msgbox ctr.name
End If
End If
Next ctr
The reason nothing appears is because there is no object "A1" manually defined as a variable.
To get the value of the box you Dynamically named "A1" you would have to refer to it as such:
If Me.Controls.Item("A1").Value = True then
Hope this helps!

Errorbars formatting in Excel 2007

Using a macro in excel 2007 I want to display the following errorbars:
No horizontal errorbar.
Red dashed with 100 plus value vertical errorbar.
I can get everything I want except the color and I don't understand why. Below is the code.
ActiveChart.SeriesCollection(6).HasErrorBars = True
With ActiveChart.SeriesCollection(6).ErrorBars
.EndStyle = xlNoCap
.Format.Line.Visible = msoTrue
.Format.Line.ForeColor.RGB = RGB(255, 0, 0)
.Format.Line.ForeColor.TintAndShade = 0
.Format.Line.Weight = 2
.Format.Line.DashStyle = msoLineDash
End With
ActiveChart.SeriesCollection(6).ErrorBar Direction:=xlX, Include:=xlNone, Type:=xlFixedValue, Amount:=0
ActiveChart.SeriesCollection(6).ErrorBar Direction:=xlY, Include:=xlPlusValues, Type:=xlFixedValue, Amount:=100
I ran into the same issue too. But after toggling the visible state of the error bars, the color change worked for me. Give this a try:
ActiveChart.SeriesCollection(6).HasErrorBars = True
With ActiveChart.SeriesCollection(6).ErrorBars
.EndStyle = xlNoCap
.Format.Line.Visible = msoTrue
.Format.Line.ForeColor.RGB = RGB(255, 0, 0)
.Format.Line.Visible = False 'ADDED
.Format.Line.Visible = True 'ADDED
.Format.Line.ForeColor.RGB = RGB(255, 0, 0) 'ADDED
.Format.Line.ForeColor.TintAndShade = 0
.Format.Line.Weight = 2
.Format.Line.DashStyle = msoLineDash
End With