命令按钮以打开文件-位于同一单元格按钮中的文件路径与 [英] Command button to open file - file path located in the same cell button is aligned to
问题描述
我正在尝试获取一个可以分配给命令按钮的宏.我有多个按钮可以打开不同的文件,因此在每个单元格中我都包含一个不同的文件路径.
I'm trying to get a single macro that I can assign to my command buttons. I have multiple buttons that open different files so in each cell I include a different file path.
当前,我的命令按钮正在查找特定的单元格引用并打开该值.有什么方法可以让宏在它所对齐的单元格中查找值?
Currently my command buttons are looking for a specific cell reference and opening that value. Is there any way I can get the macro to look for the value in the cell to which it is aligned?
此刻我正在使用两个宏-一个用于创建按钮,然后另一个用于分配给按钮.我必须为每个按钮创建一个新的宏.
I'm using two macros at the moment - one to create the buttons and then another to assign to the buttons. I am having to create a new macro for each 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 to open file...
Sub Mendip()
Dim myfile As String
myfile = Cells(6, 6).Value
Application.Workbooks.Open Filename:=myfile
End Sub
请告诉我有一种更好的方法!
Please tell me there is a better way to do this!
推荐答案
当您创建如下所示的表单按钮时,可以为其分配一个公共宏
When you create the form buttons as shown below then you can assign a common macro to them
您可以分配一个这样的宏
And you can assign a macro like this
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
编辑:
我忘了提到的一件事.要将示例"宏分配给所有按钮,请在创建形状后添加以下行.
One thing I forgot to mention. To assign the "Sample" macro to all buttons, Add the below line after you create the shape.
shp.OnAction = "Sample"
这篇关于命令按钮以打开文件-位于同一单元格按钮中的文件路径与的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!