Have been using this blog to link chart axis to cell values.
Sub ScaleAxes()
Dim wks As Worksheet
Set ws = Worksheets("AXIS")
Set cht = ActiveWorkbook.ChartObjects("ChartName1","ChartName2")
For Each cht In ActiveWorkbook.ChartObjects
cht.Activate
With ActiveChart.Axes(xlCategory, xlPrimary)
.MaximumScale = ws.Range("$B$12").Value
.MinimumScale = ws.Range("$B$11").Value
.MajorUnit = ws.Range("$B$13").Value
End With
Next cht
End Sub
I'm aiming for the values a single worksheet with axis values to update multiple charts on different worksheets. Most examples are using charts on the same worksheet. I currently get error 438 - any ideas?
Try the code below, explanations inside the code as comments:
Option Explicit
Sub ScaleAxes()
Dim Sht As Worksheet
Dim ws As Worksheet
Dim chtObj As ChartObject
Dim ChtNames
Set ws = Worksheets("AXIS")
' you need to get the names of the charts into an array, not ChartObjects array
ChtNames = Array("ChartName1", "ChartName2")
' first loop through all worksheet
For Each Sht In ActiveWorkbook.Worksheets
' loop through all ChartObjects in each worksheet
For Each chtObj In Sht.ChartObjects
With chtObj
'=== use the Match function to check if current chart's name is found within the ChtNames array ===
If Not IsError(Application.Match(.Name, ChtNames, 0)) Then
With .Chart.Axes(xlCategory, xlPrimary)
.MaximumScale = ws.Range("B12").Value
.MinimumScale = ws.Range("B11").Value
.MajorUnit = ws.Range("B13").Value
End With
End If
End With
Next chtObj
Next Sht
End Sub
Related
i have a graph multiples graph on a sheet that when i change values sometimes the label of the graph does not show. how can i create a macro that reset label text for the chart. i tried to record a macro but will only update one legend but not all of the labels that i have.enter image description here
Sub Macro1()
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.PlotArea.Select
ActiveChart.ApplyDataLabels
ActiveChart.FullSeriesCollection(1).DataLabels.Select
Selection.AutoText = True
End Sub
Here's a quick example on how you can apply data labels (and even how to position the labels) on any given chart:
Option Explicit
Sub test()
Dim ws As Worksheet
Set ws = Sheet1
Dim chartObj As ChartObject
For Each chartObj In ws.ChartObjects
ShowDataLabels chartObj.Chart
Next chartObj
End Sub
Sub ShowDataLabels(ByRef theChart As Chart, _
Optional ByVal show As Boolean = True)
With theChart
Dim i As Long
For i = 1 To .SeriesCollection.Count
With .SeriesCollection(i)
.HasDataLabels = True
.DataLabels.ShowValue = True
.DataLabels.Position = xlLabelPositionAbove
End With
Next i
End With
End Sub
I have 10 sheets. Eachs sheet has x-values and y-values which I want to plot in ONE xy-scatter plot. I wrote the code below. It works for one sheet but not for all the sheets. Additionally, I dont know how to name each series with a specific name (it can be references to a particular cell in each sheet). Kindly note that on each sheet; x-values, y-value are exactly starting and end in the same cell reference. Same is true for series name cell reference.
Sub PlotPcVsSwAllSheets()
Dim ch As Chart
Dim Sw As Range
Dim Pcres As Range
Dim ws As Worksheet
Set ch = ActiveSheet.Shapes.AddChart(xlXYScatter).Chart
For Each ws In Worksheets
Set ws.Sw = ws.Range("C23", Range("C23").End(xlDown))
Set ws.Pcres = ws.Range("AA23", Range("AA23").End(xlDown))
With ch
ch.SetSourceData Source:=Union(ws.Sw, ws.Pcres)
End With
Next ws
End Sub
You need to add each series one by one.
Sub PlotPcVsSwAllSheets()
Dim ch As Chart
Dim Sw As Range
Dim Pcres As Range
Dim ws As Worksheet, wb As Workbook
Set wb = ThisWorkbook
Set ch = ActiveSheet.Shapes.AddChart(xlXYScatter).Chart
'remove any series added by default
Do While ch.SeriesCollection.Count > 0
ch.SeriesCollection(1).Delete
Loop
For Each ws In wb.Worksheets
Set Sw = ws.Range("C23", ws.Range("C23").End(xlDown))
Set Pcres = Sw.EntireRow.Columns("AA") 'safer
With ch.SeriesCollection.NewSeries
.XValues = Sw
.Values = Pcres
.Name = ws.Range("A3").Value 'for example
End With
Next ws
End Sub
I have the following VBA code that works to export a range of cells into a jpeg into a specified folder. I would like to have it loop through all worksheets in one workbook.
I need help looping this code through all open workbooks. I believe I will need to:
Dim WS As Worksheet, then set up an If statement, insert the below code, end the if statement, then at the end put a Next WS for it to actually loop through. My problem is, is that I keep getting a 91 error when I try to combine my if statement, For Each WS In ThisWorkbook.Sheets If Not WS.Name = "Sheet2" Then, with my code below.
The following code works in one worksheet at a time.
Sub ExportAsImage()
Dim objPic As Shape
Dim objChart As Chart
Dim i As Integer
Dim intCount As Integer
'copy the range as an image
Call ActiveSheet.Range("A1:F2").CopyPicture(xlScreen, xlPicture)
'remove all previous shapes in the ActiveSheet
intCount = ActiveSheet.Shapes.Count
For i = 1 To intCount
ActiveSheet.Shapes.Item(1).Delete
Next i
'create an empty chart in the ActiveSheet
ActiveSheet.Shapes.AddChart
'select the shape in the ActiveSheet
ActiveSheet.Shapes.Item(1).Select
ActiveSheet.Shapes.Item(1).Width = Range("A1:F2").Width
ActiveSheet.Shapes.Item(1).Height = Range("A1:F2").Height
Set objChart = ActiveChart
'clear the chart
objChart.ChartArea.ClearContents
'paste the range into the chart
objChart.Paste
'save the chart as a JPEG
objChart.Export ("C:\Users\------\Desktop\Test\" & Range("B2").Value & ".jpg")
'remove all shapes in the ActiveSheet
intCount = ActiveSheet.Shapes.Count
For i = 1 To intCount
ActiveSheet.Shapes.Item(1).Delete
Next i
End Sub
Add this to your module:
Sub MAIN()
Dim sh As Worksheet
For Each sh In Sheets
sh.Activate
Call ExportAsImage
Next sh
End Sub
and run it. (there is no need to modify your code)
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
I'd like to change the values of a chart axis autumatically with macro from a cell. I can get it to work, if the command button and chart are on the same sheet. But I'd like to change it on chart, that is not in a normal sheet, but in a "chart sheet", so reference to is a little bit different. Does anyone now how?
Sub ChangeAxisScale()
With ActiveSheet.ChartObjects("chart21").Chart
With .Axes(xlValue)
.MaximumScale = ActiveSheet.Range("Axis_max").Value
.MinimumScale = ActiveSheet.Range("Axis_min").Value
.MajorUnit = ActiveSheet.Range("Unit").Value
End With
End With
End Sub
You have to use appropriate references. For example (Untested)
Sub ChangeAxisScale()
Dim wsChart As Chart
Dim wsInput As Worksheet
'~~> Change the below as applicable
Set wsChart = Chart1 '<~~ Code name of the chart sheet
Set wsInput = ThisWorkbook.Sheets("Sheet1") '<~~ Name of sheet with data
With wsChart
With .Axes(xlValue)
.MaximumScale = wsInput.Range("Axis_max").Value
.MinimumScale = wsInput.Range("Axis_min").Value
.MajorUnit = wsInput.Range("Unit").Value
End With
End With
End Sub