I have a question about something which is confusing me in VBA. I have a command button, which when clicked I want to set a variable = to a workbook object, then pass this object to another sub. I cannot work out why I can't pass it to the sub, I can't see why what I'm doing doesn't work. Code as follows:
Private Sub CommandButton2_Click()
Workbooks.Open Filename:=TextBox1.Text
Set wb = ActiveWorkbook
CreateChart wb
End Sub
Sub CreateChart(ByRef wb As Workbook)
'PURPOSE: Create a chart (chart dimensions are not required)
Dim rng As Range
Dim cht As Object
'Your data range for the chart
Set rng = wb.ActiveSheet.Range("A24:A27")
'Create a chart
Set cht = ActiveSheet.Shapes.AddChart2
'Give chart some data
cht.Chart.SetSourceData Source:=rng
'Determine the chart type
cht.Chart.ChartType = xlXYScatterLines
End Sub
NB I have found a workaround which is to set the workbook within another sub, but would like to understand why it doesn't work this way. Please can anyone offer advice?
Thankyou.
wbinPrivate Sub CommandButton2first? As inDim wb As Workbook