[英]Excel VBA: open fixed file path but not fixed file name
I want to write a code that will open the file path and within this folder the user can select his own folder. 我想编写一个将打开文件路径的代码,并且用户可以在此文件夹中选择自己的文件夹。 I only seem able to find a " general" code to open a folder. 我似乎只能找到打开文件夹的“ 常规”代码。
With wbtarget.Sheets("Data")
strPathName = Application.GetOpenFilename()
If strPathName = "False" Then
Exit Sub
End If
Set wbsource = Workbooks.Open(strPathName, 0)
.Range("A1:AL10000").Value = wbsource.Sheets(1).Range("A1:AL10000").Value
wbsource.Close (False)
End With
or open a specific file. 或打开特定文件。
folder_path = CStr("C:\Users\peter\Documents\me")
file_name = CStr("report.xlsm")
StrResource = folder_path & "\" & file_name
thank you for your help. 谢谢您的帮助。
I use this function to let the user browse for a folder instead of a file. 我使用此功能让用户浏览文件夹而不是文件。 I don't remember where I found it. 我不记得在哪里找到它。
It uses the Shell.BrowseForFolder method. 它使用Shell.BrowseForFolder方法。
Set InitPath to a string <> "", eg "D:\\Files", to limit the folder tree the user can choose from. 将InitPath设置为字符串<>“”,例如“ D:\\ Files”,以限制用户可以选择的文件夹树。
Public Function GetFolderName(Caption As String, InitPath As String) As String
Dim AppShell As Object
Dim BrowseDir As Variant
Dim sPath As String
Set AppShell = CreateObject("Shell.Application")
If InitPath <> "" Then
Set BrowseDir = AppShell.BrowseForFolder(0, Caption, &H11, (InitPath))
Else
' 17 = root folder is "My Computer"
Set BrowseDir = AppShell.BrowseForFolder(0, Caption, &H11, 17)
End If
sPath = ""
On Error Resume Next
sPath = BrowseDir.Items().Item().Path
GetFolderName = sPath
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.