【问题标题】:Insert a Checkbox into every cell and assign it to that cell在每个单元格中插入一个复选框并将其分配给该单元格
【发布时间】:2020-02-04 20:30:55
【问题描述】:

我需要为链接到该单元格的每个单元格添加一个复选框。选中时返回 true,未选中时返回 false 到分配给它的单元格中。

工作表有数千个单元格,当我手动插入它们时,我意识到必须有更好的解决方案。

我正在处理的工作表如下所示:

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    给你,克林顿。

    Sub AddCheckBoxes()
    
    Dim cb As CheckBox
    Dim myRange As Range, cel As Range
    Dim wks As Worksheet
    
    Set wks = Sheets("mySheet") 'adjust sheet to your needs
    
    Set myRange = wks.Range("A1:A10") ' adjust range to your needs
    
    For Each cel In myRange
    
        Set cb = wks.CheckBoxes.Add(cel.Left, cel.Top, 30, 6) 'you can adjust left, top, height, width to your needs
    
    
        With cb
    
            .Caption = ""
            .LinkedCell = cel.Address
    
        End With
    
    Next
    
    End Sub
    

    【讨论】:

    • 你是男人,谢谢你为我节省了 8 多个小时的繁琐工作!
    • @ClintonWarren,作为一个仅供参考,我发布这个是因为我很容易获得它。但是,谷歌搜索会在大约 3-5 分钟内得到相同类型的答案。
    • 这很好用。非常感谢我只有一个建议有没有办法让每个单元格的单元格链接移动和调整大小?我必须检查数百个需要调整大小并随单元格移动的复选框。我仍然喜欢这个解决方案,谢谢
    • @user2533460 -> 如果您使用智能感知,我敢打赌您可以找到标记移动和大小属性的属性。或者在更糟糕的情况下,您可以使用宏记录器来获取正确的代码。
    • 这有点像作弊。复选框实际上并不在单元格中,只是漂浮在它们之上。遗憾的是,Excel 并不真正支持绑定到单元格的真正控件。看起来这将是一个非常基本的功能......
    【解决方案2】:

    这是我用来向所有选定单元格添加居中复选框的通用 VBA 宏:

    'ActiveSheet.DrawingObjects.Delete ' optional to delete all shapes when testing
    Dim c As Range, cb As CheckBox
    
    For Each c In Selection
        Set cb = c.Worksheet.CheckBoxes.Add(c.Left + c.Width / 2 - 8.25, _
                c.Top + c.Height / 2 - 8.25, 0, 0)  ' 8.25 is cb.Height / 2
        cb.Text = vbNullString                      ' to clear Caption
        cb.LinkedCell = c.Address(0, 0)             ' Example A1 instead of $A$1
        cb.Name = "cb" & cb.LinkedCell              ' optional
    Next
    
    Selection.NumberFormat = ";;;" ' optional to hide the cell values
    Selection = True ' optional to check all at once (or 'selection = [#N/A]' for all xlMixed)
    

    【讨论】:

      猜你喜欢
      • 2015-04-17
      • 2017-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多