मैं वीबीए में नया हूं, इसलिए मेरे पास एक्सेल कॉलम में दस्तावेज़ (एक्सटेंशन .pdf, .docx, आदि के साथ) की एक सूची है। मैं जो करना चाहता हूं वह सूची में सभी दस्तावेज़ों को स्रोत फ़ोल्डर से गंतव्य फ़ोल्डर में कॉपी करना है।
मैंने पहले से ही कुछ कोड की कोशिश की है, यह काम करता है लेकिन कोड सूची में फ़ाइल के बजाय फ़ोल्डर में सभी फाइलों की प्रतिलिपि बनाता है (दस्तावेज़ सूची केवल बी 3: बी 10 में है)।
किसी भी मदद की वास्तव में सराहना की।
अग्रिम में धन्यवाद।
Sub copyfile()
Dim r As Range
Dim Jajal As Range
Dim sourcePath As String, DestPath As String, FName As String
sourcePath = "C:\Users\"
DestPath = "H:\Users\"
For Each r In Range(Sheet6.Range("B3"), Sheet6.Range("B10")) 'the list document is in the sheet6 B3:B10
FName = Dir(sourcePath & r)
'Loop while files found
Do While FName <> ""
'Copy the file
FileCopy sourcePath & FName, DestPath & FName
'Search the next file
FName = Dir()
Loop
Next
End Sub
2 जवाब
सीमा से फ़ाइलें कॉपी करें (सूची)
कोड
Option Explicit
' This will copy files found in a source path AND whose names
' are contained in a list (range), to a destination path,
' overwriting possible existing files.
Sub copyFiles()
Const SourcePath As String = "C:\Users\"
Const DestPath As String = "H:\Users\"
Const ListAddress As String = "B3:B10"
' Write file list to array.
Dim FileList As Variant: FileList = Sheet1.Range(ListAddress).Value
' 'Get' first file name.
Dim FName As String: FName = Dir(SourcePath)
' 'Initiate' counter.
Dim i As Long
' Loop files in SourcePath.
Do While FName <> ""
' Check if file name of current file is contained in array (FileList).
If Not IsError(Application.Match(FName, FileList, 0)) Then
' Count file.
i = i + 1
' Copy file.
FileCopy SourcePath & FName, DestPath & FName
End If
' 'Get' next file name.
FName = Dir()
Loop
' Inform user.
Select Case i
Case 0: MsgBox "No files found", vbExclamation, "No Files"
Case 1: MsgBox "Copied 1 file.", vbInformation, "Success"
Case Else: MsgBox "Copied " & i & " files.", vbInformation, "Success"
End Select
End Sub
Dir
का उपयोग करके आप निर्देशिका की सभी फाइलों पर लूप करते हैं। यदि आप अपनी फ़ाइलें जानते हैं, तो आपको Dir
की आवश्यकता नहीं है। निम्नलिखित की तरह प्रयास करें (परीक्षण नहीं किया गया):
Sub copyfile()
Dim r As Range
Dim Jajal As Range
Dim sourcePath As String, DestPath As String
sourcePath = "C:\Users\"
DestPath = "H:\Users\"
For Each r In Range(Sheet6.Range("B3"), Sheet6.Range("B10")) 'the list document is in the sheet6 B3:B10
'Loop while files found
If r.Value <> ""
'Copy the file
FileCopy sourcePath & r.Value, DestPath & r.Value
'Search the next file
End If
Next
End Sub
हालांकि, आप जांच सकते हैं कि कॉपी करने से पहले फ़ाइल मौजूद है या नहीं।
संबंधित सवाल
नए सवाल
excel
केवल एक्सेल ऑब्जेक्ट्स या फ़ाइलों, या जटिल फॉर्मूला विकास के खिलाफ प्रोग्रामिंग पर प्रश्नों के लिए। यदि आप लागू हो तो आप Excel टैग को VBA, VSTO, C #, VB.NET, PowerShell, OLE स्वचालन और अन्य प्रोग्रामिंग संबंधी टैग और प्रश्नों के साथ जोड़ सकते हैं। सुपर उपयोगकर्ता पर एकल कार्यपत्रक कार्यों के लिए एमएस एक्सेल के बारे में सामान्य सहायता उपलब्ध है।