将多个文件拖放到Userform

时间:2018-01-10 15:17:45

标签: excel vba drag-and-drop

我正在尝试在Userform上拖放多个文件以获取其路径。由于this,我用一个文件管理它。

当我将FilePath = Data.files(1)更改为FilePath = Data.Files(2)(i)时,我收到一条消息“预期的表格”。我应该创建一个表并将其重新划分吗?

到目前为止我的工作:

此作业完成(打开文件并将其复制到选定的工作表中)

Dim Wb, FilePath As String
Dim WbIni, WbCib As Workbook

Private Sub CommandButton2_Click()

If FilePath = vbNullString Then
    MsgBox "Aucun fichier n'a été importé", vbCritical, "Anomalie"
    Unload UserForm1
    Exit Sub
End If

Set WbCib = Workbooks.Open(Filename:=FilePath)
MsgBox WbCib.Name

i = WbCib.ActiveSheet.Range("A1").End(xlDown).Row
WbCib.ActiveSheet.Range("A1:A" & i).Copy
ActiveSheet.Paste Destination:=WbIni.Worksheets("Target").Range("A1:A" & i)
WbIni.Sheets("Target").Activate
WbCib.Close

Unload UserForm1

End Sub

这个初始化我的TreeView进行拖放

Private Sub UserForm_Initialize()
Wb = ThisWorkbook.Name
Set WbIni = ActiveWorkbook

TreeView1.OLEDropMode = ccOLEDropManual
End Sub

这个给了我文件路径。我想我需要循环它。

Private Sub TreeView1_OLEDragDrop(Data As MSComctlLib.DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single)

    FilePath = Data.Files(1)
    Workbooks(Wb).Activate

    MsgBox FilePath

End Sub

1 个答案:

答案 0 :(得分:1)

感谢@ R.Roe的评论,我设法做了我想要的事情:

Dim x, y As Integer
Dim PathTable As String
Dim FilePath As Variant

Private Sub TreeView1_OLEDragDrop(Data As MSComctlLib.DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single)
Dim i As Integer

Workbooks(Wb).Activate

'Counting file paths I dropped
For Each FilePath In Data.Files()
    i = i + 1
Next FilePath

'Redim my table
ReDim PathTable(i)

i = 1

'Adding data to my table
For x = 0 To UBound(PathTable) - 1
    PathTable(x) = Data.Files(i)
    i = i + 1
Next x

'Just to make sure it works
For x = 0 To UBound(PathTable) - 1
    MsgBox PathTable(x)
Next x

End Sub

干杯!