我有一个宏按钮分配给Excel表格末尾的形状。 它会打开一个用户表单,用于向表中添加数据。
问题是每个新条目的形状都不会向下移动,最终会出现在桌面上。
将Shape设置为与单元格一起移动,但是没有插入实际行,只是将数据添加到表格的末尾..
如何使用Excel-VBA使第一个空行移动?
答案 0 :(得分:0)
将此代码放在标准模块上,并在将数据写入工作表后,调用此代码将按钮(形状)放置到第一个空行。 根据您的要求进行更改。
Sub MoveButton()
Dim ws As Worksheet
Dim lr As Long
Dim buttonCell As Range
Dim shp As Shape
Set ws = Sheets("Sheet1") 'Sheet where shape is inserted
lr = ws.Cells(Rows.Count, 1).End(xlUp).Row + 1 'first empty row in column A
Set buttonCell = ws.Cells(lr, "A") 'setting the cell where button will be placed
Set shp = ws.Shapes("myShape") 'name of the shape is "myShape", change it as per your requirement
'set the shape dimensions
With shp
.Left = buttonCell.Left
.Top = buttonCell.Top
End With
End Sub