Excel histogram bins adjustment with VBA doesn't seem to work - excel

I'm trying to automatically create couple histograms for given data. The problem is that I can't change bins width with VBA. How to get around this?
(Excel Version 2210 Build 16.0.15726.20188)
I looked through the docs and tried the macro recorder and this is prototype code I came up with.
Sub generateHistograms()
Dim wsTab As Worksheet
Set wsTab = Worksheets("Tabele")
With wsTab.Shapes.AddChart2(-1, xlHistogram)
.Name = "Test"
.Chart.SeriesCollection.NewSeries
.Chart.FullSeriesCollection(1).Values = wsTab.ListObjects(1).ListColumns(3).DataBodyRange
.Chart.ChartGroups(1).BinsType = xlBinsTypeManual
.Chart.ChartGroups(1).BinWidthValue = 10
.Chart.ChartGroups(1).BinsOverflowEnabled = True
.Chart.ChartGroups(1).BinsOverflowValue = 90
End With
End Sub
Running the code line by line seems like BinsType, BinWidthValue, BinsOverflowEnabled and BinsOverflowValue doesn't do anything.

Related

Excel VBA graph based on Series not working properly

I'm using the following VBA code in Excel in order to produce 2 graphs with a data picked up from the sheet and slightly elaborated. As I'm not using ranges directly for the graphs and need to use arrays for X and Y axes, I decided to go for Series. X has Date type and Y has Integer type. First it worked, but then the graphs started giving me wrong data, something random coming directly off the sheet. I suppose there could be something wrong in the way how the charts are declared. Here's the code, what could go wrong with it?
Dim crt1, crt2 As Chart
Dim sr1, sr2 As Series
ActiveSheet.Shapes.AddChart2(201, xlColumnClustered, 60, 50).Select
Set crt1 = ActiveChart
Set s1 = crt1.SeriesCollection.NewSeries()
sr1.Values = dataArray1
sr1.XValues = datesArray1
ActiveSheet.Shapes.AddChart2(332, xlLineMarkers).Select
Set crt2 = ActiveChart
Set s2 = crt2.SeriesCollection.NewSeries()
sr2.Values = dataArray2
sr2.XValues = datesArray2
This problem also persists when I comment the code for the second graph.
P.S. Excel says "Runtime error 424. Object required" underlining "sr1.Values = dataArray1" and then "sr1.XValues = datesArray1"

Macro fails when run but works when stepping through

I am working with a 2 axis chart showing volume with columns and YoY change with no visual representation only data labels. I have written a macro to uniformly move the YoY data labels above each column. The macro works perfectly when stepping through but fails when run.
Sub Move_Data_Point_1_chart()
Set chrt_obj_sig = ActiveSheet.ChartObjects("Volume & YoY")
Set chrt_sig = chrt_obj_sig.Chart
pnt_cnt = chrt_sig.SeriesCollection(1).Points.Count
chrt_sig.FullSeriesCollection(1).DataLabels.Position = xlLabelPositionInsideEnd
For i = 1 To pnt_cnt
chrt_sig_pt = chrt_sig.SeriesCollection(1).Points(i).DataLabel.Top
chrt_sig.SeriesCollection(2).Points(i).DataLabel.Top = chrt_sig_pt - 18
Next i
chrt_sig.FullSeriesCollection(1).DataLabels.Position = xlLabelPositionCenter
End Sub
Here is my expected output.
Correct Output
Here is the failed output.
Incorrect Output
Any suggestions for fixing this?
I have searched and found many similar occurrences but nothing I could apply to my specific situation.
Thanks in advance.

Add Multiple Series of Data To Excel Chart From VB.Net

Before saying this has been answered numerous times, don't please. I have looked at every link I could find. I still am having trouble figuring it out because most answers talk about VBA IN Excel not VB.Net in Visual Studio.
I am trying this:
Dim Charts As Excel.ChartObjects = CType(sheet.ChartObjects(Type.Missing), Excel.ChartObjects)
Dim myChart As Excel.ChartObject = CType(Charts.Add(10, pos * 20, 600, 400), Excel.ChartObject)
Dim chart As Excel.Chart = myChart.Chart
chart.ChartType = Excel.XlChartType.xlLineStacked
chart.SetSourceData(sheet.Range("B4:B10, E4:E10"))
chart.SetSourceData(sheet.Range("H4:H10, J4:J10"))
It plots 1 series perfectly, but I can't get the second series on there.
The chart displays like this:
I tried creating a Macro in Excel and then pasting/editing the code and I couldn't get that to work either. Working with VB.Net and Excel is all new to me. How can I plot multiple series to an Excel sheet from VB.Net? Thanks!!
UPDATE:
I was able to FINALLY figure this out. I wasn't creating a separate series individually. Now I feel like this was a really dumb question... Thanks everyone.
This is what I did to get it working.
Dim ds As Excel.Series
ds = chart.SeriesCollection.NewSeries()
ds.XValues = sheet.Range("B4:B6")
ds.Values = sheet.Range("E4:E6")

VBA - Using Average function in Sheets.Range

I've tried searching a few different threads but wasn't able to get a solution.
My macro is going to transfer data from one sheet to another. I need to use the AVERAGE function to get the average of a range (on the other sheet)
I'm getting a RUN TIME ERROR 438 error and can't figure out why. I tried using Application.WorksheetFunction but this doesn't bear the correct result and works on the MAX function but not the AVERAGE. I saw a few solutions that involve creating a loop and variables, but I assumed a simple solution should be possible.
Code below:
Option Explicit
Sub Step8CopytoLean()
Dim wblean As Workbook
Dim wbmaster As Workbook
Set wblean = Workbooks("SLA Reporting Lean.xlsx")
Set wbmaster = Workbooks("SLA Reporting MasterFile.xlsx")
Workbooks("Lean.xlsx").Activate
Worksheets("Data").Delete
Workbooks("MasterFile.xlsx").Activate
Worksheets("Data").Copy After:=Workbooks("Lean.xlsx").Sheets("Summary")
Workbooks("Lean.xlsx").Sheets("Summary").Activate
wblean.Sheets("Summary").Range("E4").Value = wbmaster.Sheets("Summary").Range("K20")
wblean.Sheets("Summary").Range("F4").Value = wbmaster.Sheets("Summary").Range("M20")
wblean.Sheets("Summary").Range("E5:E6").Value = wbmaster.Sheets("Summary").Average(Range("K9:K11")) 'line with error
wblean.Sheets("Summary").Range("F5").Value = wbmaster.Sheets("Summary").Max(Range("M8:M11")) 'line with error
Modify the below and try:
Application.WorksheetFunction.Average(ThisWorkbook.Worksheets("Sheet1").Range("A1:A10"))
Something like this:
wblean.Sheets("Summary").Range("E5:E6").Value = Application.WorksheetFunction.Average(wbmaster.Worksheets("Summary").Range("K9:K11"))
wblean.Sheets("Summary").Range("F5").Value = Application.WorksheetFunction.Max(wbmaster.Worksheets("Summary").Range("M8:M11"))

Accessing PivotChart SeriesCollection with a Sub

I am quite new to VBA. I have written a macro which creates about 10 pivot charts and some normal charts after filtering and cutting some data from a database spreadsheet. I now want to write a sub which goes through applying the same formatting to each one. The sub is as follows:
Sub FormatChart(Cht As ChartObject, title As String)
Cht.Activate
MsgBox ActiveChart.SeriesCollection.Count
With ActiveChart
.HasTitle = True
.ChartTitle.Text = title
.Axes(xlValue).HasMajorGridlines = False
End With
ActiveChart.SeriesCollection(1).Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 182, 0)
End With
End Sub
I originally didn't include all the activates and selects, but couldn't get the macro to work without them and don't see it as the end of the world - the datasets are never going to be massive so speed isn't so much of a concern, and I disable screenupdating so that users can't click on cells/other objects and disrupt the macro as it runs.
Here's my problem. If I take out the second With loop, everything proceeds perfectly and the gridlines are removed and the title is added. However, whenever I try to edit the colours of columns with the above I get Run time error '1004': Invalid parameter. I've also tried keeping the content of the second with loop inside the first but then moved it out to try using selection to see if it made a difference.
I've fiddled around quite a bit and recorded various macros changing the colour of the chart in question, but I think the problem might be to do with referencing SeriesCollection as when I try to debug with
MsgBox ActiveChart.SeriesCollection.Count
I get "0".
Please let me know if I'm missing the point - as I said I am new to VBA and am trying to learn as much as possible :)
EDIT: The solution was that I was passing each chart to the above sub after I had created the chart, but before I had set a data source for the chart. Doh!
This obviously meant that there were no seriesCollections for that chart, hence the error I was getting.
I marked Joehannah as answering the question (even though it isn't technically the solution) because it made me check my code and notice that the above could be causing the problem - if I shouldn't do that someone please tell me and I'll try to fix it!
You are better off using Set cht = (create chart object) for each chart and then Immediately calling the format method passing in cht.
I have been told to post my own answer since I figured out the issue.
I was passing each chart to this function just after creating it, as follows:
Set ptr1 = Sheets("withinBRSPivots").PivotTables("UniqueClicks").TableRange1
Set cht1 = Sheets("BRS Overview").ChartObjects.Add(Left:=950, Top:=500, _
Width:=400, Height:=300)
cht1.Name = "UCChart"
Charter.FormatChart cht1, "Average Number of Unqique Clicks"
I then set the source data for the chart after doing the above. This meant that when the chart was passed to my chartformat sub, it had no source data. This is what resulted in being able to edit the title and gridlines, but not the seriesCollection. The fix was to just move the FormatChart sub line to after I had set the source data.
Many thanks to everyone who posted answers!

Resources