Passing arguments to a graphing function

RawlinsCross

Active Member
Joined
Sep 9, 2016
Messages
437
Good day. I have a userform with approximately 50 labels each containing a number which is the latest number in a column of numbers (i.e. I have 50 columns and say 365 rows). I have a code that calls another userform which has a graph when you click on any of the 50 labels (see code below).

So the long of it is - I don't want to make 50 separate userforms for each of the labels. I'd like to have just one userform containing the code (see below again) that accepts arguments for things like axis title, MinimumScale, NumberFormat, .Range - things like that. How is this done?

Code:
Private Sub UserForm_Initialize()

Dim MyChart As Chart
Dim ChartData As Range
Dim ChartName As String
Application.ScreenUpdating = False
Worksheets("Dashboard").Range("H4").Value = ActiveWindow.Zoom
ActiveWindow.Zoom = 85
 
Set ChartData = Worksheets("Main Element Profiles").Range("Y7:Y37")
        

ActiveSheet.Range("B2").Select
Set MyChart = ActiveSheet.Shapes.AddChart(xlXYScatterLines).Chart

With MyChart
    .SeriesCollection.NewSeries
    .SeriesCollection(1).Name = ChartName
    .SeriesCollection(1).Values = ChartData
    .SeriesCollection(1).XValues = Worksheets("Main Element Profiles").Range("B7:B37")
    .Legend.Select
        Selection.Delete
    .Axes(xlCategory).Select
        Selection.TickLabels.NumberFormat = "m/d/yyyy"
        Selection.TickLabels.NumberFormat = "[$-409]mmm-dd;@"
    .Axes(xlValue).Select
        Selection.TickLabels.NumberFormat = "#,##0.00"
        Selection.TickLabels.NumberFormat = "#,##0.0,"
    .Axes(xlValue).HasTitle = True
    .Axes(xlValue).MinimumScale = 0
    .Axes(xlValue).AxisTitle.Text = "Na Concentration (g/L)"
End With
   

Dim ImageName As String
ImageName = Application.DefaultFilePath & Application.PathSeparator & "TempChart.jpeg"
MyChart.Export filename:=ImageName
ActiveSheet.ChartObjects(1).Delete
ActiveWindow.Zoom = Worksheets("Dashboard").Range("H4").Value
Application.ScreenUpdating = True
ASSAY221FLASH2NA.Image1.Picture = LoadPicture(ImageName)
 
End Sub
 
Getting interesting now. Okay, everything in your pseudo code makes sense except for the "assuming this expressing is used for all 50 labels". In fact the description for each of the 50 labels is unique. I'll include some examples of the first few labels

Label ACFeed_AC = Element & " Tonnage through Autoclave"
Label ANOLYTE_AC = Element & " Tonnage from Anolyte through Autoclave"
Label PLS_HD = Element & " Tonnage of PLS to HD"
+ 47 more labels each with a unique Element & "from "this" to "that""

*idea* not sure what the tag property of a label is (vs. Name property) but could we not pull the graph userform's caption from there? It can't be pulled from the label's caption as that were the data goes.

Everything else looks fine. So Date1 and Date2 will be sent to MakeChart, the Index will denote the location of the data (e.g. ACFEED_AC_CO).

What's next?
 
Last edited:
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
John,

Using the Tag property of the Label is one way that the Caption1 value could be stored with the controls. Another option would be to include the Caption1, (and possibly the AxisTitle) values in the worksheet's data table. Those could be located in a row above or below the IndexTitle headers.

Either way could work. It might be easier to setup and make changes to the worksheet instead of the Tags- which can be cumbersome to edit.

Please let me know your preference and then post a small screenshot showing a few rows of your dataset that show the relationship between the header(s) and data values.
 
Upvote 0
Is this approximately what you were looking for?



*post edit* - shoot, how do you send screenshots?
 
Last edited:
Upvote 0
Until I figure out the picture thing (URL?, can't browse your computer?), I've entered some rows above the data and listed the variables and the values for the first label on the Main Userform. Is this what you are looking for?

[TABLE="width: 1740"]
<tbody>[TR]
[TD][/TD]
[TD]ACFEED_AC_[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Data Identifier[/TD]
[TD]ACFEED_AC_Ni[/TD]
[TD]ACFEED_AC_Co[/TD]
[TD]ACFEED_AC_Cu[/TD]
[TD]ACFEED_AC_Fe[/TD]
[TD]ACFEED_AC_Na[/TD]
[TD]ACFEED_AC_Mg[/TD]
[TD]ACFEED_AC_Mn[/TD]
[TD]ACFEED_AC_Ca[/TD]
[TD]ACFEED_AC_Si[/TD]
[TD]ACFEED_AC_B [/TD]
[TD]ACFEED_AC_Cl[/TD]
[/TR]
[TR]
[TD]Caption for Graph[/TD]
[TD]Ni Tonnage Through Autoclave[/TD]
[TD]Co Tonnage Through Autoclave[/TD]
[TD]Cu Tonnage Through Autoclave[/TD]
[TD]Fe Tonnage Through Autoclave[/TD]
[TD]Na Tonnage Through Autoclave[/TD]
[TD]Mg Tonnage Through Autoclave[/TD]
[TD]Mn Tonnage Through Autoclave[/TD]
[TD]Ca Tonnage Through Autoclave[/TD]
[TD]Si Tonnage Through Autoclave[/TD]
[TD]B Tonnage Through Autoclave[/TD]
[TD]Cl Tonnage Through Autoclave[/TD]
[/TR]
[TR]
[TD]Axis Title[/TD]
[TD]Ni Tonnage (t)[/TD]
[TD]Co Tonnage (t)[/TD]
[TD]Cu Tonnage (t)[/TD]
[TD]Fe Tonnage (t)[/TD]
[TD]Na Tonnage (t)[/TD]
[TD]Mg Tonnage (t)[/TD]
[TD]Mn Tonnage (t)[/TD]
[TD]Ca Tonnage (t)[/TD]
[TD]Si Tonnage (t)[/TD]
[TD]B Tonnage (t)[/TD]
[TD]Cl Tonnage (t)[/TD]
[/TR]
[TR]
[TD]Number Format[/TD]
[TD]#,##0[/TD]
[TD]#,##0[/TD]
[TD]#,##0[/TD]
[TD]#,##0[/TD]
[TD]#,##0[/TD]
[TD]#,##0[/TD]
[TD]#,##0[/TD]
[TD]#,##0[/TD]
[TD]#,##0[/TD]
[TD]#,##0[/TD]
[TD]#,##0[/TD]
[/TR]
[TR]
[TD]MinimumScale[/TD]
[TD]Auto[/TD]
[TD]Auto[/TD]
[TD]Auto[/TD]
[TD]Auto[/TD]
[TD]Auto[/TD]
[TD]Auto[/TD]
[TD]Auto[/TD]
[TD]Auto[/TD]
[TD]Auto[/TD]
[TD]Auto[/TD]
[TD]Auto[/TD]
[/TR]
[TR]
[TD]Maximum Scale[/TD]
[TD]Auto[/TD]
[TD]Auto[/TD]
[TD]Auto[/TD]
[TD]Auto[/TD]
[TD]Auto[/TD]
[TD]Auto[/TD]
[TD]Auto[/TD]
[TD]Auto[/TD]
[TD]Auto[/TD]
[TD]Auto[/TD]
[TD]Auto[/TD]
[/TR]
</tbody><colgroup><col><col span="11"></colgroup>[/TABLE]
 
Upvote 0
The worksheet is called "EBal" and the range start at row 14 and extends to row 379 but I normally graph it using the DTPicker functionality denoted by the code, partially shown below. But generally, the DTPickers have a maximum range of 365 days of data (or thereabouts).

Code:
'Setting up DT Picker
Set sh = Sheets("EBal")
Set r = sh.Range("a:a").Find(CDate(Me.DTPicker1), sh.[a7], xlValues)    ' first date
rn1 = r.Row                                                             ' where the date is
Set r = sh.Range("a:a").Find(CDate(Me.DTPicker2), sh.[a7], xlValues)    ' second date

Set rng1 = Worksheets("EBal").Range("A2:AZZ2").Find(Index, , xlValues, xlWhole)
ColumnLetter1 = Split(Cells(2, rng1.Column).Address, "$")(1)

Application.ScreenUpdating = False
Worksheets("Dashboard").Range("H4").Value = ActiveWindow.Zoom
ActiveWindow.Zoom = 85
Me.TextBox1.Value = "Auto"
Me.TextBox2.Value = "Auto"
Set ChartData = Worksheets("EBal").Range(ColumnLetter1 & r.Row & ":" & ColumnLetter1 & rn1)
        

ActiveSheet.Range("B2").Select
Set MyChart = ActiveSheet.Shapes.AddChart(xlXYScatter).Chart

With MyChart
    .SeriesCollection.NewSeries
    .SeriesCollection(1).Name = ChartName
    .SeriesCollection(1).Values = ChartData
    .SeriesCollection(1).XValues = Worksheets("EBal").Range("B" & rn1 & ":B" & r.Row)
 
Upvote 0
And what is the sheet and range for the header info shown in your post #14 ?

Is the Data Identifier "ACFEED_AC_Ni" on sheet EBal, in Cell B2?
 
Upvote 0
The table you see in Post #14 is on top of the corresponding data for ACFEED_AC_Ni, ACFEED_AC_Co, ACFEED_AC_Cu, etc (all the way to 11 elements) on sheet "EBal". Of course you know that the identifier (Index in the code in post #16 ) ACFEED_AC_Ni is used for the Y-data for the ACFEED_AC label with the Ni command button selected.

So that would begin for ACFeed_AC_Ni in cell RC2, the Y-data starts in cell RC14 with the X-data (dates) starting in B14. With the another Labels, they go all the way over to column AGI for the latest label (still not finished defining all the labels) for the label CUFE_SILICA_Ni, Co, Cu, etc.
 
Last edited:
Upvote 0
Ok I think I got it. One last clarification- you said "X-data (dates) starting in B14", am I correct in understanding the dates are in Column A (where the find of the datePicker value occurs) and the first Y values are in column B?
 
Upvote 0
Good eye, I must say. Yes, column B has dates that having a time component (depends on when the data is loaded into the spreadsheet). Of course, having a time component confuses DTPicker which is simply a date (perhaps 00:00 time if anything) so in column A14 down to A379 I apply the formula: =DATE(YEAR(B14),MONTH(B14),DAY(B14))

So the A column is simply the dates in the B column without the time component. At this point, I can get the r.Row and rn1 numbers outlined in the code in Post #16 using the data in the A column. But the actual data graphed for the x-axis is the B column.


As an aside, since I've inserted all the lines as one sees to fit in those new variables, perhaps I'll have to change the code in Post #16 from

Code:
'Setting up DT Picker
Set sh = Sheets("EBal")
Set r = sh.Range("a:a").Find(CDate(Me.DTPicker1), sh.[a7], xlValues)    ' first date
rn1 = r.Row                                                             ' where the date is
Set r = sh.Range("a:a").Find(CDate(Me.DTPicker2), sh.[a7], xlValues)    ' second date

to:

Code:
'Setting up DT Picker
Set sh = Sheets("EBal")
Set r = sh.Range("a:a").Find(CDate(Me.DTPicker1), sh.[a14], xlValues)    ' first date
rn1 = r.Row                                                             ' where the date is
Set r = sh.Range("a:a").Find(CDate(Me.DTPicker2), sh.[a14], xlValues)    ' second date
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,312
Members
452,634
Latest member
cpostell

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