【VBA】-添加文件路径以便于后面程序调用Source File或Template File


1.插入按钮1,选择对应执行的macro->Select_file

2.插入按钮2,选择对应执行的macro->Temp_file

Sub Select_file()
    fileselection ActiveSheet.Range("b3")
End Sub
Sub Temp_file() fileselection ActiveSheet.Range("b4") End Sub Public Sub fileselection(ByVal rng As Range) Dim wjdz, ini, i, iadd ini = ThisWorkbook.Path If rng.Cells <> "" Then iadd = Trim(rng.Cells.Text) For i = Len(iadd) To 1 Step -1 If Mid(iadd, i, 1) = "\" Then ini = Left(iadd, i - 1) Exit For End If Next End If With Application.FileDialog(msoFileDialogFilePicker) .InitialFileName = ini .AllowMultiSelect = False If .Show = -1 Then wjdz = .SelectedItems(1) '确定所选文件地址 Else MsgBox ("no file selected") wjdz = ini End If End With rng.Cells = wjdz End Sub