简体   繁体   English

使用VBA将1300个随机25个文件复制到另一个文件夹

[英]Copy random 25 files from 1300 to another folder with VBA

I have 1300 excel files on a server, with revenues in them. 我在服务器上有1300个excel文件,其中包含收入。 I need to compare these revenues with one pivot file to make sure the revenues are the same in the actual 2 files. 我需要将这些收入与一个数据透视文件进行比较,以确保实际2个文件中的收入相同。 Because it is on a server, opening all of them from a server would be pretty slow, thats why I want to copy a sample of them (25 excel files) to my compter first, and then run my comparison macro from this folder. 因为它在服务器上,所以从服务器打开所有文件都非常慢,这就是为什么我要首先将它们的一个样本(25个excel文件)复制到我的计算机中,然后从该文件夹运行比较宏的原因。 But I want to make the copying process automatized, so I somehow need to select randomly 25 of these files, and then copy it to an other folder. 但是我想使复制过程自动化,因此我需要以某种方式随机选择这些文件中的25个,然后将其复制到另一个文件夹中。 I have a code to copy all of the files from one folder to another, but I need the random selection to it. 我有一个代码可以将所有文件从一个文件夹复制到另一个文件夹,但是我需要对其进行随机选择。 Thanks. 谢谢。

 Sub Copy_Folder()

 Dim FSO As Object
    Dim FromPath As String
    Dim ToPath As String
FromPath = "C:\Users\NagyI2\Documents\Macro testing"
ToPath = "C:\Users\NagyI2\Documents\Copy test"

If Right(FromPath, 1) = "\" Then
    FromPath = Left(FromPath, Len(FromPath) - 1)
End If

If Right(ToPath, 1) = "\" Then
    ToPath = Left(ToPath, Len(ToPath) - 1)
End If

Set FSO = CreateObject("scripting.filesystemobject")

If FSO.FolderExists(FromPath) = False Then
    MsgBox FromPath & " doesn't exist"
    Exit Sub
End If

FSO.CopyFolder Source:=FromPath, Destination:=ToPath

End Sub

The files -collection of a folder -object gives a list of files in that folder. files - folder集合-对象给出了该files folder中的文件列表。 However, you cannot access to one of the files by index, just by name. 但是,您不能按索引访问文件之一,只能按名称访问。 So the following code creates first an array with the names of all files. 因此,以下代码首先创建一个包含所有文件名称的数组。 Then, in a second loop, a file index is created by random, and the file is copied to the destination folder. 然后,在第二个循环中,随机创建文件索引,然后将文件复制到目标文件夹。

Dim FSO As Object, folder a Object, file as Object
Set folder = fso.GetFolder(FromPath)
Dim fList() As String, i As Long
ReDim fList(1 To folder.Files.Count)

For Each file In folder.Files
    i = i + 1
    fList(i) = file.Name
Next file

Dim copyCount As Long, fIndex As Long
copyCount = 0
Do While copyCount < 25 And copyCount < folder.Files.Count
    fIndex = Int(Rnd * folder.Files.Count) + 1
    If fList(fIndex) <> "" Then
        Set file = folder.Files(CStr(fList(fIndex)))
        file.Copy ToPath, True
        fList(fIndex) = ""    '  Mark this file as copied to prevent that it is picked a 2nd time
        copyCount = copyCount + 1
    End If
Loop

A possible solution for your task is: 您的任务的可能解决方案是:

  1. Read all filenames in FromPath in an array. 在数组的FromPath中读取所有文件名。
  2. In a loop with 25 runs generate a random number based on the length of the array. 在25次循环的循环中,根据数组的长度生成一个随机数。
  3. Ensure that you did not copy by chance a file you already have copied. 确保您没有偶然复制已经复制的文件。

it must be very fast 它必须非常快

Sub CopyFiles()
    Dim objRows() As String
    Dim fso As Object
    Dim randNum As Long
    Source = "C:\Users\NagyI2\Documents\Macro testing\"
    Destination = "C:\Users\NagyI2\Documents\Copy test\"
    randNum = 25 ' set random number
        results = CreateObject("WScript.Shell").Exec("CMD /C DIR """ & Source & "*.xls*"" /S /B /A:-D").StdOut.ReadAll ' get file list in Source
        objRows = Split(results, vbCrLf) ' move list to array
        ReDim Preserve objRows(UBound(objRows) - 1) ' trim last empty value
        sList = getRand(randNum, objRows) ' get randomized list
            Set fso = VBA.CreateObject("Scripting.FileSystemObject")
                For Each sFile In sList
                    Call fso.CopyFile(sFile, Destination, True) ' copy randomized files
                Next sFile
End Sub

Function getRand(rKey As Long, sArr As Variant) As Variant
    Randomize
    Set dict = CreateObject("Scripting.Dictionary")
    upperbound = UBound(sArr) 
    lowerbound = LBound(sArr)
    If rKey > upperbound Then getRand = sArr: Exit Function
    For i = 1 To rKey
        key = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
        If Not dict.Exists(key) Then dict.Add key, sArr(key) Else i = i - 1
    Next i
    getRand = dict.Items
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Excel VBA 将文件从一个文件夹复制到另一个文件夹的更快方法 - Faster Way to copy files from one folder to another with Excel VBA VBA-将多个选定文件从一个文件夹复制到另一个 - VBA - Copy multiple selected files from 1 folder to another VBA 将一个文件夹中多个工作簿的范围复制到另一个文件夹 - VBA to copy a range from multiple workbooks in a folder into another folder Excel VBA宏从文件夹复制多个文件到文件夹 - excel vba macro copy multiple files from folder to folder VBA从文件夹中的所有文件复制工作表并将其复制到主控 - VBA to copy sheets from all files in folder and copy it to master 如何将特定文件从子文件夹复制到目标文件夹? (Excel VBA) - How to copy specific files from subfolders to a destination folder? (Excel VBA) VBA将信息从文件文件夹复制并将信息编译到单个工作簿 - VBA to Copy Information From a Folder of Files and Compile Information to a Single Workbook VBA将工作表从一个工作簿复制到另一个文件夹中的所有工作簿 - VBA to copy worksheet from one workbook to all workbooks in another folder VBA使用进度条将pdf文件从一个位置复制到另一位置 - VBA to copy pdf files from one location to another with progress bar 无法将文件 (.pdf/.jpeg/.jpg) 从一个文件夹复制到另一个文件夹 - Unable to copy files (.pdf/.jpeg/.jpg) from one folder to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM