+ Reply to Thread
Results 1 to 5 of 5

VBA to add Checkboxes Based on Condition in Another Range

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-18-2014
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2013
    Posts
    197

    VBA to add Checkboxes Based on Condition in Another Range

    I'm needing to add a condition to the following code, but I just can't get the syntax right. I need it to insert checkboxes into column F only when there is data in Column A.

    The code in bold is my attempt to add the condition. What am I doing wrong?



    
    Option Explicit
    Sub InsertCheckboxes()
    
     
    Sub Test()
    
    Dim LastRow As Long
    LastRow = Cells(Rows.Count, 8).End(xlUp).Row
    
    Dim myBox As CheckBox
    Dim myCell As Range
    
    Dim Last As Long
    Dim i As Integer
    Dim cellRange As Range
    Dim cboxLabel As String
    Dim linkedColumn As String
    
        Set cellRange = Range("F4:F" & LastRow & "")
    
        linkedColumn = "Z"
    
        cboxLabel = ""
    
        With ActiveSheet
            For Each myCell In cellRange
    
            For i = Last To 1 Step -1
                If (Cells(i, "A").Value) <> "" Then
    
                    With myCell
                        Set myBox = .Parent.CheckBoxes.Add(Top:=.Top, _
                                                           Width:=.Width, Left:=.Left, Height:=.Height)
        
                        With myBox
                            .LinkedCell = linkedColumn & myCell.Row
                            .Caption = cboxLabel
                            .Name = "checkbox_" & myCell.Address(0, 0)
                            .OnAction = "Mixed_State"
                        End With
        
                        .NumberFormat = ";;;"
                    
                    End With
                End If
            Next i
            
            Next myCell
        End With
    
    
    End Sub

    thanks!!
    Last edited by phelbin; 07-29-2016 at 06:54 PM.

  2. #2
    Forum Contributor
    Join Date
    02-18-2014
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2013
    Posts
    197

    Re: VBA to add Checkboxes Based on Condition in Another Range

    I changed

    For i = Last To 1 Step -1
    to

    For i = LastRow To 1 Step -1

    I'm not getting error messages anymore. But now it's placing checkboxes in every cell in column F, not just in the ones with data in column A, the way I need it to.

  3. #3
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA to add Checkboxes Based on Condition in Another Range

    Hi phelbin

    Try this revised Code...

    Option Explicit
    Sub InsertCheckboxes()
    
    
       'Sub Test()
    
       Dim LastRow      As Long
       LastRow = Cells.Find("*", Cells(Rows.Count, Columns.Count), SearchOrder:=xlByRows, _
                            SearchDirection:=xlPrevious).Row
    
    
       'LastRow = Cells(Rows.Count, 8).End(xlUp).Row
    
       Dim myBox        As CheckBox
       Dim myCell       As Range
    
       'Dim Last As Long
       Dim i            As Integer
       Dim cellRange    As Range
       Dim cboxLabel    As String
       Dim linkedColumn As String
    
       Set cellRange = Range("F4:F" & LastRow & "")
    
       linkedColumn = "Z"
    
       cboxLabel = ""
    
       With ActiveSheet
          For Each myCell In cellRange
    
             '        For i = LastRow To 1 Step -1
             If (Cells(myCell.Row, "A").Value) <> "" Then
    
                With myCell
                   Set myBox = .Parent.CheckBoxes.Add(Top:=.Top, _
                                                      Width:=.Width, Left:=.Left, Height:=.Height)
    
                   With myBox
                      .LinkedCell = linkedColumn & myCell.Row
                      .Caption = cboxLabel
                      .Name = "checkbox_" & myCell.Address(0, 0)
                      .OnAction = "Mixed_State"
                   End With
    
                   .NumberFormat = ";;;"
    
                End With
             End If
             '        Next i
    
          Next myCell
       End With
    
    
    End Sub
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  4. #4
    Forum Contributor
    Join Date
    02-18-2014
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2013
    Posts
    197

    Re: VBA to add Checkboxes Based on Condition in Another Range

    Jaslake, it worked. Thanks so much!!

  5. #5
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA to add Checkboxes Based on Condition in Another Range

    You're welcome...glad I could help.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

+ 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] How to sum range based on condition
    By JackBauer in forum Excel General
    Replies: 4
    Last Post: 01-28-2016, 09:45 AM
  2. How would you set up a dynamic range based on this condition
    By unnobtanium in forum Excel General
    Replies: 17
    Last Post: 12-11-2014, 01:15 PM
  3. Checkboxes If Else condition..
    By siobeh in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-24-2014, 05:11 PM
  4. Highlighting a range from A:I based on condition
    By naga in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-15-2014, 04:46 AM
  5. Set Checkboxes to Checked/Unchecked in Range based on Values in Other Range
    By lowprofile in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-28-2013, 11:25 AM
  6. Print Sheets based on checkboxes in UserForm, Checkboxes also control another macro
    By krackaberr in forum Excel Programming / VBA / Macros
    Replies: 34
    Last Post: 03-05-2013, 11:12 AM
  7. Replies: 3
    Last Post: 08-11-2012, 09:57 AM

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