Plotting multiple cruves on a single XY scatter plot from different sheets - vba

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

Related

Adding series from multiple workbooks

The following code is used to add a chart at the workbook that is opened. The chart data series values are taken from Worksheets(1) of every workbook inside "InputPathName" directory using a loop. Then I apply a template graph saved at "TemplatePath". Two problems arise:
Series Name will not have the value of cell B7. I tried 2 ways but did not work (marked as: ATTEMPT 1 & 2)
Some of the workbooks of directory "InputPathName" have more than one worksheet. If any worksheet other than worksheets(1) is active, I have a Run-time error '1004' Method 'Range' of object '_Worksheet' failed (highlighted line: Set xRange =...). If I add ws.Activate after Set ws=.... (as commented out), the graph will be a mess and won't show the correct results.
Note: If the active sheet in every workbook is worksheets(1), then the code works well.
DEVELOPING: How can I loop inside every worksheet of the workbooks without knowing the number of the worksheets?
Sub InputFromOtherWorksheets()
Dim ch As Chart
Dim ws As Worksheet
Dim wb As Workbook
Dim xRange As Range
Dim yRange As Range
Dim TemplatePath As String
Dim TemplateName As String
Dim InputPathName As String
Dim InputFileName As String
TemplatePath = "C:\Charts"
TemplateName = "templ4"
InputPathName = "C:\New folder\"
Set ch = Charts.Add2
InputFileName = Dir(InputPathName)
Do While InputFileName <> ""
Set wb = Workbooks.Open(InputPathName & InputFileName)
Set ws = wb.Worksheets(1)
'ws.Activate
Set xRange = ws.Range("B18", Range("B18").End(xlDown))
Set yRange = ws.Range("C18", Range("C18").End(xlDown))
With ch.SeriesCollection.NewSeries
.Name = ws.Range("B7") 'ATTEMPT 1
.XValues = xRange
.Values = yRange
End With
ch.SeriesCollection(1).Name = ws.Range("B7") 'ATTEMPT 2
wb.Close SaveChanges:=False
InputFileName = Dir()
Loop
ch.ApplyChartTemplate TemplatePath & "\" & TemplateName
ch.ChartTitle.Text = "Specimen 1"
End Sub
Try the code below:
With ch.SeriesCollection.NewSeries
.Name = "=" & ws.Range("B7").Address(False, False, xlA1, xlExternal)
' rest of your code
End With
To loop through every sheet of a workbook, you have to use Worksheets collection. Let's say we have set Workbook object in wb variable:
Dim ws As Worksheet
' some code
For Each ws in wb.Worksheets
' do some operations on ws object
Next

Excel chart axis scale

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

Splitting Sheets into Separate Workbooks

I have a workbook with a master sheet for school report cards. I have a macro applied to a button for exporting information from the master sheet to separate, newly-generated sheets in the same workbook. A1:C71 is the template and goes to every new sheet, and the following columns of info, from D1:71 to Q1:71, each appear in separate sheets (always in D1:71).
Here's the screenshot (http://imgur.com/a/ZDOVb), and here's the code:
`Option Explicit
Sub parse_data()
Dim studsSht As Worksheet
Dim cell As Range
Dim stud As Variant
Set studsSht = Worksheets("Input")
With CreateObject("Scripting.Dictionary")
For Each cell In studsSht.Range("D7:Q7").SpecialCells(xlCellTypeConstants, xlTextValues)
.Item(cell.Value) = .Item(cell.Value) & cell.EntireColumn.Address(False, False) & ","
Next
For Each stud In .keys
Intersect(studsSht.UsedRange, studsSht.Range(Left(.Item(stud), Len(.Item(stud)) - 1))).Copy Destination:=GetSheet(CStr(stud)).Range("D1")
Next
End With
studsSht.Activate
End Sub
Function GetSheet(shtName As String) As Worksheet
On Error Resume Next
Set GetSheet = Worksheets(shtName)
If GetSheet Is Nothing Then
Set GetSheet = Sheets.Add(after:=Worksheets(Worksheets.Count))
GetSheet.Name = shtName
Sheets("Input").Range("A1:C71").Copy
GetSheet.Range("A1:D71").PasteSpecial xlAll
GetSheet.Range("A1:B71").EntireColumn.ColumnWidth = 17.57
GetSheet.Range("C1:C71").EntireColumn.ColumnWidth = 54.14
GetSheet.Range("D1:D71").EntireColumn.ColumnWidth = 22
End If
End Function`
I would now like to create a separate button to split the sheets into separate workbooks so that the master sheet can be kept for record keeping and the individual workbooks can be shared with parents online (without divulging the info of any kid to parents other than their own). I would like the workbooks to be saved with the existing name of the sheet, and wonder if there's a way to have the new workbooks automatically saved in the same folder as the original workbook without having to input a path name? (It does not share the same filename as any of the sheets).
I tried finding other code and modifying it, but I just get single blank workbooks and I need as many as have been generated (preferably full of data!), which varies depending on the class size. Here's the pathetic attempt:
`Sub split_Reports()
Dim splitPath As String
Dim w As Workbook
Dim ws As Worksheet
Dim i As Long, j As Long
Dim lastr As Long
Dim wbkName As String
Dim wksName As String
Set wsh = ThisWorkbook.Worksheets(1)
splitPath = "G:\splitWb\"
Set w = Workbooks.Add
For i = 1 To lastr
wbkName = ws
w.Worksheets.Add(After:=w.Worksheets(Worksheets.Count)).Name = ws
w.SaveAs splitPath
w.Close
Set w = Workbooks.Add
Next i
End Sub`
I have learned so much, and yet I know so little.
Maybe this will start you off, just some simple code to save each sheet as a new workbook. You would probably need some check that the sheet name is a valid file name.
Sub x()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
ws.Copy
ActiveWorkbook.Close SaveChanges:=True, Filename:=ws.Name & ".xlsx"
Next ws
End Sub

Loop through worksheets while exporting range as image

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)

Copy and Paste Filtered Data from one Spreadsheet to another using VBA

I am trying to copy filtered data from one spreadsheet and paste it onto another using a macro but I receive an error when the code runs. What I have is as follows:
Sub Unmet_Projects()
Dim x As Workbook
Dim y As Workbook
Dim ws As Worksheet
Dim sh As Worksheet
Dim rng As Range
Set x = ThisWorkbook
Set y = Workbooks.Open("C:\Users\turnbull\Documents\Global Unmet Demand\1- extract-Unmet projects.xls")
Set ws = x.Sheets("Unmet Projects")
Set sh = y.Sheets("Sheet1")
Set rng = sh.Range("A1:CA100").Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy
x.Sheets("Unmet Projects").Range("L3").PasteSpecial xlValues
End Sub
Any help would be greatly appreciated.
Thanks