Excel VBA Macros xlDays - vba

I'm working on Line with markers chart. I have recorded a macro to create line chart (exactly xlLineMarkers). particularly x-axis with 7 days gap.
but it gives runtime error on
ActiveChart.Axes(xlCategory).MajorUnitScale = xlDays
I'm new to Excel VBA, please can anyone help?

apparently that doesn't work for my problem
i want to pick max and min date from datasheet and then display on x axis b y 1 week difference. can anybody help please ( in a deadlock :( )
[coding][1]
line chart
the code runs error free but doesnt show anything on x axis

Related

VBA code, Error 1004

Apollogies, I know this has been posted hundreds of times but Ive looked at them and still cant solve this
So I have a workbook (first time using vba)
first sheet is a graph
second sheet is a sheet containing values of nodes and stress
Third sheet is a sheet containing values of nodes and stress
Etc up to 17th sheet
I have a graph plot of stress vs number in sheet 1, and it contains only data from sheet 2 "stress1" and sheet 3 "stress2"
trying to make code to add the values of stress3 to my graph in sheet 1
CODE
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(x).Name = "=""Stress3"""
ActiveChart.SeriesCollection(x).Values = ThisWorkbook.Sheets("Stress3").Range("B9:B782")
Everything but the last line works, throws an error 1004 which I assume is because I have to do something to allow access to other sheets?
I have also tried
ActiveChart.SeriesCollection(x).Values = "=Stressx!$B$9:$B$782"
as the last line, same error
SOLVED
I am an idiot for putting x=3 at the top of my code, getting it working then not paying attention and removing it
the issue is the (x)
when I removed x=3 the sheets try to grab values from sheet x
which it doesnt know what x is
fixed by readding x=3 or changing the (x) to a number

Trend line in line chart next to input line chart

I cannot figure out why a trend is drawn next to input serie chart. The trend curve should be plotted through the existing data (first serie) and extend 1 month ahead. Instead, it starts where the input serie ends and continues (see the orange dashed line):
The code I use for creating the trend line is following
' do stuff for series (0) and (1)....
' databind before creating trend curve........
Me.Chart4.DataBind()
' trend ----------------------
Me.Chart4.Series.Add("TrendCurve")
Me.Chart4.Series("TrendCurve").ChartType = DataVisualization.Charting.SeriesChartType.Spline
Me.Chart4.Series("TrendCurve").BorderWidth = 3
Me.Chart4.Series("TrendCurve").BorderDashStyle = DataVisualization.Charting.ChartDashStyle.Dash
Me.Chart4.Series("TrendCurve").IsVisibleInLegend = False
' ************* calculate trend line **************
Me.Chart4.DataManipulator.FinancialFormula(
DataVisualization.Charting.FinancialFormula.Forecasting,
"Exponential,1,false,false",
Me.Chart4.Series("Performance"),
Me.Chart4.Series("TrendCurve"))
Also, the trend "curve" appears as line, although I selected "exponential" and I'd expect it to be slightly curved...
EDIT: I tried to fill the first dataset point by point instead by a dataset (because examples show it this way), but as I expected, it had no effect.
I new it had to be something simple, but... It seems that the only reason for the problem was, that the data were not explicitly ORDERED (even they apeared ordered). After I explicitly added order clause into SQL query:
ORDER BY StatDate
...the trend line shows correctly.
So after few months of searching I can close this question.

Excel VBA - a macro to 'Format Data Series - Gap Width' in a chart?

I have to change the design of about 100 charts in Excel 2011, and I'm trying to speed things up a little with macros.
The problem is that Excel doesn't want to simply record some actions into a macro, it seems they need to be manually written.
I've managed to make a macro for changing the formatting of Data Labels using tips from this thread:
Formatting data labels in Excel charts using VBA
But now I'd like to also edit Label Series- Gap Width percentage, through a macro. I don't know the exact VBA syntax for this action. Maybe someone here can help.
I've tried
ActiveChart.SeriesCollection(1).DataSeries.GapWidth = "110%"
But it didn't work. Run-time error 438, Object doesn't support property or method.
Does anyone know the correct syntax?
You can try the following code:
ActiveChart.ChartGroups(1).GapWidth = 110

Color Points (Bars) in Pivot Chart based on Row Labels (Axis Fields)

I'm trying to automate a process that so far I have been doing manually in Excel 2010. I create Pivot Charts often. One of the series on these charts is displayed as bars. I change the fill color of each bar based on one of the row labels of the pivot chart. For instance, if the row label = "GEO", I change the fill color of the bar to green.
I'm sure that it's possible to automate this process through VBA. Here's my code so far. When I run this macro, it stops at the first line of the If statement and gives this error. Compile error: Expected array. Can anyone give me some advice as to how to make this code work?
Sub By_Rig_PC_Coloring()
For i = 1 To ActiveChart.SeriesCollection(2).Points.Count
ActiveChart.SeriesCollection(2).Points(i).Select
If xlRowField("MFR") = "GEO" Then
Selection.Format.Fill.Forcolor.RGB = RGB(0, 176, 80)
End If
Next i
End Sub
As far as I know, you cannot access X axis labels associated with current Point. But this is a PIVOT chart, so you can use your pivot table to get the info you need.
Points are DataField's PivotItems, Series are ColumnFields and X labels are RowFields.
So SeriesCollection(2).Points(10) will be pvtYourPivotTable.ColumnFields(2).DataRange.Cells(10) (assuming that you have only one DataField, otherwise DataRange will be multi-column, and you'll have to adjust for that).
So once you have a cell in pivot table assosacietd with Point, the label will be located at Intersect(pvtYourPivotTable.RowFields("MFR").DataRange, pvtYourPivotTable.ColumnFields(2).DataRange.Cells(10).EntireRow. You can also use Offset or other method.
Here is another method of colouring chart bars based on X labels: Peltier Tech Blog without VBA.
Hope this helps.

Changing Line WIdth of Line charts and Max. scale for xlCategory

I created a line chart in Excel 2010. Dim co(1 To 90) As ChartObject.
How can I change its Line Width?
I tried using:
co(3).Chart.Series["Quality"].BorderWidth = 1
Error occurred: Series is not a part of Chart objects And
Is there any workaround for implementing Max. Scale for xlCategory?
You can use..
co(3).Chart.Series["Quality"].SeriesCollection(1).Border.Weight = 4
this case the macro recorder is very helpful, do the turn you have to do and then see the code
[]´s