【发布时间】:2016-09-21 00:54:16
【问题描述】:
我创建了一个 VBA 用户表单,通过操作我从网站上找到的代码,让同事将文件从一个列表框中的选定文件夹传输到第二个列表框中的另一个文件夹。列表框中的文件夹每天都在变化。它适用于带有 fmSingleSelect 的两个列表框,但我无法弄清楚如何使用第二个列表框上的 fmMultiSelect 属性正确运行它(是的,我在第二个列表框上将属性更改为 fmMultiSelect)。
能够多选项目文件夹并同时运行传输将节省时间。
下面是单选的代码,并注释掉了我为多选使用的一些代码
代码下方还有一张图片
谢谢
Private Sub CmdBtn_transfer_Click()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim FileExt As String
Dim Value As String
Dim i As Integer
FromPath = "C:\Users\us-lcn-dataprep03\Desktop\Production files\" & (Me.ListBox1) '<< Change
ToPath = "\\bego.hb\MED_PRODUCTION\USA_Datapreparation\" & (Me.ListBox2) '<< Change
' For i = 0 To ListBox2.Items.Count - 1
' If ListBox2.Items(i).Selected = True Then
' Val = ListBox2.Items(i).Value
' End If
'Next i
FileExt = "*.sli*" '<< Change
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
If FSO.FolderExists(ToPath) = False Then
MsgBox ToPath & " doesn't exist"
Exit Sub
End If
FSO.CopyFile Source:=FromPath & FileExt, Destination:=ToPath
MsgBox "You can find the files from " & FromPath & " in " & ToPath
End Sub
【问题讨论】:
-
您是否尝试将所有文件从 一个 目录复制到 多个 目标目录?
-
它当前将所有 .sli 文件从源文件夹复制到目标文件夹。我想将文件从源文件夹复制到多个选定的目标文件夹
标签: excel listbox multi-select userform vba