+ Reply to Thread
Results 1 to 15 of 15

Uncheck checkbox if condition is met

Hybrid View

  1. #1
    Registered User
    Join Date
    07-12-2013
    Location
    mexico
    MS-Off Ver
    Excel 2010
    Posts
    18

    Uncheck checkbox if condition is met

    Hello everyone!

    I need a macro that unchecks a Checkbox1 when a A1 contains "-"

    I guessing something like this but it doesn't work:

    Sub Example1()
    
    If Range("A1").Value = "-" Then
    
    Me.CheckBox1.Value = False
    
    
    Else
    Me.CheckBox1.Value = True
    
    End If
    
    End Sub
    Last edited by barqujo; 08-07-2013 at 05:15 PM.

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Uncheck checkbox if condition is met

    Sub Example1()
    
    If Range("A1").Value = "-" Then
    
    Me.CheckBox1.Checked = False
    
    
    Else
    Me.CheckBox1.Checked = True
    
    End If
    
    End Sub
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  3. #3
    Registered User
    Join Date
    07-12-2013
    Location
    mexico
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Uncheck checkbox if condition is met

    Thanks XeRo Solus!

    However it tells me Invalid use of Me keyword

    What is the correct syntax?

  4. #4
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Uncheck checkbox if condition is met

    Use me.checkbox1 if the macro is saved in a UserForm module. Where did you save the code?

    Are you using userform controls or ActiveX controls?

  5. #5
    Registered User
    Join Date
    07-12-2013
    Location
    mexico
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Uncheck checkbox if condition is met

    I saved it in a new module like this

    Private Sub Example1()
    
    If Range("A1").Value = "-" Then
    
    Me.CheckBox1.Checked = False
    
    
    Else
    Me.CheckBox1.Checked = True
    
    End If
    
    End Sub

    I want to have the macro there to be able to launch it when other conditions are met

  6. #6
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Uncheck checkbox if condition is met

    If you're going to save it in a module and run it from somewhere else then change me to the name of your userform...

    like userform1.checkbox1

    However, if you're going to call this sub from somewhere else you'll need to remove private from the name of the sub.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Uncheck checkbox if condition is met

    Where is the checkbox located?
    If posting code please use code tags, see here.

  8. #8
    Registered User
    Join Date
    07-12-2013
    Location
    mexico
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Uncheck checkbox if condition is met

    The checkbox is in the spreadsheet

  9. #9
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Uncheck checkbox if condition is met

    The syntax you have is for a userform. If your checkbox is on your spreadsheet its either a standard (form) control or an activeX control and will have to be coded differently.

    Would you like to upload an example file to see if you we can get you where you want to go?

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Uncheck checkbox if condition is met

    XeroSolus

    Me can also refer to a worksheet.

    So if there was an ActiveX checkbox, CheckBox1, on a worksheet this would be valid in the worksheet's module
    MsgBox Me.CheckBox1.Value

  11. #11
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Uncheck checkbox if condition is met

    The OP saved the code above in a new module. I was going off this assuming the code would be called from somewhere else.

    Maybe I'm mistaken but I thought me.checkbox would only be valid if they code was saved in the worksheet module.

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Uncheck checkbox if condition is met

    I think the OP needs to clarify things.

    As well as the questions you've asked, how do they want the code to actually run?

  13. #13
    Registered User
    Join Date
    07-12-2013
    Location
    mexico
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Uncheck checkbox if condition is met

    Ok, I will clarify things!

    The CheckBox is an ActiveX Control, If I uncheck it manually, it hides row 8 with the following code:

    Private Sub CheckBox1_Click()
    If Me.CheckBox1.Value = True Then
       ActiveSheet.Rows("8:8").Hidden = False
    
    Else
       ActiveSheet.Rows("8:8").Hidden = True
       End If
    End Sub
    Now, appart from that, there will be another cell (a dropdownbox) with several values. One of these values is "-" . When this value is selected in this dropdown box, I want Checkbox1 to automatically uncheck so that row 8 will hide.

    My question is, how do I refer to the checkbox in the conditional?? Me.CheckBox.Value is not working, and also in which module should it be?

  14. #14
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Uncheck checkbox if condition is met

    Try saving in the sheet module of the sheet your checkbox is on and try again.

  15. #15
    Registered User
    Join Date
    07-12-2013
    Location
    mexico
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Uncheck checkbox if condition is met

    Ok I did it!! I pasted the conditional in the sheet code as you told me

    Worked with If Range Me.CheckBox3.Value not .Checked though

    
    If Range("L10").Value = "-" Then
    
        Me.CheckBox1.Value = False
    
    
        Else
        Me.CheckBox1.Value = True
    
        End If

    Thanks a lot!! Wouldn't have able to do it without your help

+ 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. [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
  2. [SOLVED] CheckBox Uncheck
    By Anto_BT in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-24-2012, 10:15 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 on form won't uncheck
    By beckstei in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-07-2007, 01:55 PM
  5. Check and uncheck a checkbox
    By NoProgram in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-25-2006, 05:50 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