Public Function declare for use in form with CommandButton and listbox

papakos

New Member
Joined
Apr 10, 2017
Messages
12
Hello,
Trying to declare some variables in Module, so when i hit commandbutton to take another value (also depending from listbox value) to create a graph in a picturebox,
but when i execute the values are 0.
Here is the code.

Option Explicit
Public Function mysub()
Dim myrange As Range
Dim mycol As String
Dim myrow_start As String
Dim myrow_end As String
Dim chartT As XlChartType




If CommandButton1.Click Then
mycol = "C"
Else
If CommandButton2.Click Then
mycol = "D"
Else
If CommandButton3.Click Then
mycol = "E"
Else
If CommandButton4.Click Then
mycol = "F"
etc etc
End If
End If
End If
End If


Select Case ListBox1.Value

Case ListBox1.Value = "A"
myrow_start = "6"
myrow_end = "17"

Case ListBox1.Value = "B"
myrow_start = "22"
myrow_end = "33"

Case ListBox1.Value = "C"
myrow_start = "38"
myrow_end = "49"
etc etc

End Function




Private Sub CommandButton1_Click()
myrange = mycol & myrow_start & ":" & mycol & myrow_end
Set ChartData_GR = ActiveSheet.Range(myrange)


On Error Resume Next
ActiveSheet.ChartObjects.Delete
On Error GoTo 0
ChartData.Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartArea.Width = 468
ActiveChart.ChartArea.Height = 354
ActiveChart.SetSourceData Source:=myrange
ActiveChart.ChartType = chartT
ActiveChart.ApplyLayout (5)
ActiveChart.ChartTitle.Select
ActiveChart.ChartTitle.Text = ListBox1.Value
ActiveChart.SeriesCollection(1).XValues = ActiveSheet.Range("C2:N2")
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
They need to be declared outside the function like this
Code:
Option Explicit
Public myrange As Range
Public mycol As String
Public myrow_start As String
Public myrow_end As String
Public chartT As XlChartType

Public Function mysub()
 
Upvote 0
They need to be declared outside the function like this
Code:
Option Explicit
Public myrange As Range
Public mycol As String
Public myrow_start As String
Public myrow_end As String
Public chartT As XlChartType

Public Function mysub()

Tried that. Still myrange=0
 
Upvote 0
You're not setting MyRange anywhere in the function.
 
Upvote 0
try
Code:
Set myrange = mycol & myrow_start & ":" & mycol & myrow_end
 
Upvote 0
Code:
Set myrange = rnage(mycol & myrow_start & ":" & mycol & myrow_end)
 
Upvote 0
How are you calling the Function?
 
Upvote 0

Forum statistics

Threads
1,215,003
Messages
6,122,655
Members
449,091
Latest member
peppernaut

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