返回列表 上一主題 發帖

請問如何用VBA開啟檔案總管視窗讀取Folder或Files的路徑or檔名

請問如何用VBA開啟檔案總管視窗讀取Folder或Files的路徑or檔名

Dear 各位大大,

請問大大有沒有辦法讓user 在Excel裡面 按下Button 然後就會看到檔案總管視窗(類似存檔畫面的視窗),然後指另一個路徑。
User 選取檔案夾或檔案,然後可以複製該檔案 或 檔案夾的 路徑 or 檔名。

選好後路徑or檔名~  會自動複製到某存格上。

謝謝各位厲害的大大~!!

目前我只知道可以打開視窗而已  網路上查到的是這樣  

Fname = Application.GetOpenFilename
還有檢視檔案 用MsgBox顯示
Dim NextFile As String
   Dim AllFiles As String

   AllFiles = ""
   NextFile = Dir("C:\", 0)

   While NextFile  <> ""
      AllFiles = AllFiles & Chr(13) &  NextFile
      NextFile  = Dir
   Wend

   MsgBox AllFiles

謝謝各位大大~

  1. Private Sub CommandButton1_Click()
  2. With Application.FileDialog(msoFileDialogOpen)
  3.    .AllowMultiSelect = True
  4.    .Show
  5.    For i = 1 To .SelectedItems.Count
  6.      Cells(i, 1) = .SelectedItems(i)
  7.      MsgBox .SelectedItems(i)
  8.    Next
  9. End With
  10. End Sub
複製代碼
學海無涯_不恥下問

TOP

回復 2# Hsieh


謝謝版主~
請問後半段
我不太懂,我想知道如果是要指定貼回路徑到某一儲存格 該怎麼寫呢?

謝謝版主大大!!
  1. For i = 1 To .SelectedItems.Count
  2.      Cells(i, 1) = .SelectedItems(i)
  3.      MsgBox .SelectedItems(i)
  4.    Next
  5. End With
  6. End Sub
複製代碼

TOP

回復 3# webbliu

在上面的程式裡就有貼到儲存格裡的功能囉.

Cells(i, 1) = .SelectedItems(i)

就是將所選的檔名依序貼到 A1  A2  A3 ... 的儲存格內,

Cells(a, b)
a 為列號  1,2,3,4...
b 為欄號 A,B,C,D...

所以 Cells(i, 1) 就表示 A1 儲存格.

TOP

回復 4# luhpro


    謝謝大大~ 我完全明白了!!

TOP

回復 2# Hsieh


    謝謝版主的指導!!

TOP

大師:
     如果我要開啟的路徑是: D\文件檔\附屬檔案
請問程式要怎麼修改?

TOP

回復 7# 佩玄
Private Sub CommandButton1_Click()
    With Application.FileDialog(msoFileDialogOpen)
        .InitialFileName = "D\文件檔\附屬檔案"
        .AllowMultiSelect = True
        .Show
        For i = 1 To .SelectedItems.Count
            Cells(i, 1) = .SelectedItems(i)
            MsgBox .SelectedItems(i)
        Next
    End With
End Sub

TOP

Shell "explorer D:\文件檔\附屬檔案"

TOP

OK~~感謝~~

TOP

        靜思自在 : 人要自愛,才能愛普天下的人。
返回列表 上一主題