Consulting

Results 1 to 13 of 13

Thread: Range " does not contain" - code

  1. #1

    Range " does not contain" - code

    HI,
    I'm trying to write a code for my macro and I can't figure out how to do this properly.
    I would like to obtain sth like that:

    If "selected cells area" "does not contain" "checkbox or option button" then...

    Anyone help?

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Get the TopLeftCell property of each shape and check if it intersects your range
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    This is what I have right now ( not working)
    Dim RngOff As Range, Ob
    
    For Each Ob In ActiveSheet.OptionButtons
                 If Application.Intersect(Ob.TopLeftCell, RngOff) Is Nothing 
          Then 
                 MsgBox ("***x")
    
    Next ob
    
    For Each Ob In ActiveSheet.OptionButtons
         If Application.Intersect(Ob.TopLeftCell, RngOff) Is Nothing 
         Then
         MsgBOX ("aaa")

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Either an adaptation of something I did for you elsewhere:
    Sub check2()
    Dim RngClrBut As Range
    somethingfound = False
    Set RngClrBut = Selection
    For Each Ob In ActiveSheet.CheckBoxes
      If Not Application.Intersect(Ob.TopLeftCell, RngClrBut) Is Nothing Then
        somethingfound = True
        Exit For
      End If
    Next Ob
    If Not somethingfound Then
      For Each Ob In ActiveSheet.OptionButtons
        If Not Application.Intersect(Ob.TopLeftCell, RngClrBut) Is Nothing Then
          somethingfound = True
          Exit For
        End If
      Next Ob
    End If
    If Not somethingfound Then MsgBox "No optionbutton or check box in this selection"
    End Sub
    or:
    Sub check3()
    somethingfound = False
    For Each shp In ActiveSheet.Shapes
      If shp.Type = msoFormControl Then
        If shp.FormControlType = xlOptionButton Or shp.FormControlType = xlCheckBox Then
          If Not Intersect(Selection, shp.TopLeftCell) Is Nothing Then
            somethingfound = True
            Exit For
          End If
        End If
      End If
    Next shp
    If Not somethingfound Then MsgBox "No optionbutton or check box in this selection"
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    Yup that works perfect , Thanks p45cal.
    btw. cool idea with "somethingfound"

  6. #6
    I've made a combination of both codes you helped me with:

    Sub check2()
        Dim RngClrBut As Range, ob
        somethingfound = False
        
        If Selection.Count > 0 Then
            Set RngClrBut = Selection.SpecialCells(xlVisible)
            For Each ob In ActiveSheet.CheckBoxes
                If Not Application.Intersect(ob.TopLeftCell, RngClrBut) Is Nothing Then
                    ob.Value = False
                    somethingfound = True
                End If
                Exit For
         End If
        Next ob
        If Not somethingfound Then
            For Each ob In ActiveSheet.OptionButtons
                If Not Application.Intersect(ob.TopLeftCell, RngClrBut) Is Nothing Then
                    ob.Value = False
                    somethingfound = True
                    
                    Exit For
                End If
            Next ob
        End If
        If Not somethingfound Then MsgBox "No optionbutton or check box in this selection"
    End Sub
    But something is wrong.... When there is a couple of checkboxes within the selected range it's either: only one of them gets unchecked or I get an error "End if without block if"
    Help Please.

  7. #7
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    try:
    Sub check2()
    Dim RngClrBut As Range, ob
    somethingfound = False
    
    If Selection.Count > 0 Then
      Set RngClrBut = Selection.SpecialCells(xlVisible)
      For Each ob In ActiveSheet.CheckBoxes
        If Not Application.Intersect(ob.TopLeftCell, RngClrBut) Is Nothing Then
          ob.Value = False
          somethingfound = True
        End If
      Next ob
    End If
    If Not somethingfound Then
      For Each ob In ActiveSheet.OptionButtons
        If Not Application.Intersect(ob.TopLeftCell, RngClrBut) Is Nothing Then
          ob.Value = False
          somethingfound = True
        End If
      Next ob
    End If
    If Not somethingfound Then MsgBox "No optionbutton or check box in this selection"
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  8. #8
    Thanks again p45cal, now everything is OK

  9. #9
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Actually, take out the red lines below too:
    If Not somethingfound Then
    For Each ob In ActiveSheet.OptionButtons
    If Not Application.Intersect(ob.TopLeftCell, RngClrBut) Is Nothing Then
    ob.Value = False
    somethingfound = True
    End If
    Next ob
    End If
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  10. #10
    Already did that

  11. #11
    Actually there still is one issue...
    Everything works just fine only if I select 2 cells or more.
    If I select only one then it doesn't matter which cell is selected - macro unchecks all OptionButtons and checkboxes on the worksheet.

    Current code is:
    Private Sub CommandButton41_Click()
       Dim RngClrBut As Range, ob
        SomethingFound = False
         
        If Selection.Count > 0 Then
            Set RngClrBut = Selection.SpecialCells(xlVisible)
            For Each ob In ActiveSheet.CheckBoxes
                If Not Application.Intersect(ob.TopLeftCell, RngClrBut) Is Nothing Then
                    ob.Value = False
                    SomethingFound = True
                End If
            Next ob
        
            For Each ob In ActiveSheet.OptionButtons
                If Not Application.Intersect(ob.TopLeftCell, RngClrBut) Is Nothing Then
                    ob.Value = False
                    SomethingFound = True
                End If
            Next ob
        End If
        If Not SomethingFound Then MsgBox "No optionbutton or check box in this selection"
    End Sub
    Please help anyone.
    Last edited by Pawel; 07-25-2016 at 10:16 AM.

  12. #12
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Ah yes, if there's only one cell selected then the .SpecialCells assumes you want to process the whole sheet!
    Try:
    Private Sub CommandButton41_Click()
    Dim RngClrBut As Range, ob
    SomethingFound = False
    
    If Selection.Count > 0 Then
      If Selection.Count = 1 Then
        Set RngClrBut = Selection
      Else
        Set RngClrBut = Selection.SpecialCells(xlVisible)
      End If
      For Each ob In ActiveSheet.CheckBoxes
        If Not Application.Intersect(ob.TopLeftCell, RngClrBut) Is Nothing Then
          ob.Value = False
          SomethingFound = True
        End If
      Next ob
    
      For Each ob In ActiveSheet.OptionButtons
        If Not Application.Intersect(ob.TopLeftCell, RngClrBut) Is Nothing Then
          ob.Value = False
          SomethingFound = True
        End If
      Next ob
    End If
    If Not SomethingFound Then MsgBox "No optionbutton or check box in this selection"
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  13. #13
    Works perfect now.
    Thank you once again p45cal

Posting Permissions

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