Pass data from a userform to a variable within a macro

llama24601

New Member
Joined
Jun 23, 2020
Messages
4
Office Version
  1. 2013
Platform
  1. Windows
Hi! I'm working on a macro which graphs columns selected with an input box, and I'm trying to change these input boxes to userforms (in order to decrease the amount of popups for the user). However, I'm unsure of how to assign cells selected in a userform to variables which can be used in a separate subroutine. (I'm new to creating userforms).

I know that this is a bit of a big ask, but any help would be greatly appreciated! Thanks so much!

Here is the code behind a command button in the userform which is *supposed* to save the data and close the userform:
VBA Code:
Private Sub CommandButtonX1_Click()
‘
Dim address1, address2 As String
Dim rng1, rng2 As Range
 ‘
address1 = RefEditX1.Value
Set rng1 = Range(address1)
 ‘
address2 = RefEditX1.Value
Set rng2 = Range(address2)
 ‘
Me.Hide
Unload Me
‘
End Sub


Here is my attempt to use the userform data in a subroutine:
VBA Code:
Sub ATrialRun()
 ‘
Load UserFormX1
UserFormX1.Show
 ‘
Dim myXTitle As Range
myXTitle = UserFormX1.RefEditX1.Value
 ‘
Dim myXCell As Range
myXCell = UserFormX1.RefEditX2.Value
 ‘
Dim myXSeries As Range
Set myXSeries = Range(myXCell, myXCell.End(xlDown))
 ‘
End Sub

This is the graphing subroutine with inputboxes: (I bolded the parts which I believe are pertinent, and which I want to replace with userforms)
VBA Code:
Sub ForumCombo2()
With ActiveSheet
'
‘ my favorite so far
‘
‘
' Set x values with input box
[B]Dim myXCell As Range
Dim myXSeries As Range
Dim myXTitle As Range
Set myXTitle = Application.InputBox("Please select the heading of the column which contains your desired X values:", "Select title cell", Type:=8)
Set myXCell = myXTitle.Offset(1, 0)
Set myXSeries = Range(myXCell, myXCell.End(xlDown))[/B]
'
'
' Set y values with input box
[B]Dim myYCell As Range
Dim myYSeries As Range
Dim myYTitle As Range
Set myYTitle = Application.InputBox("Please select the heading of the column which contains your desired Y values:", "Select title cell", Type:=8)
Set myYCell = myYTitle.Offset(1, 0)
Set myYSeries = Range(myYCell, myYCell.End(xlDown))[/B]
'
'
' Create Blank Graph
Dim chartObj As ChartObject
Dim DataChart As Chart
Set chartObj = ActiveSheet.ChartObjects.Add(Top:=10, Left:=325, Width:=600, Height:=300)
Set DataChart = chartObj.Chart
DataChart.ChartType = xlXYScatterSmooth
'
'
' Remove auto-plotted data
Do While DataChart.SeriesCollection.Count > 0
DataChart.SeriesCollection(1).Delete
Loop
'
'
' Add first data series
[B]With DataChart.SeriesCollection.NewSeries
    .Name = myYTitle
.XValues = myXSeries
    .Values = myYSeries[/B]
End With
'
'
' Display a message box with yes/no and question icon - want to continue?
If MsgBox("Would you like to add another Y data series to your graph?", vbQuestion + vbYesNo + vbDefaultButton2, "Continue?") = vbYes Then
     MsgBox "The user clicked Yes"
'
'
'
' BEGIN THE LOOP of selecting additional Y values until user selects NO
Do Until answer = vbNo
'
' Set additional y values with input box
[B]Dim myAddCell As Range
Dim myAddSeries As Range
Dim myAddTitle As Range
Set myAddTitle = Application.InputBox("Please select the heading of the column which contains the Y values you want to add:", "Select title cell", Type:=8)
Set myAddCell = myAddTitle.Offset(1, 0)
Set myAddSeries = Range(myAddCell, myAddCell.End(xlDown))[/B]
'
'
' Add the new data to graph
[B]With DataChart.SeriesCollection.NewSeries
   .Name = myAddTitle
.XValues = myXSeries
   .Values = myAddSeries[/B]
End With
'
'
' Display message box with yes/no and question icon
answer = MsgBox("Would you like to continue and select another Y data series?", vbQuestion + vbYesNo + vbDefaultButton2, "Continue?")
' END OF LOOP
Loop
Else
     MsgBox "The user clicked No"
End If
'
'
'
End With
End Sub

Again, thanks so much!
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
The RefEdit ActiveX control is known for its unpredictable behavior, it may cause Excel to crash. Way back Jon Peltier wrote a blog about this issue and provided an useful alternative. You might consider to take a look over here...
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,021
Latest member
pingpong7117

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