llama24601
New Member
- Joined
- Jun 23, 2020
- Messages
- 4
- Office Version
- 2013
- Platform
- 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:
Here is my attempt to use the userform data in a subroutine:
This is the graphing subroutine with inputboxes: (I bolded the parts which I believe are pertinent, and which I want to replace with userforms)
Again, thanks so much!
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!