Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 在更改行高或列宽时,是否有方法动态更改复选框的大小和位置?_Excel_Vba - Fatal编程技术网

Excel 在更改行高或列宽时,是否有方法动态更改复选框的大小和位置?

Excel 在更改行高或列宽时,是否有方法动态更改复选框的大小和位置?,excel,vba,Excel,Vba,我有一个工作表,用来记录参与者的注册情况。我使用一组复选框和组合框来允许用户选择一个程序并选择相关的测试轮次。组合框和复选框是使用按钮单击事件创建的,它确保每个组件的位置和高度与它所在的单元完全对齐 问题是,用户可能会选择更改列宽和行高,以将数据放入特定的单元格中。对于附加注释列,这一点尤其正确。这些更改将取消复选框和组合框与其各自单元格的对齐 我已经尝试过使用sheet.Change事件,但该函数仅在单元格内容更改时引发中断。其他属性(如行高和列宽)不受监控。当发生此类更改时,用户必须手动移动

我有一个工作表,用来记录参与者的注册情况。我使用一组复选框和组合框来允许用户选择一个程序并选择相关的测试轮次。组合框和复选框是使用按钮单击事件创建的,它确保每个组件的位置和高度与它所在的单元完全对齐

问题是,用户可能会选择更改列宽和行高,以将数据放入特定的单元格中。对于附加注释列,这一点尤其正确。这些更改将取消复选框和组合框与其各自单元格的对齐

我已经尝试过使用sheet.Change事件,但该函数仅在单元格内容更改时引发中断。其他属性(如行高和列宽)不受监控。当发生此类更改时,用户必须手动移动组合框和复选框并调整其大小,这非常繁琐,会降低工作表的显示质量

当行高发生变化时发生中断时,可以使用以下VBA代码:

private sub Row_Height_Change(ByVal Target As Range)
   'Special interrupt function

   Dim Row_Counter As Integer
   'Declare a row counter variable to store the row number

   Dim Row_Counter As Integer 
   'Declare a column counter variable to store the column number

   Row_Counter = 'Retrieve information from the interrupt
   Column_Counter = 'We know which column the CheckBox is found in

   Dim CheckBoxName As String

   CheckBoxName = "CB Test Round " & CStr(Row_Counter)
   'Name CheckBoxes in "Create Participant" macro using their position

   Column_Letter = Split(Cells(Row_Counter, Column_Counter).Address, "$")(1)

   Set ws = ActiveSheet
   Set TestRound1_CheckBox = ws.CheckBoxes(CheckBoxName)
   'Set the checkbox in question
   Set Cell_1 = ws.Cells(CStr(Row_Counter), Column_Letter) 
   'Find the cell where the checkbox is located

   With TestRound1_CheckBox
       .Left = Cell_1.Left + (Cell_1.Width / 2)
       .Top = Cell_1.Top + (Cell_1.Height / 2)
       .Right = ...

   End With

问题是我找不到这样的打断。非常感谢您的帮助。

要使任何形状与放置它们的单元格保持对齐,请将它们的“放置”属性设置为xlMove-在这种情况下,即使调整大小,它们也会留在单元格内。无需处理任何调整大小事件。在Excel中,可以通过右键单击控件,选择“格式控件”、“属性”,然后选择“移动但不移动大小”来设置此属性。但您也可以使用VBA来实现这一点

此代码将为激活图纸的所有形状设置属性:

Dim sh as Shape
For Each sh In activeSheet.Shapes
    sh.Placement = xlMove
Next sh
或者只是为了你的复选框

Dim sBox As CheckBox
For Each sBox In ws.CheckBoxes
    sBox.Placement = xlMove
Next sBox

如果通过代码创建控件,则在创建控件时应直接设置属性

你不能保护床单吗?谢谢你的回复SJR。我当然可以保护图纸,但我不想限制用户更改行高度以满足其需要的能力。这会使表单变得不那么友好。谢谢您的评论!您提出的解决方案可行,但并不完美。复选框的移动很笨拙,并不总是完全跟随单元格尺寸的变化。单元格尺寸一旦更改,是否无法自动移动复选框并使其居中?