Problem identifying with Checkbox's status

G

Guest

Hi

I have three checkboxes on my userform ("Userform") chk1, chk2, and chk3. I would like to record the name(s) of the checked checkbox(es) in a dynamic array ("MyArray")

For some reason my current code below doesn't recognise that the checkbox has been checked. It is also interesting to note that the ".Value" method for ctrlDummy does not show up when typing the code...maybe this could be the reason

Could you please help me fix this error

----------------------------------------------------------
Sub ChkBox(

Dim ctrlDummy As msforms.Contro
Dim MyArray() As String
Dim i as Intege

For Each ctrlDummy In Userform.Control

If TypeOf ctrlDummy Is msforms.CheckBox The

If ctrlDummy.Value The

MyArray(i) = ctrlDummy.Nam

i = i +

End I

End I

Next ctrlDumm

ReDim MyArray(i

End Su
 
G

Greg Wilson

Sub ChkBox()
Dim ctrlDummy As MSForms.Control
Dim MyArray() As String
Dim i As Integer, ii As Integer
i = 0
For Each ctrlDummy In UserForm.Controls
If TypeOf ctrlDummy Is MSForms.CheckBox Then
If ctrlDummy.Value = True Then
ReDim Preserve MyArray(i)
MyArray(i) = ctrlDummy.Name
i = i + 1
End If
End If
Next ctrlDummy

For ii = 0 To i - 1
MsgBox MyArray(ii)
Next

End Sub


Regards,
Greg
-----Original Message-----
Hi,

I have three checkboxes on my userform ("Userform") chk1,
chk2, and chk3. I would like to record the name(s) of the
checked checkbox(es) in a dynamic array ("MyArray").
For some reason my current code below doesn't recognise
that the checkbox has been checked. It is also interesting
to note that the ".Value" method for ctrlDummy does not
show up when typing the code...maybe this could be the
reason?
 
G

Guest

Hi Greg

Thanks for your suggestion. Unfortunately it still fails to recognise the checkbox is checked

I'm thinking that it's because the ctrlDummy doesn't have a Value method, and hence the ctrlDummy.Value isn't actually going to the checkbox's value

So to get around this, is it possible to create another variable like so

--------------------------------
Dim chkDummy as MSForms.Checkbo
--------------------------------

and then set the chkDummy to the ctrlDummy checkbox

I was thinking along the lines of

----------------------------------------------------
For Each ctrlDummy In Userform.Control

If TypeOf ctrlDummy Is MSForms.CheckBox The

****Set chkDummy = CtrlDummy***

If chkDummy.Value = True The
ReDim Preserve MyArray(i
MyArray(i) = chkDummy.Nam
i = i +
End I

End I

Next ctrlDumm
----------------------------------------------------

The "****" line is causing errors. What would be the correct code to achieve this

Thanks very much

SuperJas.
 
G

Greg Wilson

Hi SuperJas,

The code I posted worked for me when run from a userform
assigned to the userform Click event. I don't know how
you're executing the code. It might be a version
dependent issue - i.e. MSForms.Checkbox may not be
recognized. I suggest using the TypeName function
instead. The following code worked for me when assigned
to a command button used to close the userform:

Private Sub CommandButton1_Click()
Dim ctrlDummy As Control
Dim MyArray() As String
Dim i As Integer, ii As Integer
i = 0
For Each ctrlDummy In Me.Controls
If TypeName(ctrlDummy) = "CheckBox" Then
If ctrlDummy.Value = True Then
ReDim Preserve MyArray(i)
MyArray(i) = ctrlDummy.Name
i = i + 1
End If
End If
Next ctrlDummy
For ii = 0 To i - 1
MsgBox MyArray(ii)
Next
Unload Me
End Sub

Regards,
Greg
-----Original Message-----
Hi Greg,

Thanks for your suggestion. Unfortunately it still fails
to recognise the checkbox is checked.
I'm thinking that it's because the ctrlDummy doesn't have
a Value method, and hence the ctrlDummy.Value isn't
actually going to the checkbox's value?
 
G

Guest

Hi Greg

I've found the problem with the checkboxes - I unloaded the userform *before* assigning values to my array. So that's all fixed now. =

Thanks heaps for your help! Very much appreciated

SuperJas.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top