Keep certain text from showing in textbox

JayH4

New Member
Joined
Apr 19, 2017
Messages
26
Good evening,

I have the following code in a userform to automatically take what the operator has selected from a choice of checkboxes and puts the caption value in a textbox. However I have other checkboxes in the userform and it is picking up all of the checkboxes. Is there a way to make it only pick up the captions of check boxes that are within its own frame and not the entire userform. The frame where the checkboxes are located is called CallSign.
Code:
Private Sub WriteToTextBox()
    'Call Sign Textbox
    Dim c As Control
    Dim s As String
    
    For Each c In Me.Controls 'Loop through all controls
        If TypeName(c) = "CheckBox" Then 'Ensure control is a checkbox
            If c.Value = True Then 'We know it's a checkbox so check if it has been ticked
                s = IIf(s = "", c.Caption, s & ", " & c.Caption) 'Populate the string variable
               ' Me.txcallsign.Text.Replace("Protective Services Staff  Dispatched", "").Trim()
            End If
        End If
    Next c
    
    frcallsign.txcallsign = s 'write string to textbox
End Sub

I attempted to use a "Replace" however it did not work

Thank you for any help
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try...

Code:
[FONT=Arial][COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] WriteToTextBox()
    [COLOR=green]'Call Sign Textbox[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] c [COLOR=darkblue]As[/COLOR] Control
    [COLOR=darkblue]Dim[/COLOR] s [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    
    [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] c [COLOR=darkblue]In[/COLOR] Me.frCallSign.Controls [COLOR=green]'Loop through all controls[/COLOR]
        [COLOR=darkblue]If[/COLOR] TypeName(c) = "CheckBox" [COLOR=darkblue]Then[/COLOR] [COLOR=green]'Ensure control is a checkbox[/COLOR]
            [COLOR=darkblue]If[/COLOR] c.Value = [COLOR=darkblue]True[/COLOR] [COLOR=darkblue]Then[/COLOR] [COLOR=green]'We know it's a checkbox so check if it has been ticked[/COLOR]
                [COLOR=darkblue]If[/COLOR] c.Caption <> "Protective Services Staff Dispatched" [COLOR=darkblue]Then[/COLOR]
                    s = s & ", " & c.Caption [COLOR=green]'Populate the string variable[/COLOR]
                [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    [COLOR=darkblue]Next[/COLOR] c
    
    s = Trim(Mid(s, 3))
    
    Me.frCallSign.txCallSign = s [COLOR=green]'write string to textbox[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR][/FONT]

Hope this helps!
 
Upvote 0
I walked away from my screen for a bit and had an idea
I came back to my computer and tried it and it worked...then I came back here and saw that you had answered my question.
So thank you very much...I basically did the same thing
Code:
Private Sub WriteToTextBox()    'Call Sign Textbox
    Dim c As Control
    Dim s As String
    
    For Each c In Me.CallSign.Controls 'Loop through all controls
        If TypeName(c) = "CheckBox" Then 'Ensure control is a checkbox
            If c.Value = True Then 'We know it's a checkbox so check if it has been ticked
                s = IIf(s = "", c.Caption, s & ", " & c.Caption) 'Populate the string variable
            End If
        End If
    Next c
    
    frcallsign.txcallsign = s 'write string to textbox
End Sub

Thank you none the less
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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