Chart Location in excel vba - sql

how to set chart location i have below code
Dim rng As Range
Dim cht As Object
'Your data range for the chart
Set rng = ActiveSheet.Range("C10:Q12")
'Create a chart
Set cht = ActiveSheet.Shapes.AddChart2
'Give chart some data
cht.Chart.SetSourceData Source:=rng
'Determine the chart type
cht.Chart.ChartType = xlLineMarkers
'Change chart's title
cht.Chart.ChartTitle.Text = "Budget vs Actual (Income)"

You mean, location of chart on the worksheet?
First, you're better off using this to declarecht:
Dim cht As ChartObject
Then to locate cht, you can use (for example):
cht.Left = 150
cht.Top = 100
or tie cht to a cell's location:
cht.Left = ActiveSheet.Range("D4").Left
cht.Top = ActiveSheet.Range("D4").Top

Related

Repositioning Chart Object VBA

I am trying to create a chart and reposition it afterwards to the correct location on my PowerPoint sheet. But receive a runtime error. The relevant bits of code are:
(My chart data is not in the code)
Dim myChart As chart
Dim gChartData As ChartData
Dim gWorkBook As Excel.Workbook
Dim gWorkSheet As Excel.Worksheet
'Create the chart and set a reference to the chart data.
Set myChart = ActivePresentation.Slides(2).Shapes.AddChart.chart
Set gChartData = myChart.ChartData
'Set the Workbook and Worksheet references.
Set gWorkBook = gChartData.Workbook
Set gWorkSheet = gWorkBook.Worksheets(1)
With myChart
.ChartType = xlColumnStacked
.ChartStyle = 30
.ApplyLayout 4
.ClearToMatchStyle
End With
With myChart
.PlotArea.Left = 290
.PlotArea.Top = 90
End With
I receive the following error code:
Error: -2147467259 (80004005) during Runtime:
Method Left of object PlotArea failed
Some searching already suggested I may have a macro security problem but in the options all macro actions are currently allowed.
If you need further information please let me know.
Your code is moving the chart within the shape that it's been added to, rather than moving the container shape itself.
You need to move the parent of myChart:
Sub Test()
Dim myChart As Chart
Set myChart = ActivePresentation.Slides(2).Shapes.AddChart.Chart
With myChart
.ChartType = xlColumnStacked
.ChartStyle = 30
.ApplyLayout 4
.ClearToMatchStyle
'Move shape containing chart on PPT.
With .Parent
.Left = 290
.Top = 90
End With
'Move chart plot area within chart (I think).
.PlotArea.Left = 290
.PlotArea.Top = 90
End With
End Sub
I didn't get the error you have, but that could be trying to move the PlotArea outside the shape?

Using charts with VBA

I'm trying to generate two charts using VBA. The problem is most examples use ActiveChart but I want multiple charts on multiple sheets. If I inserted a blank chart how do I rename that chart to reference it. I don't want a new chart to be generated each time I run the macro and I want it to be in the sheet. I'm struggling with the code but am assuming it will be something like the code below. I've attached the desired graph (I made this through excel, but I need to do it through VBA).
macro1()
lastrow2 = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
dim chart1 as chart
dim chart2 as chart ' ect
chart1.title = "test"
chart1.xaxis = sheet1.cell(lastrow2,1)
chart1.yaxis = "manhours"
end sub
using a the record function, i got the code commented below. I tried to change it but i'm still having issues
Sub Macro7()
Dim Chart2 As ChartObject
Dim chartb As Chart
Chart2 = Sheet1.chartb.SeriesCollection(2)
chartb.Select
Formula = "=SERIES(Master!R3C3,Master!R4C1:R18C1,Master!R4C3:R19C3,2)"
' ActiveChart.SeriesCollection(2).Select
' Selection.Formula =_
'"=SERIES(Master!R3C3,Master!R4C1:R18C1,Master!R4C3:R19C3,2)"
End Sub
I really just need this formula converted to i can reference my lastrow function and individual sheets
ActiveChart.SeriesCollection(2).Select
Selection.Formula =_
"SERIES(sheet1.cells(3,3),sheet1.cells(4,1):sheet1.cells(18,1)_
,sheet1.cells(4,3):sheet1.cells(4,19),2"
' Selection.Formula"_
' =SERIES(Master!R3C3,Master!R4C1:R18C1,Master!R4C3:R19C3,2)"
this was what i was trying to do. It declares the sheet name and references an existing chart named chart 1.
Dim cht As ChartObject
Dim rng As Range
Set cht = Sheets("Master").ChartObjects("Chart 1")
Set rng = Sheets("Master").Range("A4", Range("D4").End(xlDown).Offset(-1))
cht.Chart.SetSourceData Source:=rng
cht.Chart.HasTitle = True
cht.Chart.ChartTitle.Text = "Bird Report - By Cost Code/Activity" ' title
cht.Chart.SeriesCollection(1).Name = "=Master!$B$3"
cht.Chart.SeriesCollection(2).Name = "=Master!$C$3"
cht.Chart.SeriesCollection(3).Name = "=Master!$D$3"

Chart Data Table Sizing Issue

I am running a macro to resize my plot area to make all graphs uniform within a report. However, when resizing the plot area the Chart Data Table resizes as well and the Labels for the rows in the Data Table become text wrapped. This issue is fixed if I manually resize the Plot Area slightly and then the text is fixed and is autofit and no longer text wrapped to take up three or four lines. Any way to fix this? This is my current code.
Sub Color_Loop_Series()
Dim sht As Worksheet
Dim CurrentSheet As Worksheet
Dim cht As ChartObject
Dim ser As Series
For Each sht In ActiveWorkbook.Worksheets
For Each cht In sht.ChartObjects
cht.Activate
ActiveChart.ChartType = xlLineMarkers
ActiveChart.Legend.Position = xlLegendPositionBottom
ActiveChart.Legend.Font.Size = 9
ActiveChart.PlotArea.Select
Selection.Width = 380
Selection.Left = 11
Selection.Top = 3
Selection.Height = 250
ActiveChart.Axes(xlValue).AxisTitle.Position = xlAxisPositionLeft
ActiveChart.DataTable.Font.Size = 5.5
Next cht
Next sht
End Sub
I was able to duplicate your problem using Excel 2010 by recording a macro while moving the plot area. When running the recorded macro the data table legend resized and text wrapped. I was not able to find any direct handle for the data table legend but did find that both .Left and .InsideLeft affect word wrapping.
Here is your code, modified a little to remove all Active statements.
Sub Color_Loop_Series()
Dim sht As Worksheet
Dim CurrentSheet As Worksheet
Dim cht As ChartObject
Dim ser As Series
For Each sht In ActiveWorkbook.Worksheets
For Each cht In sht.ChartObjects
With cht.Chart
.ChartType = xlLineMarkers
.Legend.Position = xlLegendPositionBottom
.Legend.Font.Size = 9
.DataTable.Font.Size = 5.5
.Axes(xlValue).AxisTitle.Position = xlAxisPositionLeft
End With
With cht.Chart.PlotArea
.Width = 380
.Height = 250
.Left = 11
.Top = 3
.InsideLeft = 80 '<- Adjust this value
End With
Next cht
Next sht
End Sub
Results when using .InsideLeft
Results when not using .InsideLeft

How can I edit this code to make it only apply to one chart?

I found Hide data label containing series name if value is zero on Super User but it removes data labels that have a value of 0 for all charts:
Sub RemoveZeroValueDataLabel()
'runs through every chart on the ActiveSheet
Dim cht As Chart
Dim chtObj As ChartObject
For Each chtObj In ActiveSheet.ChartObjects
Set cht = chtObj.Chart
Dim ser As Series
For Each ser In cht.SeriesCollection
Dim vals As Variant
vals = ser.Values
'include this line if you want to reestablish labels before deleting
ser.ApplyDataLabels xlDataLabelsShowLabel, , , , True, False, False, False, False
'loop through values and delete 0-value labels
Dim i As Integer
For i = LBound(vals) To UBound(vals)
If vals(i) = 0 Then
With ser.Points(i)
If .HasDataLabel Then
.DataLabel.Delete
End If
End With
End If
Next i
Next ser
Next chtObj
End Sub
I tried to edit it myself:
Sub RemoveZeroValueDataLabelonlyonechart()
Dim cht As Chart
Dim chtObj As ChartObject
Set cht = chtObj.Chart
Dim ser As Series
For Each ser In cht.SeriesCollection
Dim vals As Variant
vals = ser.Values
'include this line if you want to reestablish labels before deleting
ser.ApplyDataLabels xlDataLabelsShowLabel, , , , True, False, False, False, False
'loop through values and delete 0-value labels
Dim i As Integer
For i = LBound(vals) To UBound(vals)
If vals(i) = 0 Then
With ser.Points(i)
If .HasDataLabel Then
.DataLabel.Delete
End If
End With
End If
Next i
Next ser
End Sub
But this returns:
Microsoft visual basic | Run-time error '91' | Object variable or With block variable not set
How can I edit the code so it only removes data labels from the chart I have selected, not all charts in the sheet?
Dim chtObj As ChartObject
In the original loop chtObj loops on all the chart objects in the ActiveSheet. Here you want to set it only on a specific Chart object, so you removed the For loop, fine. But your chtObj, which you defined as a reference to a ChartObject, references nothing up till now. You need to assign it to some Chart object. You need to know either the name or the index of the Chart object you want to modify. Then you will add one simple line after the one above:
Set chtObj = ActiveSheet.ChartObjects("someName")
or, if the chart is the first one created within that worksheet:
Set chtObj = ActiveSheet.ChartObjects(1)
After you add one of these two lines, with the appropriate name or number that corresponds to the target Chart, the rest of the code should work fine.

How to orderly arrange charts in excel using VBA?

I have this code but when the VBA copies and pastes the charts on the Excelsheet, the Charts overlap.
Is there a way to arrange them in 1 column without overlapping?
Thank you!
Sub Test1()
Dim cht As Excel.ChartObject
Worksheets("ChartObjects").ChartObjects.Delete
For Each Sheet In ActiveWorkbook.Worksheets
If Sheet.Name <> "ChartObjects" Then
Sheet.Select
For Each cht In Sheet.ChartObjects
cht.Select
cht.Copy
Sheets("ChartObjects").Select
Range("C5").Select
ActiveSheet.Paste
Next
End If
Next Sheet
End Sub
You can use the .Top and .Left properties of the cell, and the .Top and .Height properties of each ChartObject to align each successive chart with the previous chart's bottom:
Sub foo()
Dim cht As ChartObject
Dim newCht As ChartObject
Dim sht As Worksheet
Dim chtSht As Worksheet
Dim top As Double
Dim left As Double
'Define the destination worksheet
Set chtSht = ActiveWorkbook.Worksheets("ChartObjects")
'Define the starting parameter for the charts
With chtSht.Range("C5")
top = .top
left = .left
End With
'Iterate the sheets in the workbook
For Each sht In ActiveWorkbook.Worksheets
'Ignore the chtSheet
If Not sht.Name = chtSht.Name Then
'Iterate the charts in each worksheet
For Each cht In sht.ChartObjects
'Copy the chart
cht.Copy
'Paste it in to the destination sheet
chtSht.Paste
'Get a handle on the chart we just pasted
Set newCht = chtSht.ChartObjects(chtSht.ChartObjects.Count)
'Assign the top location of this chart
newCht.top = top
newCht.left = left
'Add with the height of this chart to determine the "top" for the next chart
top = newCht.top + newCht.Height
Next
End If
Next
End Sub