为啥两个单元格合成一个单元格后会使后面批量插入的复选框发生偏移?

这个是一个单元格插入的

这个是合并单元格插入的

这个其实是位置慢慢往上移的,数据有点少看不出来,下列是插入代码

Sub CreateCheckBoxes()

Dim ws As Worksheet

Dim cell As Range

Dim chkBox As CheckBox

Dim i As Long

Set ws = ActiveSheet

i = 0 '计数器

For Each cell In ws.Range("F3:F660")

cell.Font.Color = cell.Interior.Color

If cell.Row Mod 2 = 1 Then

cell.Value = False

'添加复选框

With ws.CheckBoxes.Add(cell.Left, cell.Top, cell.Width, cell.Height)

.Caption = "已完成"

.LinkedCell = cell.Address

.Name = "CheckBox_" & cell.Address(False, False)

Set chkBox = .TopLeftCell.Parent.CheckBoxes(.Name)

End With

'如果需要更精确,可以再调整一次

chkBox.Top = cell.Top + 5

chkBox.Left = cell.Left + 5

'输出单元格和复选框位置信息

Debug.Print "单元格地址: " & cell.Address

Debug.Print "单元格位置 - 左: " & cell.Left & ", 上: " & cell.Top & _

", 宽: " & cell.Width & ", 高: " & cell.Height

Debug.Print "复选框位置 - 左: " & chkBox.Left & ", 上: " & chkBox.Top & _

", 宽: " & chkBox.Width & ", 高: " & chkBox.Height

Debug.Print "链接单元格: " & chkBox.LinkedCell

Debug.Print "----------------------------------------"

i = i + 1

End If

Next cell

Debug.Print "操作完成!共创建了 " & i & " 个复选框,已精确对齐。"

End Sub

广东省
浏览 536
收藏
1
分享
1 +1
5
+1
全部评论 5
 
恰同学少年
工作表单元格内容发生改变,重置复选框位置: function Workbook_SheetChange(Sh, rg) { let sheet = Application.Workbooks.Item(1).Sheets.Item(1); console.log(Sh.Name) if(Sh.Name == sheet.Name){ resetCheckBoxPos() } }
· 黑龙江省
回复
 
恰同学少年
/*重置复选框位置*/ function resetCheckBoxPos(){ console.clear(); console.log(Date.now() + "重置位置") let sheet = Application.Workbooks.Item(1).Sheets.Item(1); let shapes = sheet.Shapes for(let shape of shapes){ shape.Top = shape.TopLeftCell.MergeArea.Top shape.Left = shape.TopLeftCell.MergeArea.Left shape.Height = shape.TopLeftCell.MergeArea.Height shape.Width = shape.TopLeftCell.MergeArea.Width } }
· 黑龙江省
回复
 
恰同学少年
合并单元格区域,插入复选框 function test(row,col){ let sheet = Application.Workbooks.Item(1).Sheets.Item(1); let shapes = sheet.Shapes //let row = 2; //let col = 6; let cell = sheet.Cells.Rows.Item(row).Columns.Item(col); let lb = shapes.AddFormControl(xlCheckBox, cell.Left,cell.Top, cell.MergeArea.Width, cell.MergeArea.Height) lb.ControlFormat.Enabled = true; lb.ControlFormat.LinkedCell = "=$C$1"; lb.ControlFormat.LockedText = false; lb.TextFrame2.TextRange.Text = "测试文本"; }
· 黑龙江省
回复
 
会飞的鱼
会飞的鱼

KVP函数专家、创作者俱乐部成员

https://bbs.wps.cn/topic/78068 具体代码可以参考这个帖子
· 河北省
回复
 
会飞的鱼
会飞的鱼

KVP函数专家、创作者俱乐部成员

这是WPS的一个bug,可以加一个辅助表,把对象插入到辅助表的A1单元格,然后复制辅助表的A1单元格 粘贴到目标表
· 河北省
回复