Hi. I am a new Excel VBA user and need help.
I have created a Userform named "ChromatographySystemChoice" which is something like the image below
Ignoring the frame control, I have added a textbox where I get a numeric value of the number of samples and click on "Add Sample Details" to fetch its value.
It displays something like this :
UserForm code is as follows :
I have created the following Class Module "Class 1" to get the object instances to work certain way.
However, when I run the Userform, the class property gets manipulated only in the last line of the chosen number of samples.
I need to find a way to fetch a variable or some sort of an array at runtime (after I add the number and click on "Add Sample Details", so that every object instance gets attached to the class module and its events. Any help would be appreciated.
I have created a Userform named "ChromatographySystemChoice" which is something like the image below
Ignoring the frame control, I have added a textbox where I get a numeric value of the number of samples and click on "Add Sample Details" to fetch its value.
It displays something like this :
UserForm code is as follows :
VBA Code:
Option Explicit
Option Base 1
Dim objMyEventClass As New Class1
Public NumberOfSamplesToAnalyze As Long
Public SampleIDNumber As Long
Private Sub AddSampleDetails_Click()
NumberOfSamplesToAnalyze = NumberOfSamples.Value
ReDim txtB1(1 To NumberOfSamplesToAnalyze)
ReDim txtB2(1 To NumberOfSamplesToAnalyze)
ReDim txtB3(1 To NumberOfSamplesToAnalyze)
ReDim txtB4(1 To NumberOfSamplesToAnalyze)
ReDim txtB5(1 To NumberOfSamplesToAnalyze)
ReDim txtB6(1 To NumberOfSamplesToAnalyze)
ReDim chk1(1 To NumberOfSamplesToAnalyze)
ReDim chk2(1 To NumberOfSamplesToAnalyze)
ReDim chk3(1 To NumberOfSamplesToAnalyze)
ReDim chk4(1 To NumberOfSamplesToAnalyze)
ReDim chk5(1 To NumberOfSamplesToAnalyze)
Dim Lbl(1 To 11) As Control
Dim LabelCaption
Dim j As Integer, i As Integer
LabelCaption = Array("Batch No.", "Run No.", "Tag", "Volume (mL)", "From Fraction", "To Fraction", "Load", "Standard", "Pool", "Fraction", "Other")
Set Lbl(1) = Me.Controls.Add("Forms.Label.1")
With Lbl(1)
.Name = "Label1"
.Height = 20
.Width = 100
.Left = 20
.Top = NumberOfSamples.Top + NumberOfSamples.Height + 10
.Caption = LabelCaption(1)
End With
For j = 2 To 11
Set Lbl(j) = Me.Controls.Add("Forms.Label.1")
With Lbl(j)
.Name = "Label" & j
.Height = 20
.Width = 35
.Left = Lbl(j - 1).Left + Lbl(j - 1).Width + 20
.Top = NumberOfSamples.Top + NumberOfSamples.Height + 10
.Caption = LabelCaption(j)
End With
Next j
For i = 1 To NumberOfSamplesToAnalyze
Set txtB1(i) = Me.Controls.Add("Forms.TextBox.1")
With txtB1(i)
.Name = "TextBox" & i & "1"
.Height = 20
.Width = 100
.Left = 20
.Top = Lbl(1).Top + (25 * i)
End With
Set txtB2(i) = Me.Controls.Add("Forms.TextBox.1")
With txtB2(i)
.Name = "TextBox" & i & "2"
.Height = 20
.Width = 35
.Left = txtB1(i).Left + txtB1(i).Width + 20
.Top = Lbl(1).Top + (25 * i)
End With
Set txtB3(i) = Me.Controls.Add("Forms.TextBox.1")
With txtB3(i)
.Name = "TextBox" & i & "3"
.Height = 20
.Width = 35
.Left = txtB2(i).Left + txtB2(i).Width + 20
.Top = Lbl(1).Top + (25 * i)
End With
Set txtB4(i) = Me.Controls.Add("Forms.TextBox.1")
With txtB4(i)
.Name = "TextBox" & i & "4"
.Height = 20
.Width = 35
.Left = txtB3(i).Left + txtB3(i).Width + 20
.Top = Lbl(1).Top + (25 * i)
End With
Set txtB5(i) = Me.Controls.Add("Forms.TextBox.1")
With txtB5(i)
.Name = "TextBox" & i & "5"
.Height = 20
.Width = 35
.Left = txtB4(i).Left + txtB4(i).Width + 20
.Top = Lbl(1).Top + (25 * i)
End With
Set txtB6(i) = Me.Controls.Add("Forms.TextBox.1")
With txtB6(i)
.Name = "TextBox" & i & "6"
.Height = 20
.Width = 35
.Left = txtB5(i).Left + txtB5(i).Width + 20
.Top = Lbl(1).Top + (25 * i)
End With
Set chk1(i) = Me.Controls.Add("Forms.CheckBox.1")
With chk1(i)
.Name = "CheckBox" & i & "1"
.Height = 20
.Width = 35
.Left = txtB6(i).Left + txtB6(i).Width + 20
.Top = Lbl(1).Top + (25 * i)
End With
Set objMyEventClass.cbLODEvents = chk1(i)
Set chk2(i) = Me.Controls.Add("Forms.CheckBox.1")
With chk2(i)
.Name = "CheckBox" & i & "2"
.Height = 20
.Width = 35
.Left = chk1(i).Left + chk1(i).Width + 20
.Top = Lbl(1).Top + (25 * i)
End With
Set objMyEventClass.cbSTDEvents = chk2(i)
Set chk3(i) = Me.Controls.Add("Forms.CheckBox.1")
With chk3(i)
.Name = "CheckBox" & i & "3"
.Height = 20
.Width = 35
.Left = chk2(i).Left + chk2(i).Width + 20
.Top = Lbl(1).Top + (25 * i)
End With
Set objMyEventClass.cbPOOLEvents = chk3(i)
Set chk4(i) = Me.Controls.Add("Forms.CheckBox.1")
With chk4(i)
.Name = "CheckBox" & i & "4"
.Height = 20
.Width = 35
.Left = chk3(i).Left + chk3(i).Width + 20
.Top = Lbl(1).Top + (25 * i)
End With
Set objMyEventClass.cbFRACEvents = chk4(i)
Set chk5(i) = Me.Controls.Add("Forms.CheckBox.1")
With chk5(i)
.Name = "CheckBox" & i & "5"
.Height = 20
.Width = 35
.Left = chk4(i).Left + chk4(i).Width + 20
.Top = Lbl(1).Top + (25 * i)
End With
Set objMyEventClass.cbOTHEREvents = chk5(i)
Next i
SampleIDNumber = i - 1
End Sub
Private Sub CancelChromatographySystemChoice_Click()
Unload ChromatographySystemChoice
End Sub
Private Sub OKChromatographySystemChoice_Click()
Unload ChromatographySystemChoice
End Sub
I have created the following Class Module "Class 1" to get the object instances to work certain way.
VBA Code:
Option Base 1
Option Explicit
Public WithEvents cbLODEvents As MSForms.CheckBox
Public WithEvents cbSTDEvents As MSForms.CheckBox
Public WithEvents cbPOOLEvents As MSForms.CheckBox
Public WithEvents cbFRACEvents As MSForms.CheckBox
Public WithEvents cbOTHEREvents As MSForms.CheckBox
Dim SampleID As Integer
Public Sub cbLODEvents_Click()
SampleID = ChromatographySystemChoice.SampleIDNumber
ChromatographySystemChoice.Controls("CheckBox" & SampleID & "5").Value = False
ChromatographySystemChoice.Controls("CheckBox" & SampleID & "2").Value = False
ChromatographySystemChoice.Controls("CheckBox" & SampleID & "3").Value = False
ChromatographySystemChoice.Controls("CheckBox" & SampleID & "4").Value = False
ChromatographySystemChoice.Controls("TextBox" & SampleID & "3").Value = "LOD"
ChromatographySystemChoice.Controls("TextBox" & SampleID & "5").Enabled = False
ChromatographySystemChoice.Controls("TextBox" & SampleID & "5").BackColor = &H80000016
ChromatographySystemChoice.Controls("TextBox" & SampleID & "6").Enabled = False
ChromatographySystemChoice.Controls("TextBox" & SampleID & "6").BackColor = &H80000016
End Sub
Public Sub cbSTDEvents_Click()
SampleID = ChromatographySystemChoice.SampleIDNumber
ChromatographySystemChoice.Controls("CheckBox" & SampleID & "5").Value = False
ChromatographySystemChoice.Controls("CheckBox" & SampleID & "1").Value = False
ChromatographySystemChoice.Controls("CheckBox" & SampleID & "3").Value = False
ChromatographySystemChoice.Controls("CheckBox" & SampleID & "4").Value = False
ChromatographySystemChoice.Controls("TextBox" & SampleID & "3").Value = "STD"
ChromatographySystemChoice.Controls("TextBox" & SampleID & "5").Enabled = False
ChromatographySystemChoice.Controls("TextBox" & SampleID & "5").BackColor = &H80000016
ChromatographySystemChoice.Controls("TextBox" & SampleID & "6").Enabled = False
ChromatographySystemChoice.Controls("TextBox" & SampleID & "6").BackColor = &H80000016
End Sub
Public Sub cbPOOLEvents_Click()
SampleID = ChromatographySystemChoice.SampleIDNumber
ChromatographySystemChoice.Controls("CheckBox" & SampleID & "5").Value = False
ChromatographySystemChoice.Controls("CheckBox" & SampleID & "1").Value = False
ChromatographySystemChoice.Controls("CheckBox" & SampleID & "2").Value = False
ChromatographySystemChoice.Controls("CheckBox" & SampleID & "4").Value = False
ChromatographySystemChoice.Controls("TextBox" & SampleID & "3").Value = ""
ChromatographySystemChoice.Controls("TextBox" & SampleID & "5").Enabled = True
ChromatographySystemChoice.Controls("TextBox" & SampleID & "5").BackColor = vbWhite
ChromatographySystemChoice.Controls("TextBox" & SampleID & "6").Enabled = True
ChromatographySystemChoice.Controls("TextBox" & SampleID & "6").BackColor = vbWhite
End Sub
Public Sub cbFRACEvents_Click()
SampleID = ChromatographySystemChoice.SampleIDNumber
ChromatographySystemChoice.Controls("CheckBox" & SampleID & "5").Value = False
ChromatographySystemChoice.Controls("CheckBox" & SampleID & "1").Value = False
ChromatographySystemChoice.Controls("CheckBox" & SampleID & "2").Value = False
ChromatographySystemChoice.Controls("CheckBox" & SampleID & "3").Value = False
ChromatographySystemChoice.Controls("TextBox" & SampleID & "3").Value = ""
ChromatographySystemChoice.Controls("TextBox" & SampleID & "5").Enabled = False
ChromatographySystemChoice.Controls("TextBox" & SampleID & "5").BackColor = &H80000016
ChromatographySystemChoice.Controls("TextBox" & SampleID & "6").Enabled = False
ChromatographySystemChoice.Controls("TextBox" & SampleID & "6").BackColor = &H80000016
End Sub
Public Sub cbOTHEREvents_Click()
SampleID = ChromatographySystemChoice.SampleIDNumber
ChromatographySystemChoice.Controls("CheckBox" & SampleID & "1").Value = False
ChromatographySystemChoice.Controls("CheckBox" & SampleID & "2").Value = False
ChromatographySystemChoice.Controls("CheckBox" & SampleID & "3").Value = False
ChromatographySystemChoice.Controls("CheckBox" & SampleID & "4").Value = False
ChromatographySystemChoice.Controls("TextBox" & SampleID & "3").Value = ""
ChromatographySystemChoice.Controls("TextBox" & SampleID & "5").Enabled = False
ChromatographySystemChoice.Controls("TextBox" & SampleID & "5").BackColor = &H80000016
ChromatographySystemChoice.Controls("TextBox" & SampleID & "6").Enabled = False
ChromatographySystemChoice.Controls("TextBox" & SampleID & "6").BackColor = &H80000016
End Sub
However, when I run the Userform, the class property gets manipulated only in the last line of the chosen number of samples.
I need to find a way to fetch a variable or some sort of an array at runtime (after I add the number and click on "Add Sample Details", so that every object instance gets attached to the class module and its events. Any help would be appreciated.