I am trying to create a bar chart using data in separate columns, and am using a union of the two ranges (one range is titles, other is data) to create my graph. However, I am receiving multiple "Object variable or with block not set" error messages .
I believe this is due to an error in reading my union, as the function can create a graph using only one data set with no issues Here is what I have.
Sub Barchart()
Dim ws As Worksheet
Set ws = Worksheets("AvgValues")
Dim bchart
Dim rng As Range
rng = Union(ws.Range("D4:D10"), ws.Range("B5:B10"))
If WorksheetFunction.CountA(Range("D5:D10")) <> 0 Then
On Error Resume Next
ws.ChartObjects.Delete
Set bchart = ws.ChartObjects.Add(Left:=450, Width:=350, Top:=10, Height:=270)
bchart.Chart.SetSourceData Source:=rng
bchart.Chart.ChartType = xlBar
bchart.Chart.ApplyDataLabels
Else
MsgBox ("Chart requires at least one element to be built")
End If
End Sub
Uncommenting the 'On error resume' message creates a blank graph. I assume due to the inability to read the unioned data properly.
Any help or clarification will be appreciated!