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

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")

Related

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

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.

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"

Repositioning Excel Charts as I add them into the same powerpoint slide

I am using Python3 and the win32com package to automatically copy charts from an excel file and paste them into a powerpoint presentation. The charts do not all fit on one slide unless they are resized, so I scale their width by 0.85. However, I am not sure how to paste them at specific locations so they are all pasted atop one another. I think I have found a solution in visual basic (here: https://www.mrexcel.com/board/threads/vba-positioning-chart-in-powerpoint-after-copy-pasting-from-excel.1107603/ and here: VBA: Copy + Paste Selected Charts from Excel to Powerpoint) but I don't know any VBA so am not sure how to translate it to python. Thank you for your help!
import os
import win32com.client as client
from win32com.client import constants
xl = client.gencache.EnsureDispatch('Excel.Application')
ppt = client.gencache.EnsureDispatch('PowerPoint.Application')
wb = xl.Workbooks.Open(os.path.abspath("desiredFile.xlsx"))
prs = ppt.Presentations.Open(os.path.abspath("sample00.pptx"))
sheet = wb.Sheets('TheSheetINeed')
Slide = prs.Slides.Add(prs.Slides.Count,constants.ppLayoutBlank)
for ch in sheet.ChartObjects():
ch.Activate()
ch.Copy()
Slide.Shapes.PasteSpecial(constants.ppPasteShape).ScaleWidth(.85,0)
#Here is where I need help
prs.Save()
prs.Close()
wb.Close(False)
ppt.Quit()
xl.Quit()
I don't know how Python3 declares variables or otherwise works, but I would try something like the following. Instead of
Slide.Shapes.PasteSpecial(constants.ppPasteShape).ScaleWidth(.85,0)
I would try
shp = Slide.Shapes.PasteSpecial(constants.ppPasteShape).ScaleWidth(.85,0)
shp.Left = LeftValue
shp.Top = TopValue
where shp is a variable that represents the pasted shape.

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"))

Setting VBA Excel Chart Legend Position Property in MS Access not working

This is a very strange issue I am facing for a while now, when creating some Excel worksheets programmatically from MS Access 2003.
Using this VBA-Code snippet I am not able to set the Position property of an Excel's Legend object (variable definitions and so on are left out to ease understanding).
...
Set ChartObject = myWorksheet.ChartObjects.Add(myRange.Left, myRange.Top, myRange.width, myRange.Height)
Set Chart = ChartObject.Chart
Chart.HasLegend = True
'This line raises an error:
Chart.Legend.Position = -4107 '=xlLegendPositionBottom
...
MS Access always raises the Error 1004:
"Unable to set the Position property of the Legend class"
It confuses me that I can use the exact same code from within Exel VBA and it just works. What confuses me even more is that the property HasLegend can be set whithout any error ocurring.
Someone has a hint to solve this issue?
After building a generic version of my code I found that in Office/Excel 2003 I have to populate my Series of data to the chart before changing the Legend's position. Probably the object hasn't been initiated before. My Code should therefore look like this:
...
Set ChartObject = myWorksheet.ChartObjects.Add(myRange.Left, myRange.Top, myRange.width, myRange.Height)
Set Chart = ChartObject.Chart
Chart.HasLegend = True
...
Chart.SeriesCollection.Add myRange, 1 '=xlRows
'Now this works:
Chart.Legend.Position = -4107 '=xlLegendPositionBottom

Resources