chriscorpion786
Board Regular
- Joined
- Apr 3, 2011
- Messages
- 112
- Office Version
- 365
- Platform
- Windows
Hi All,
I have a macro that creates multiple charts from one sheet and places them on another sheet and realigns all in a grid manner.
The problem is that if I run the code from within the VB editor, the code runs perfectly, but if I run the code from the sheet using a button, it doesn't work. There are around 20 plus charts.
Is there a problem ??? I cant seem to understand that it runs fine from within the VB editor, then it should run fine as well from the sheet too....am i missing something here...
Appreciate your help...I'm running the code from sheet 2 using a button....sheet2 is Activesheet below
Below is the code
Sub CreateMultipleCharts()
Dim ChtObj As ChartObject
Dim ChtRng As Range
Dim x As Long
Dim lastrow As Long
Const rowstall As Long = 8
Const colswide As Long = 5
Const chtsperrow As Long = 4
Const skiprows As Long = 2
Const skipcols As Long = 1
Dim chtwidth As Double
Dim chtheight As Double
Dim chtleft As Double
Dim chttop As Double
Dim rowsbetweenchts As Double
Dim colsbetweenchts As Double
lastrow = Cells(Rows.Count, 2).End(xlUp).Row
On Error Resume Next
ActiveSheet.ChartObjects.Delete
Sheet3.ChartObjects.Delete
For x = 2 To lastrow
Set ChtRng = Union(Range("B1:N1"), Range("B" & x & ":N" & x))
ActiveSheet.ChartObjects.Add(20, 20, 300, 300).Select
With ActiveChart
.SetSourceData ChtRng
.ChartType = xlLineMarkers
.FullSeriesCollection(1).ApplyDataLabels
.FullSeriesCollection(1).DataLabels.Position = xlLabelPositionAbove
.FullSeriesCollection(1).DataLabels.NumberFormat = "#,###,"
.Axes(xlValue).MajorGridlines.Delete
.Axes(xlValue).Delete
.Legend.Delete
.ChartTitle.Format.TextFrame2.TextRange.Font.Size = 8
End With
Next x
Call MoveAllCharts
With ActiveSheet.Range("A2")
chttop = .Top
chtleft = .Left
chtwidth = colswide * .Width
chtheight = rowstall * .Height
rowsbetweenchts = skiprows * .Height
colsbetweenchts = skipcols * .Width
End With
'Reset X to Zero
x = 0
For x = 1 To ActiveSheet.ChartObjects.Count
Set ChtObj = ActiveSheet.ChartObjects(x)
With ChtObj
.Left = ((x - 1) Mod chtsperrow) * (chtwidth + colsbetweenchts) + chtleft
.Top = Int((x - 1) / chtsperrow) * (chtheight + rowsbetweenchts) + chttop
.Width = chtwidth
.Height = chtheight
End With
Next x
Range("A2").Select
End Sub
I have a macro that creates multiple charts from one sheet and places them on another sheet and realigns all in a grid manner.
The problem is that if I run the code from within the VB editor, the code runs perfectly, but if I run the code from the sheet using a button, it doesn't work. There are around 20 plus charts.
Is there a problem ??? I cant seem to understand that it runs fine from within the VB editor, then it should run fine as well from the sheet too....am i missing something here...
Appreciate your help...I'm running the code from sheet 2 using a button....sheet2 is Activesheet below
Below is the code
Sub CreateMultipleCharts()
Dim ChtObj As ChartObject
Dim ChtRng As Range
Dim x As Long
Dim lastrow As Long
Const rowstall As Long = 8
Const colswide As Long = 5
Const chtsperrow As Long = 4
Const skiprows As Long = 2
Const skipcols As Long = 1
Dim chtwidth As Double
Dim chtheight As Double
Dim chtleft As Double
Dim chttop As Double
Dim rowsbetweenchts As Double
Dim colsbetweenchts As Double
lastrow = Cells(Rows.Count, 2).End(xlUp).Row
On Error Resume Next
ActiveSheet.ChartObjects.Delete
Sheet3.ChartObjects.Delete
For x = 2 To lastrow
Set ChtRng = Union(Range("B1:N1"), Range("B" & x & ":N" & x))
ActiveSheet.ChartObjects.Add(20, 20, 300, 300).Select
With ActiveChart
.SetSourceData ChtRng
.ChartType = xlLineMarkers
.FullSeriesCollection(1).ApplyDataLabels
.FullSeriesCollection(1).DataLabels.Position = xlLabelPositionAbove
.FullSeriesCollection(1).DataLabels.NumberFormat = "#,###,"
.Axes(xlValue).MajorGridlines.Delete
.Axes(xlValue).Delete
.Legend.Delete
.ChartTitle.Format.TextFrame2.TextRange.Font.Size = 8
End With
Next x
Call MoveAllCharts
With ActiveSheet.Range("A2")
chttop = .Top
chtleft = .Left
chtwidth = colswide * .Width
chtheight = rowstall * .Height
rowsbetweenchts = skiprows * .Height
colsbetweenchts = skipcols * .Width
End With
'Reset X to Zero
x = 0
For x = 1 To ActiveSheet.ChartObjects.Count
Set ChtObj = ActiveSheet.ChartObjects(x)
With ChtObj
.Left = ((x - 1) Mod chtsperrow) * (chtwidth + colsbetweenchts) + chtleft
.Top = Int((x - 1) / chtsperrow) * (chtheight + rowsbetweenchts) + chttop
.Width = chtwidth
.Height = chtheight
End With
Next x
Range("A2").Select
End Sub