VBA Error when creating chart

zarathustra

New Member
Joined
Jun 8, 2017
Messages
19
I always get an error message when my code reaches the sub CreateChart. Any ideas what goes wrong there? What am I missing?

Code:
Sub q()
Dim DataforPie As Range
Dim Lastrow As Integer
Dim LastColumn As Integer
Dim i As Long


On Error GoTo Help
    Application.Calculation = xlCalculationManual
    
    Application.ScreenUpdating = False
    
    Application.DisplayStatusBar = False
    
    Application.EnableEvents = False

Lastrow = Cells(Rows.Count, "A").End(xlUp).Row

LastColumn = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column

For i = 2 To 4
With Worksheets("Sheet1")
'Set DataforPie = Union(.Range("A1:" & LastColumn & Lastrow), .Range(.Cells(i, 1), .Cellls(i, Replace(Cells(1, LastColumn - 1).Address(False, False), "1", ""))))
Set DataforPie = Union(.Range("A1:G1"), _
.Range(.Cells(i, 1), .Cells(i, 6)))
End With

LPOS = 10
TPOS = 10
Call CreateChart(DataforPie, LPOS, TPOS, 180, 120)

Next i
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True

Help:
    
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    Application.DisplayStatusBar = True
    Application.EnableEvents = True
    MsgBox "Error:Something went wrong!", vbInformation, Important

End Sub

Sub CreateChart(rng, l, t, w, h)
With Worksheets("Sheet2").Shapes. _
AddChart2(332, xlLineMarkers, l, t, w, h).Chart
.SetSourceData Source:=rng
.Axes(xlValue).MinimumScale = 0
.Axes(xlValue).MaximumScale = 100
End With
End Sub
 
Last edited:

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

Forum statistics

Threads
1,214,646
Messages
6,120,715
Members
448,985
Latest member
chocbudda

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