返回列表 上一主題 發帖

[發問] VBA可以做到設計彈出式窗讓使用者點選檔案以匯入的功能嗎?

[發問] VBA可以做到設計彈出式窗讓使用者點選檔案以匯入的功能嗎?

請教各位前輩,目前小弟匯入.TXT檔都是要點選"資料-取得外部資料-從文字檔"(2007)來匯入,之後再進行分割。以下為部分錄製出來的內容。
QueryTables.Add(Connection:="TEXT;C:\Users\Administrator\Desktop\Excel練習\test.txt", Destination:=Range("$A$1"))
若此檔案放在別的路徑,以上code就不能用了。
請問是否能藉由VBA來實現跳出視窗讓使用者自行選擇檔案?
謝謝!!

回復 11# shakeass

貼文失敗重貼一帖sorry
    請教各位高手大大
小弟想要使用選擇檔案 並將選擇之檔案 複製到d:\test 資料夾裡
但是 FILECOPY "來源","D:\test"     的來源想用變數取代 卻一直出現錯誤
可以麻煩高手幫忙看一下需要如何修改才能符合順利跑下去
Private Sub CommandButton1_Click()

With Application.FileDialog(msoFileDialogOpen)
   .AllowMultiSelect = True
   .Show
   For i = 1 To .SelectedItems.Count
     Cells(i, 1) = .SelectedItems(i)
     MsgBox .SelectedItems(i)
     fs = .SelectedItems(i)

       fileCopy(fs,"d:\test")
   Next
End With
End Sub

TOP

回復 9# Hsieh


請教各位高手大大
小弟想要使用選擇檔案 並將選擇之檔案 複製到d:\test 資料夾裡
但是 FILECOPY "來源","D:\test"     的來源想用變數取代 卻一直出現錯誤
可以麻煩高手幫忙看一下需要如何修改才能符合順利跑下去
Private Sub CommandButton1_Click()

With Application.FileDialog(msoFileDialogOpen)
   .AllowMultiSelect = True
   .Show
   For i = 1 To .SelectedItems.Count
     Cells(i, 1) = .SelectedItems(i)
     MsgBox .SelectedItems(i)
     fs = .SelectedItems(i)

       ==> Private Sub CommandButton1_Click()

With Application.FileDialog(msoFileDialogOpen)
   .AllowMultiSelect = True
   .Show
   For i = 1 To .SelectedItems.Count
     Cells(i, 1) = .SelectedItems(i)
     MsgBox .SelectedItems(i)
     fs = .SelectedItems(i)

       fileCopy(fs,"d:\test")
   Next
End With
End SubfileCopy(fs,"d:\test")
   Next
End With
End Sub
感激

TOP

謝謝Hsieh版大~~!!

TOP

回復 8# yafeii
  1. Sub nn()
  2. Dim FileToOpen
  3. FileToOpen = Application _
  4.     .GetOpenFilename("Text Files (*.txt), *.txt", , , , True)
  5. If IsArray(FileToOpen) Then
  6.     MsgBox Join(FileToOpen, Chr(10))
  7.     ElseIf FileToOpen <> False Then
  8.     MsgBox FileToOpen
  9. End If
  10. End Sub
複製代碼
學海無涯_不恥下問

TOP

謝謝Hsieh大,請問一定要用Application.FileDialog(msoFileDialogOpen),不能用GetOpenFilename嗎?謝謝!!

TOP

學海無涯_不恥下問

TOP

請教高手們,如果希望在彈出式窗讓使用者同時點選多個檔案匯入
時應如何設定呢?謝謝!!

TOP

回復 4# oobird

謝謝oobird的回覆,原來使用GetOpenFilename方法就可以了。
感嗯!

TOP

Dim Fname As String
Fname = Application.GetOpenFilename("文字文件,*.txt")
If len(Fname)>0  Then

TOP

        靜思自在 : 好事要提得起,是非要放得下,成就別人即是成就自己。
返回列表 上一主題