+ Reply to Thread
Results 1 to 9 of 9

Cell Link assignment for check boxes

Hybrid View

  1. #1
    Forum Expert
    Join Date
    11-27-2007
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    1,669

    Cell Link assignment for check boxes

    Is there a way to create a check box, assign cell link,
    then copy the check box multiple times and have the cell link follow in a relative fashion.
    In other words all subsequent check boxes would have different cell links.
    modytrane

  2. #2
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394

    Re: Cell Link assignment for check boxes

    I doubt it. I am pretty sure there is no referential relationship between an object you add to your sheet and the sheet itself. How would it know which cell to reference to when it was copied?

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

    Re: Cell Link assignment for check boxes

    Hello Modytrane,

    I revised this macro I wrote a back in March. This macro will automatically add a CheckBox to each cell you have selected. When it starts, you will be asked if you want to add a macro and a linked cell to the check boxes. The same macro will be run by each check box if you choose to add one. The link cell will update based on Excel's absolute and relative referencing for A1 style entries. You can link to another worksheet when prompted by including the sheet with the range, like this...
    [code]
    Sheet2!B10
    'Written: March 17, 2009
    'Updated: April 17, 2009 - Added Linked Cell with Relative or Absolute addressing
    'Author:  Leith Ross
    'Summary: Adds Form CheckBoxes to the selected cells and assign the macro "CheckBoxMacro"
    '         to each CheckBox. CheckBoxes are sized automatically to fit the cell.
    
    Private Sub AddCheckBoxToCell(Ref_cell As Range, ByVal Macro_Name As String, ByRef Link_Cell As Range)
        
      Dim ChkBox As CheckBox
      Dim N As Double
      Dim Wks As Worksheet
      
        With Ref_cell.Cells(1, 1)
          refLeft = .Left
          refTop = .Top
          refHeight = .Height
        End With
          
          Set Wks = Worksheets(Ref_cell.Parent.Name)
          Set ChkBox = Wks.CheckBoxes.Add(10, 10, 15, 12)
          
          N = (refHeight - ChkBox.Height) / 2#
          
        With ChkBox
          .Caption = Caption
          .Top = refTop + N
          .Left = refLeft
            If Not Link_Cell Is Nothing Then
               .LinkedCell = Link_Cell.Address
            Else
              .LinkedCell = ""
            End If
          .OnAction = Macro_Name
        End With
        
    End Sub
    
    Public Sub AddCheckBoxesToSelection()
    
      Dim AbsC As Boolean
      Dim AbsR As Boolean
      Dim Answer As String
      Dim C As Long
      Dim LinkCell As Range
      Dim MacroName As String
      Dim R As Long
      Dim RefCell As Range
               
        MacroName = InputBox("Do you want the Check Box to run a macro?" & vbCrLf _
                  & "If so, enter the macro's name below.", "Add a Macro")
        
    EnterLink:
        Answer = InputBox("Do you want to add a Linked Cell?" & vbCrLf _
               & "If so, enter the cell address below in A1 style." & vbCrLf _
               & "Relative and Absolute address rules apply.", _
               "Add a Link Cell")
               
        If Answer <> "" Then
           On Error GoTo BadRange
             Set LinkCell = Range(Answer)
           On Error GoTo 0
           AbsC = Answer Like "*[$][A-Za-z]*"
           AbsR = Answer Like "*[$][0-9]*"
        End If
        
          For Each RefCell In Selection
            On Error GoTo BadMacro
            AddCheckBoxToCell RefCell, MacroName, LinkCell.Offset(R, C)
            If (Not AbsC) Then
               C = C + 1
            End If
            If (Not AbsR) Then
               R = R + 1
            End If
            If C > Columns.Count Or R > Rows.Count Then GoTo NoMore
          Next RefCell
          
        Exit Sub
        
    BadMacro:
        MsgBox "There is problem with the Macro to be assigned." & vbCrLf _
             & "Please correct the error and try again." & vbCrLf & vbCrLf _
             & "'" & MacroName & "' - " & Err.Description
        Exit Sub
    
    BadRange:
        MsgBox "You have entered a Bad Range or Address." & vbCrLf _
             & "Please enter it again.", vbInformation
        Err.Clear
        On Error GoTo 0
        GoTo EnterLink
        
    NoMore:
        MsgBox "Aborting - No more Check Boxes can be added to the Sheet.", vbCritical
        
    End Sub
    Adding the Macro
    1. Copy the macro above pressing the keys CTRL+C
    2. Open your workbook
    3. Press the keys ALT+F11 to open the Visual Basic Editor
    4. Press the keys ALT+I to activate the Insert menu
    5. Press M to insert a Standard Module
    6. Paste the code by pressing the keys CTRL+V
    7. Make any custom changes to the macro if needed at this time.
    8. Save the Macro by pressing the keys CTRL+S
    9. Press the keys ALT+Q to exit the Editor, and return to Excel.

    To Run the Macro...
    To run the macro from Excel, open the workbook, and press ALT+F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Forum Expert
    Join Date
    11-27-2007
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    1,669

    Re: Cell Link assignment for check boxes

    Leith,
    The macro works well, thank you.
    I am not sure about the size of the check box though.
    It seems like the size is same regardless of the cell size.
    I have another thread for that issue, but since you mentioned that your macro addresses the size, I thought I would ask you here.
    I'll make sure to close the other thread if it gets addressed here.

    Thanks again,
    modytrane.

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

    Re: Cell Link assignment for check boxes

    Hello moodytrane,

    When I first wrote the macro, it was to add a check box without a caption that would match the cell's height. Tell me what you need and I'll modify the code for you.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Cell Link assignment for check boxes

    Modytrane, are you familiar with Marlett checkboxes? They are easy to implement in code, and are on the same layer as the rest of the data. Here's an example:

    In the sheet module:
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
            ' change range as desired
            Marlett Target, Range("A1:B65536"), True, 6 ' make checked cells yellow
    
        ' other double-click code goes here
    
    End Sub
    In a code module:
    Sub Marlett(cell As Range, r As Range, _
                Optional bChangeColor As Boolean, _
                Optional iCheck As XlColorIndex = xlColorIndexNone, _
                Optional iNoCheck As XlColorIndex = xlColorIndexNone)
                
        With cell
            If .Count > 1 Or Intersect(.Cells, r) Is Nothing Then Exit Sub
            If IsEmpty(.Value) Then
                .Value = "a"
                .Font.Name = "Marlett"
                If bChangeColor Then .Interior.ColorIndex = iCheck
            Else
                .ClearContents
                If bChangeColor Then .Interior.ColorIndex = iNoCheck
            End If
        End With
    End Sub
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Cell Link assignment for check boxes

    escobf,

    Unfortunately you need to post your question in a new thread, it's against the forum rules to post a question in the thread of another user. If you create your own thread, any advice will be tailored to your situation so you should include a description of what you've done and are trying to do. Also, if you feel that this thread is particularly relevant to what you are trying to do, you can surely include a link to it in your new thread.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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