+ Reply to Thread
Results 1 to 13 of 13

Checkbox Can I do This

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-22-2008
    Location
    S-o-t
    MS-Off Ver
    Office 2000 & 2010
    Posts
    166

    Checkbox Can I do This

    I have a userform that contains 15 labels, each label has Numbers in it each time i click on a label the text is inserted into an excel spreadsheet. I use it as a fast input solution and works well and has done for a few years.

    I Now want to Add three checkboxes to the userform
    If checkbox One is ticked i want the letter "A" and the number in the label i choose to be inserted into the spreadsheet.
    Checkbox Two "B"
    Checkbox Three "c"

    I would only ever tick one of the checkboxes never two or three so it would be A, B or C

    This is the code for The Labels i use

    private Sub Label4_Click()
    Call InsertTextSelection(Label4.Caption, RGB(0, 0, 255), 0)
    End Sub
    
    Private Sub Label5_Click()
    Call InsertTextSelection(Label5.Caption, RGB(0, 0, 0), 0)
    End Sub
    and so on

    This is the Call Insert textselection

    Public Sub InsertTextSelection(text, colour, hours)
        UserForm1.Hide
        r = Selection.Row
        c = Selection.Column
        Range(Cells(r, c - 0), Cells(r, c - 0)).Select
        With Selection
            .ClearContents
            .Font.Size = 9
            .Font.Color = colour
            .MergeCells = True
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .Value = text
        End With
        
        End Sub
    The userform works by doubleclick

    Hope you Can help

    Dave
    Last edited by Dave69rock; 08-25-2008 at 06:14 PM. Reason: wrap code

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Dave69rock,

    Please remember to wrap your code using the Code tags.

    How to wrap your Code
    On the Message window Toolbar you will see the # icon. This will automatically wrap the text you selected with the proper VB tags to create a Code Window in your post. You can do this manually by placing the tag [code] at the start of the line, and the tag [/code] at the end.

    Sincerely,
    Leith Ross

  3. #3
    Forum Contributor
    Join Date
    08-22-2008
    Location
    S-o-t
    MS-Off Ver
    Office 2000 & 2010
    Posts
    166
    Didn't know still new to the site

    its done and will be in future

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Dave69rock,

    The easiest solution would be to declare a Public string variable to hold the letter. The Click event of each check box would set this variable to the proper letter (checked) or clear it (unchecked).

    Sincerely,
    Leith Ross

  5. #5
    Forum Contributor
    Join Date
    08-22-2008
    Location
    S-o-t
    MS-Off Ver
    Office 2000 & 2010
    Posts
    166
    Thanks leith my knowledge of vb isn't to that standard any chance of an example.

    Dave

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    You could change this line in InsertTextSelection
        .Value = Switch(CheckBox1.Value, "A", CheckBox2.Value "B", CheckBox3.Value, "C", True, vbNullstring ) & text
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Dave69rock,

    I'll need a little time to put together a sample workbook for you as a guide.

    Sincerely,
    Leith Ross

  8. #8
    Forum Contributor
    Join Date
    08-22-2008
    Location
    S-o-t
    MS-Off Ver
    Office 2000 & 2010
    Posts
    166
    Leith

    Wow this is great many thanks
    Dave

  9. #9
    Forum Contributor
    Join Date
    08-22-2008
    Location
    S-o-t
    MS-Off Ver
    Office 2000 & 2010
    Posts
    166
    Mikerickson thanks for your reply

    i can't seem to get it to work i replaced the Call InsertTextSelection with what you said.

    Dave

  10. #10
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    I expressed myself poorly. Leave the calls for the sub as they are and change the sub to
    Public Sub InsertTextSelection(text, colour, hours)
        UserForm1.Hide
        r = Selection.Row
        c = Selection.Column
        Range(Cells(r, c - 0), Cells(r, c - 0)).Select
        With Selection
            .ClearContents
            .Font.Size = 9
            .Font.Color = colour
            .MergeCells = True
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .Value = Switch(CheckBox1.Value, "A", CheckBox2.Value, "B", _
                          CheckBox3.Value, "C", True, vbNullstring ) & text
        End With
        
        End Su
    b

  11. #11
    Forum Contributor
    Join Date
    08-22-2008
    Location
    S-o-t
    MS-Off Ver
    Office 2000 & 2010
    Posts
    166
    Mikerickson

    Thank you so much i love how your code works.

    Dave

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. checkbox cell range change vba
    By novitaun in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-14-2008, 12:53 PM
  2. Checkbox and recalculate
    By lemonstar in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-24-2008, 11:31 AM
  3. How to have Checkbox A uncheck with checked Checkbox B
    By Texas Aggie 09 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-23-2007, 09:29 AM
  4. Checkbox - Date
    By bmasella in forum Excel General
    Replies: 1
    Last Post: 05-09-2007, 12:57 PM
  5. text box input and checkbox on user form
    By raw in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-05-2007, 05:27 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1