命令button打开文件 – 位于同一个单元格button的文件path被alignment

我试图得到一个我可以分配给我的命令button的macros。 我有多个button,打开不同的文件,所以在每个单元格我包括一个不同的文件path。

目前我的命令button正在寻找特定的单元格引用并打开该值。 有没有什么办法可以让macros查找它alignment的单元格中的值?

我现在使用两个macros – 一个是创buildbutton,另一个是分配给button。 我不得不为每个button创build一个新的macros。

macros创buildbutton…

子button()

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 

结束小组

macros打开文件…

 Sub Mendip() Dim myfile As String myfile = Cells(6, 6).Value Application.Workbooks.Open Filename:=myfile End Sub 

请告诉我有一个更好的方法来做到这一点!

如下所示创build表单button时,可以为它们分配一个公共macros

在这里输入图像说明

你可以像这样分配一个macros

在这里输入图像说明

 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 

编辑

有一件事我忘了提及。 要将“示例”macros分配给所有button,请在创build形状后添加下面的行。

 shp.OnAction = "Sample"