首页 > 解决方案 > 单元格中心的 Excel 复选框

问题描述

是否可以将复选框框移动到单元格的中心?我正在创建复选框。复选框本身位于左侧,文本位于右侧。我正在删除文本,所以只剩下“复选框”。我希望单元格的整个区域都是“可点击的”。因此,如果我调整字段大小,可点击区域会更小。有没有办法保持复选框的大小与单元格大小相同并将复选框框移动到中间/中心?就像文本一样,有按钮可以将文本定位在左侧、中间和右侧?

在此处输入图像描述

标签: excelcheckboxpositioncenter

解决方案


很简单。您必须获取要使复选框居中的单元格的TopLeft位置。所以...

Option Explicit

Sub CenterMyCheckbox()
    Dim myCheckbox As Shape
    Set myCheckbox = ActiveSheet.Shapes("Check Box 1")

    Dim cbCell As Range
    Set cbCell = ActiveSheet.Range("B3")

    '--- this just positions the checkbox even with the top, left of the cell
    myCheckbox.left = cbCell.left
    myCheckbox.top = cbCell.top

    '--- this centers the checkbox vertically
    Dim cellHeight As Double
    Dim cbHeight As Double
    cellHeight = cbCell.Height
    cbHeight = myCheckbox.Height
    myCheckbox.top = cbCell.top + (cellHeight / 2#) - (cbHeight / 2#)
End Sub

推荐阅读