+ Reply to Thread
Results 1 to 8 of 8

how to use if condition to check whether checkbox is checked

  1. #1
    Registered User
    Join Date
    04-26-2016
    Location
    singapore
    MS-Off Ver
    Office xp
    Posts
    20

    how to use if condition to check whether checkbox is checked

    I have created two checkbox from the control toolbox and a form button, the checkbox is place beside each row of data. When the button clicked while the checkbox is checked, that row of data is formatted into the body of the email. I tried using this code that I found on online, but it shows run-time error '1004'.
    Please Login or Register  to view this content.
    This is my code
    Please Login or Register  to view this content.
    Last edited by Bronx; 05-12-2016 at 01:54 AM.

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: how to use if condition to check whether checkbox is checked

    What's 'l'? Where is it declared or assigned a value, or do you really have checkboxes named 'CheckBox-3' & 'CheckBox-4'?

  3. #3
    Registered User
    Join Date
    04-26-2016
    Location
    singapore
    MS-Off Ver
    Office xp
    Posts
    20

    Re: how to use if condition to check whether checkbox is checked

    Thanks for your reply cytop. The 'l' is where the checkbox are, so there is a checkbox at "L3" and another at "L4".
    I got the code from http://stackoverflow.com/questions/30502202/send-email-if-check-box-is-checked"]here

  4. #4
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    861

    Re: how to use if condition to check whether checkbox is checked

    Hi there!
    ActiveSheet.OLEObjects("Checkbox" & l - 3).Object.Value should contain the name of the checkbox object not its position.
    So in your example if the checkbox name is "Checkbox1" then the code should look like
    Please Login or Register  to view this content.
    hth.
    .. and don't forget to have fun!
    Bogdan.

    mark SOLVED and Add Reputation if my answer pleases you

  5. #5
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: how to use if condition to check whether checkbox is checked

    You are using an undeclared variable called 'l' (upper case for clarity - 'L')

    As it is undeclared, it will assume a value of 0 or 'Empty' depending on how it is used. The iine
    Please Login or Register  to view this content.
    will force l to a value of 0 so that line is looking for a checkbox named 'Checkbox-3'

    You need to replace the calculated name with the actual name of the checkbox. You can get this by right clicking on the chckbox and looking at the Name box to the left of the Formula bar above the worksheet and edit the line like
    Please Login or Register  to view this content.
    Where 'Checkbox1' is the actual name of the checkbox.

    There are 2 lines to edit.

    To prevent errors like this you should use the
    Please Login or Register  to view this content.
    directve at the top of each code module. This will raise a compile error if there are any undeclared variables. See http://www.cpearson.com/excel/declaringvariables.aspx for details.

  6. #6
    Registered User
    Join Date
    04-26-2016
    Location
    singapore
    MS-Off Ver
    Office xp
    Posts
    20

    Re: how to use if condition to check whether checkbox is checked

    Thanks bulina that fixed the error.

  7. #7
    Registered User
    Join Date
    04-26-2016
    Location
    singapore
    MS-Off Ver
    Office xp
    Posts
    20

    Re: how to use if condition to check whether checkbox is checked

    Quote Originally Posted by cytop View Post
    You are using an undeclared variable called 'l' (upper case for clarity - 'L')

    As it is undeclared, it will assume a value of 0 or 'Empty' depending on how it is used. The iine
    Please Login or Register  to view this content.
    will force l to a value of 0 so that line is looking for a checkbox named 'Checkbox-3'

    You need to replace the calculated name with the actual name of the checkbox. You can get this by right clicking on the chckbox and looking at the Name box to the left of the Formula bar above the worksheet and edit the line like
    Please Login or Register  to view this content.
    Where 'Checkbox1' is the actual name of the checkbox.

    There are 2 lines to edit.

    To prevent errors like this you should use the
    Please Login or Register  to view this content.
    directve at the top of each code module. This will raise a compile error if there are any undeclared variables. See http://www.cpearson.com/excel/declaringvariables.aspx for details.
    Thanks for the advice and help!

  8. #8
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    861

    Re: how to use if condition to check whether checkbox is checked

    I'm glad I could help. Thanks for the rep point.
    Cytop is right you know.. You should use variable declaration to avoid this kind of errors in the future.
    The mode to enable variable declaration requirement is to check the -=Tools-Option-[Editor] tab - Require Variable Declaration=- checkbox in the VBA Menu.
    ...don't forget to have fun!

+ 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. How to check if excel file is Checked Out in SharePoing Check Out, run code, Check In
    By jrtraylor in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-08-2016, 04:36 PM
  2. Replies: 1
    Last Post: 09-24-2015, 11:50 AM
  3. Check Box that shows options in Combo Box if Checked, and Hides if Not Checked
    By dumbjodie in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-13-2013, 10:06 AM
  4. [SOLVED] When checkbox is checked it does not copy to new sheet. Only if not checked using false
    By thelisa in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-28-2013, 07:59 AM
  5. Automatically check one or more check boxes when a parent check box is manually checked
    By Steverizer in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-28-2013, 01:56 PM
  6. [SOLVED] VBA code to uncheck checkbox 2 & 3 if checkbox 1 is checked
    By hydz1213 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 02-25-2013, 03:10 AM
  7. Replies: 2
    Last Post: 12-19-2012, 11:23 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