Board logo

標題: [發問] VBA請益如何選取多檔後上傳至目的檔 [打印本頁]

作者: rouber590324    時間: 2018-5-30 08:05     標題: VBA請益如何選取多檔後上傳至目的檔

Dear ALL 大大:
1.如圖一目前可以選取單一檔案成功上傳,
2.但是改成多檔後上傳會沒有動作,
2.1目前單檔選曲寫法:
      source = Application.GetOpenFilename
2.2目前多檔選取寫法:
      'source = Application.GetOpenFilename(MultiSelect:=True)
3.請教如何修改為多檔後上傳至目的檔.煩不吝賜教. THANKS

圖一
Sub test()
Dim folder As String '資料
Dim source As String '來源檔
Dim dest As String '目的檔
Dim p As Integer
Dim s As Integer
Dim i As Long

folder = "D:\ABC" '存取目的位置

source = Application.GetOpenFilename '來源

If source = "False" Then: MsgBox "上傳失敗,請選取正確來源檔案。": Exit Sub

p = 1
i = 1
Do Until p = 0
p = InStr(i, source, "\", 1)
If p = 0 Then Exit Do
s = p
i = p + 1
Loop

dest = folder & Mid(source, s, Len(source))

If Dir(dest) <> "" Then
MsgBox msg1
Else
FileCopy source, dest
MsgBox source & " 已上傳。"
End If
Exit Sub
End Sub
作者: naruto018    時間: 2018-5-30 13:14

回復 1# rouber590324

參考看看
  1. Sub test()
  2. Dim folder As String '資料
  3. Dim source ' As String '來源檔
  4. Dim source2, source3, source4 As String
  5. Dim dest As String '目的檔
  6. Dim p As Integer
  7. Dim s As Integer
  8. Dim i As Long

  9. folder = "D:\ABC" '存取目的位置

  10. source = Application.GetOpenFilename(MultiSelect:=True)  '來源

  11. If Not IsArray(source) Then: MsgBox "上傳失敗,請選取正確來源檔案。": Exit Sub

  12. For Each source2 In source
  13.     source3 = Split(source2, "\")
  14.     source4 = source3(UBound(source3))
  15.         
  16.     dest = IIf(Right(folder, 1) = "\", folder, folder & "\") & source4
  17.    
  18.     If Dir(dest) <> "" Then
  19.     MsgBox msg1
  20.     Else
  21.     FileCopy source2, dest
  22.     MsgBox source2 & " 已上傳。"
  23.     End If
  24. Next
  25. End Sub
複製代碼

作者: rouber590324    時間: 2018-5-31 08:31

DEAR naruto018  大大
感謝解惑  THANKS*10000




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)