如题所述
1、首先打开Excel数据表,在开发工具中打开VBA编辑器。
2、在单元格区域当中输入一些内容。
3、在VBA编辑器中插入模块。
4、Sub获取文件路径()Dim filess, tempfiless=Application.GetOpenFilename(, , "打开文件")If filess=False Then MsgBox "你未选择文件,程序即将退出!": Exit Subtemp=Split(filess, "\")。
5、MsgBox "你选择的《" & temp(UBound(temp)) & "》的路径为:" & Chr(10) & filess _
& Chr(10) & "当前表路径为:" & Chr(10) & ThisWorkbook.FullName, vbokonty+64,"返回路径"End Sub。在模块当中输入代码,然后运行。
6、然后单击“执行”按钮,程序弹出“选择文件”对话框。选择一个文件后,程序立即弹出所选文件的路径,然后弹出当前工作簿的路径。
温馨提示:答案为网友推荐,仅供参考
第1个回答 推荐于2017-09-15
发给你参考一下,网上搜的
Sub getpath()
Range("A2:D1000").ClearContents
On Error Resume Next
Dim shell As Variant
Set shell = CreateObject("Shell.Application")
Set filePath = shell.BrowseForFolder(&O0, "选择文件夹", &H1 + &H10, "") '获取文件夹路径地址
Set shell = Nothing
If filePath Is Nothing Then
Exit Sub
Else
gg = filePath.Items.Item.Path
End If
Set obj = CreateObject("Scripting.FileSystemObject")
Set fld = obj.getfolder(gg)
For Each ff In fld.Files
m = m + 1
Cells(m + 1, 1) = ff.name
Cells(m + 1, 2) = ff.Path
Cells(m + 1, 3) = ff.Size
Cells(m + 1, 4) = ff.DateCreated
Next
End Sub本回答被提问者和网友采纳
Sub getpath()
Range("A2:D1000").ClearContents
On Error Resume Next
Dim shell As Variant
Set shell = CreateObject("Shell.Application")
Set filePath = shell.BrowseForFolder(&O0, "选择文件夹", &H1 + &H10, "") '获取文件夹路径地址
Set shell = Nothing
If filePath Is Nothing Then
Exit Sub
Else
gg = filePath.Items.Item.Path
End If
Set obj = CreateObject("Scripting.FileSystemObject")
Set fld = obj.getfolder(gg)
For Each ff In fld.Files
m = m + 1
Cells(m + 1, 1) = ff.name
Cells(m + 1, 2) = ff.Path
Cells(m + 1, 3) = ff.Size
Cells(m + 1, 4) = ff.DateCreated
Next
End Sub本回答被提问者和网友采纳
第2个回答 2017-02-15
Dim ActivePath as String
ActivePath = ActiveWorkBook.Path
ActivePath = ActiveWorkBook.Path