正如标题所说,我想删除给定单元格位置的单元格顶部的按钮。
到目前为止,这是我尝试过的,但没有成功。
Dim r As Range 'range is already given from another function call
Dim butns As button
With Sheet1.Buttons
butns.Top = r.Top
butns.Left = r.Left
End With
Sheet1.Buttons(butns).Delete
我的尝试是获取单元格“top”和“left”值并找到与该值关联的按钮,然后将其删除。
非常感谢任何帮助。谢谢!
答案 0 :(得分:2)
我认为你能真正找到按钮位于单元格顶部的唯一方法是查看按钮的中心是否包含在单元格的维度中。
Dim r As Range
Dim s As Shape
Dim i As Long
Dim cx As Double, cy As Double
For i = 1 To Sheet1.Shapes.Count
Set s = Sheet1.Shapes(i)
cx = s.Left + s.Width / 2
cy = s.Top + s.Height / 2
If cx >= r.Left And cx <= r.Left + r.Width And _
cy >= r.Top And cy <= r.Top + r.Height _
Then
s.Delete
Exit For
End If
Next
答案 1 :(得分:1)
表格控制按钮 - 用户3221162 13分钟前
这是你在尝试的吗?
注意循环遍历形状时,务必识别.Type
和.FormControlType
以确保您只删除表单按钮控件,否则它将删除任何符合条件的形状。
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim shp As Shape
'~~> Change this to the relevant worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
'~~> Loop through shapes
For Each shp In ws.Shapes
'~~> Check if it is a form control
If shp.Type = 8 Then
'~~> Check if it is a button
If shp.FormControlType = xlButtonControl Then
'~~> Check the cell. Taking B2 as example
If shp.TopLeftCell.Address = "$B$2" Then shp.Delete
End If
End If
Next
End Sub
答案 2 :(得分:0)
我建议您在形状的名称(Button_1_1,Button_2_1等)中添加一些信息,然后在需要时拆分出坐标。 通过它,您可以跟踪创建的位置。
协调转换通常比预期更棘手。你需要照顾缩放,DPI和其他东西。