我有一个excel宏,要求用户使用.GetOpenFilename
选择文件。用户选择文件后,宏将为该文件创建超链接。我希望宏完成的下一步是使用所选文件在宏所在的摘要表中创建链接。我现在已经设置好了,如果我预先确定了文件名,它就可以工作,但是每次都不同,因此我想使用用户选择的文件。用户选择的文件来自模板,我已经命名了需要链接到模板中以使其更容易的单元格。我的代码如下,我认为我需要用适当的代码替换[my_Filename]
。
Dim rngDestin0 As Range
Dim rngDestin1 As Range
Dim rngDestin2 As Range
Dim rngDestin3 As Range
Dim my_FileName As String
Dim fPath As String, fName As String
fPath = Application.GetOpenFilename("Excel Files (*.xlsm), *.xlsm")
fName = Split(fPath, Application.PathSeparator)(UBound(Split(fPath,
Application.PathSeparator)))
my_FileName = Application.GetOpenFilename("Excel Files (*.xlsm), *.xlsm")
Set rngDestin0 = Range("SO_Number").Resize(1).Offset(-1)
With Range("A1").Hyperlinks
.Delete
.Add Anchor:=rngDestin0, Address:=fPath, TextToDisplay:=fName
End With
'SALES ORDER NUMBER LINK
Set rngDestin1 = Range("SO_Number").Resize(1).Offset(-1)
rngDestin1.Select
ActiveCell.FormulaR1C1 = "='[my_FileName]tab1'!S_O_N"
'JOB NUMBER LINK
Set rngDestin2 = Range("A").Resize(1).Offset(-1)
rngDestin2.Select
ActiveCell.FormulaR1C1 = "='[my_FileName]tab1'!Job_Num"
'START DATE LINK
Set rngDestin3 = Range("B").Resize(1).Offset(-1)
rngDestin3.Select
ActiveCell.FormulaR1C1 = "='[my_FileName]tab1'!Job_Start"
答案 0 :(得分:0)
您已经在变量my_FileName
中选择了他们选择的文件,因此可以根据需要将其连接到每个字符串中:
ActiveCell.Formula = "='[" & my_FileName & " ]tab1'!Job_Start"