将多个文件拖放到用户窗体 [英] Drag and drop multiple files to Userform

查看:107
本文介绍了将多个文件拖放到用户窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Userform上拖放多个文件以获取其路径。我通过

I am trying to drag and drop more than one file on a Userform to get their paths. I managed it with one file thanks to this.

当我将 FilePath = Data.files(1)更改为 FilePath = Data.Files(2)(i),我收到一条消息 Table Expected。我应该创建一个表并重新对其进行修改吗?

When I change FilePath = Data.files(1) to FilePath = Data.Files(2) or (i), I get a message "Table Expected". Should I create a Table and Redim it?

到目前为止,我的工作是

My work so far:

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

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

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


推荐答案

感谢@ R.Roe的评论,我设法做到了自己想要的:

Thanks to @R.Roe's comment, I managed to do what I wanted :

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

干杯!

这篇关于将多个文件拖放到用户窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆