我正在尝试获取一个可以分配给命令按钮的宏。我有多个按钮可以打开不同的文件,因此在每个单元格中我都包含不同的文件路径。
目前,我的命令按钮正在查找特定的单元格引用并打开该值。 有什么方法可以让宏来查找它对齐的单元格中的值吗?
我目前正在使用两个宏 - 一个用于创建按钮,另一个用于分配按钮。我不得不为每个按钮创建一个新的宏。
创建按钮的宏......
子按钮()
Dim i As Long
Dim lRow2 As Integer
Dim shp As Object
Dim dblLeft As Double
Dim dblTop As Double
Dim dblWidth As Double
Dim dblHeight As Double
With Sheets("Print Schedule")
dblLeft = .Columns("A:A").Left 'All buttons have same Left position
dblWidth = .Columns("A:A").Width 'All buttons have same Width
For i = Range("E65536").End(xlUp).Offset(1, 0) To ActiveCell + 15
dblHeight = .Rows(i).Height 'Set Height to height of row
dblTop = .Rows(i).Top 'Set Top top of row
Set shp = .Buttons.Add(dblLeft, dblTop, dblWidth, dblHeight)
shp.Characters.Text = "Open Print Schedule"
Next i
End With
End Sub
要打开文件的宏......
Sub Mendip()
Dim myfile As String
myfile = Cells(6, 6).Value
Application.Workbooks.Open Filename:=myfile
End Sub
请告诉我有更好的方法来做到这一点!
答案 0 :(得分:3)
当您创建如下所示的表单按钮时,您可以为它们分配一个公共宏
你可以像这样分配一个宏
Sub Sample()
Dim shp As Shape
Set shp = ActiveSheet.Shapes(Application.Caller)
'MsgBox shp.TopLeftCell.Address
Select Case shp.TopLeftCell.Address
Case "$A$1"
'~~> Do Something
Case "$B$1"
'~~> Do Something
'
'~~> And So on
'
End Select
End Sub
修改强>:
有一件事我忘了提及。要将“Sample”宏指定给所有按钮,请在创建形状后添加以下行。
shp.OnAction = "Sample"