VBA problem with setting data source range

Akbarov

Active Member
Joined
Jun 30, 2018
Messages
347
Office Version
  1. 365
Platform
  1. Windows
Hello community,

I writed VBA code to update series of charts in worksheet. It chooses 1st chart, updates range (ranges are changing time to time) then loops untill last chart.
Previously it was working perfectly fine, then I made some changes and now it is giving strange results.

Here is the full code:
VBA Code:
Sub UpdateChart() 'Excel VBA procedure to update the chart.
Dim CHARTDATA As Range
Dim i As Integer
Dim j As Long
Dim x As Long
Dim lw As Long
Dim LR As Long
Dim sh As Worksheet
Dim ws As Worksheet
Dim lc As Long
Dim lastCell As Range
Dim cell1 As Range
Dim B68 As Integer

Set sh = ActiveSheet
B68 = sh.Range("B68").Value

    
    x = 0

'Number of charts
    j = 1

'Find last col
    Dim Rng As Range, ii As Long, jj As Long
    ii = 6
    Do While ii < (10 * B68)   '<< second number is number of ranges


        Set Rng = Cells(2, ii).Resize(, 10)
        For jj = 10 To 1 Step -1
            If Rng.Cells(jj) <> "" Then
               lc = Rng.Cells(jj).Column
                Exit For
                 Exit Do
            End If
            
        Next jj

  



'Find last non-blank series name
   ' Dim c
  '  For Each c In sh.Range(Cells(2, 6 + x), Cells(2, 6 + x).Offset(0, 9))
       ' If c = "" Then
       '  lc = c.column
        '    Exit For
     '   End If
   ' Next
 If lc = 0 Then lc = 9 + x
 
'Finding last row
With ActiveSheet.Range("E3").CurrentRegion
     LR = .Rows(.Rows.Count).Row
     LR = Range("E3:E" & LR).Find(vbNullString, , xlValues, xlWhole, xlByRows, xlNext).Row
 End With
'lr = 55

'Set range of data
If j = 1 Then
Set CHARTDATA = sh.Range(Cells(3, 6 + x).Address, Cells(LR, lc).Address)
Else
Set CHARTDATA = sh.Range(Cells(3, 6 + x).Address, Cells(LR, lc).Address)
End If

'Activate chart and make required changes
sh.ChartObjects("Cluster" & j).Activate
ActiveChart.ChartArea.Select
ActiveChart.SetSourceData Source:=CHARTDATA, PlotBy:=xlColumns
ActiveChart.ChartType = xlColumnClustered
ActiveChart.DisplayBlanksAs = xlNotPlotted
'ActiveChart.Axes(xlCategory).Select
'Selection.TickLabels.Orientation = 45
For i = 1 To ActiveChart.SeriesCollection.Count  'Headers to be added
Set cell1 = sh.Cells(2, 5 + i + x)

ActiveChart.SeriesCollection(i).Name = cell1.Value
Next i

ActiveChart.FullSeriesCollection(1).XValues = "='" & sh.Name & "'!" & "$E$3:$E$" & LR

If x > B68 * 10 Then
Exit Sub
End If
ii = ii + 10
x = x + 10
i = 0
j = j + 1
 Loop
End Sub

Code itself looks very complicated, but I guess problem is in CHARTDATA or SeriesCollection.
I am sharing expected result and sample workbook with hope someone can help me.

 

Attachments

  • expected_result.png
    expected_result.png
    62.4 KB · Views: 21

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top