انتقل إلى المحتوى الرئيسي

كيفية إجراء حلقة عبر الملفات في دليل ونسخ البيانات إلى ورقة رئيسية في Excel؟

لنفترض أن هناك العديد من مصنفات Excel في مجلد ، وتريد تكرار كل ملفات Excel هذه ونسخ البيانات من نطاق محدد من أوراق العمل ذات الاسم نفسه إلى ورقة عمل رئيسية في Excel ، فماذا يمكنك أن تفعل؟ تقدم هذه المقالة طريقة لتحقيق ذلك بالتفصيل.

قم بالتكرار خلال الملفات الموجودة في دليل وانسخ البيانات إلى ورقة رئيسية برمز VBA


قم بالتكرار خلال الملفات الموجودة في دليل وانسخ البيانات إلى ورقة رئيسية برمز VBA

إذا كنت ترغب في نسخ البيانات المحددة في النطاق A1: D4 من كافة أوراق المصنفات الموجودة في مجلد معين إلى ورقة رئيسية ، فيرجى القيام بما يلي.

1. في المصنف الذي ستقوم بإنشاء ورقة عمل رئيسية ، اضغط على قديم + F11 مفاتيح لفتح ميكروسوفت فيسوال باسيك للتطبيقات نافذة.

2. في ال ميكروسوفت فيسوال باسيك للتطبيقات الإطار، انقر فوق إدراج > وحدة. ثم انسخ رمز VBA أدناه في نافذة الكود.

كود فبا: تكرار الملفات في مجلد وانسخ البيانات إلى ورقة رئيسية

Sub Merge2MultiSheets()
    Dim xRg As Range
    Dim xSelItem As Variant
    Dim xFileDlg As FileDialog
    Dim xFileName, xSheetName, xRgStr As String
    Dim xBook, xWorkBook As Workbook
    Dim xSheet As Worksheet
    On Error Resume Next
    Application.DisplayAlerts = False
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    xSheetName = "Sheet1"
    xRgStr = "A1:D4"
    Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
    With xFileDlg
        If .Show = -1 Then
            xSelItem = .SelectedItems.Item(1)
            Set xWorkBook = ThisWorkbook
            Set xSheet = xWorkBook.Sheets("New Sheet")
            If xSheet Is Nothing Then
                xWorkBook.Sheets.Add(after:=xWorkBook.Worksheets(xWorkBook.Worksheets.Count)).Name = "New Sheet"
                Set xSheet = xWorkBook.Sheets("New Sheet")
            End If
            xFileName = Dir(xSelItem & "\*.xlsx", vbNormal)
            If xFileName = "" Then Exit Sub
            Do Until xFileName = ""
               Set xBook = Workbooks.Open(xSelItem & "\" & xFileName)
                Set xRg = xBook.Worksheets(xSheetName).Range(xRgStr)
                xRg.Copy xSheet.Range("A65536").End(xlUp).Offset(1, 0)
                xFileName = Dir()
                xBook.Close
            Loop
        End If
    End With
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub

ملاحظات:

1). في الكود ، "A1: D4"و"Sheet1"يعني أنه سيتم نسخ البيانات الموجودة في النطاق A1: D4 من كل ورقة 1 إلى الورقة الرئيسية. و "ورقة جديدة"هو اسم الورقة الرئيسية المنشأة الجديدة.
2). يجب ألا تفتح ملفات Excel في المجلد المحدد.

3. اضغط على F5 مفتاح لتشغيل الكود.

4. في الافتتاح تصفح نافذة ، يرجى تحديد المجلد الذي يحتوي على الملفات التي ستمررها ، ثم انقر فوق OK زر. انظر لقطة الشاشة:

ثم يتم إنشاء ورقة عمل رئيسية تسمى "ورقة جديدة" في نهاية المصنف الحالي. ويتم سرد البيانات الموجودة في النطاق A1: D4 لكل Sheet1 في المجلد المحدد داخل ورقة العمل.


مقالات ذات صلة:

أفضل أدوات إنتاجية المكتب

🤖 مساعد Kutools AI: إحداث ثورة في تحليل البيانات على أساس: التنفيذ الذكي   |  إنشاء التعليمات البرمجية  |  إنشاء صيغ مخصصة  |  تحليل البيانات وإنشاء الرسوم البيانية  |  استدعاء وظائف Kutools...
الميزات الشعبية: البحث عن التكرارات أو تمييزها أو تحديدها   |  حذف الصفوف الفارغة   |  دمج الأعمدة أو الخلايا دون فقدان البيانات   |   جولة بدون صيغة 
سوبر بحث: معايير متعددة VLookup    VLookup ذات القيمة المتعددة  |   VLookup عبر أوراق متعددة   |   بحث غامض ....
قائمة منسدلة متقدمة: إنشاء القائمة المنسدلة بسرعة   |  القائمة المنسدلة التابعة   |  قائمة منسدلة متعددة التحديد ....
مدير العمود: إضافة عدد محدد من الأعمدة  |  نقل الأعمدة  |  تبديل حالة رؤية الأعمدة المخفية  |  مقارنة النطاقات والأعمدة 
الميزات المميزة: التركيز على الشبكة   |  عرض تصميم   |   شريط الفورمولا الكبير    مدير المصنفات والأوراق   |  مكتبة الموارد (النص السيارات)   |  منتقي التاريخ   |  اجمع أوراق العمل   |  تشفير/فك تشفير الخلايا    إرسال رسائل البريد الإلكتروني عن طريق القائمة   |  سوبر تصفية   |   مرشح خاص (تصفية غامق / مائل / يتوسطه خط ...) ...
أفضل 15 مجموعة أدوات12 نص الأدوات (إضافة نص, إزالة الأحرف، ...)   |   +50 رسم الأنواع (مخطط جانت، ...)   |   40+ عملي الصيغ (احسب العمر على أساس تاريخ الميلاد، ...)   |   19 إدخال الأدوات (أدخل رمز الاستجابة السريعة, إدراج صورة من المسار، ...)   |   12 تحويل الأدوات (أرقام إلى كلمات, نتيجة تحويل عملة، ...)   |   7 دمج وتقسيم الأدوات (الجمع بين الصفوف المتقدمة, تقسيم الخلايا، ...)   |   ... و اكثر

عزز مهاراتك في Excel باستخدام Kutools for Excel، واختبر كفاءة لم يسبق لها مثيل. يقدم Kutools for Excel أكثر من 300 ميزة متقدمة لتعزيز الإنتاجية وتوفير الوقت.  انقر هنا للحصول على الميزة التي تحتاجها أكثر...

الوصف


يجلب Office Tab الواجهة المبوبة إلى Office ، ويجعل عملك أسهل بكثير

  • تمكين التحرير والقراءة المبوبة في Word و Excel و PowerPointوالناشر والوصول و Visio والمشروع.
  • فتح وإنشاء مستندات متعددة في علامات تبويب جديدة من نفس النافذة ، بدلاً من النوافذ الجديدة.
  • يزيد من إنتاجيتك بنسبة 50٪ ، ويقلل مئات النقرات بالماوس كل يوم!
Comments (22)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Good afternoon. I urgently need your help: what VBA code could I use to copy a folder from an Excel workbook and paste it into an existing Excel workbook in another workbook? Would it be possible to copy the formatting just from the formatting?
This comment was minimized by the moderator on the site
Boa tarde. Preciso urgentemente de sua ajuda: qual código de VBA poderia utilizar para copiar a uma planilha inteira de uma pasta de trabalho Excel e colar em várias outras pastas de trabalho Excel já existentes em uma em um mesmo diretório? Teria como copiar apenas a formatação da planilha inteira?
This comment was minimized by the moderator on the site
My scenario is similar, except I have multiple sheets in each file, all with different names but consistent between files. Is there a way to Loop this code to copy the data within the files and paste (values) to specific sheet names in the master workbook? The sheet names in the master are the same as in the files. I want to loop through them. Also, the amount of data in each sheet will vary, so I will need to select the data in each sheet using something like this:

Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select


File sheet names are Giving, Services, Insurance, Car, Other Expenses, etc...

Thanks in advance.
This comment was minimized by the moderator on the site
Hi Andrew Shahan,
The following VBA code can solve your problem. After running the code and selecting a folder, the code will automatically match the worksheet by name and paste the data into the worksheet of the same name in the master workbook.
Sub Merge2MultiSheets()
'Updated by Extendoffice 20221209
    Dim xRg As Range
    Dim xSelItem As Variant
    Dim xFileDlg As FileDialog
    Dim xFileName, xSheetName, xRgStr As String
    Dim xBook As Workbook, xMainBook As Workbook
    Dim xSheet As Worksheet
    On Error Resume Next
    
    Application.DisplayAlerts = False
    Application.EnableEvents = False
    Application.ScreenUpdating = False

    Set xMainBook = ThisWorkbook
    
    Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
    With xFileDlg
        If .Show = -1 Then
            xSelItem = .SelectedItems.Item(1)
            xFileName = Dir(xSelItem & "\*.xlsx", vbNormal)
            If xFileName = "" Then Exit Sub
            
            Do Until xFileName = ""
            Set xBook = Workbooks.Open(xSelItem & "\" & xFileName)

            For Start = 1 To xBook.Worksheets.Count
                Set xSheet = xBook.Worksheets.Item(Start)
                xSheet.Activate
                xSheetName = xSheet.Name
                xSheet.UsedRange.Copy (xMainBook.Worksheets.Item(xSheetName).Range("A1048576").End(xlUp).Offset(1, 0))
            Next
                xFileName = Dir()
                xBook.Close
            Loop
        End If
    End With
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
This comment was minimized by the moderator on the site
Olá bom dia.
Gostei muito dessde código, mas não me ajudou com os relatórios que eu preciso impreimir.
Preciso imprimir 2.400 relatório de exel que estão em pastas diferentes e não estão configuradas corretamente para impressão. Pode me enviar um códgo de VBA que automatize essas impressões ? Me ajudaria muito, obrigada.
This comment was minimized by the moderator on the site
Hi Maria Soares,
Please check if the VBA code in the following post can help.
How to print multiple workbooks in Excel?
This comment was minimized by the moderator on the site
Hi i want a code to copy the data in 6 different workbooks(in a folder) which has sheets included in them to NEW WORKBOOK. in vba
plz help me asp
This comment was minimized by the moderator on the site
Hi Paranusha,
The VBA script in the following article can combine multiple workbooks or specified sheets of workbooks to a master workbook. Please check if it can help.
How To Combine Multiple Workbooks Into One Master Workbook In Excel?
This comment was minimized by the moderator on the site
for me, the "Sheet1" tab name changes for each of my files. For instance, Tab1, Tab2, Tab3, Tab4...How can I setup a loop to run through a list in excel and keep changing the "Sheet1" name until it runs through everything?
This comment was minimized by the moderator on the site
Hi Nick,The VBA code below can help you solve the problem. Please have a try.<div data-tag="code">Sub LoopThroughFileRename()
'Updated by Extendofice 2021/12/31
Dim xRg As Range
Dim xSelItem As Variant
Dim xFileDlg As FileDialog
Dim xFileName, xSheetName, xRgStr As String
Dim xBook, xWorkBook As Workbook
Dim xSheet As Worksheet
Dim xShs As Sheets
Dim xName As String
Dim xFNum As Integer
On Error Resume Next
Application.DisplayAlerts = False
Application.EnableEvents = False
Application.ScreenUpdating = False
Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
xFileDlg.Show
xSelItem = xFileDlg.SelectedItems.Item(1)
xFileName = Dir(xSelItem & "\*.xlsx", vbNormal)
Do While xFileName <> ""
Set xWorkBook = Workbooks.Open(xSelItem & "\" & xFileName)
Set xShs = xWorkBook.Sheets
For xFNum = 1 To xShs.Count
Set xSheet = xShs.Item(xFNum)
xName = xSheet.Name
xName = Replace(xName, "Sheet", "Tab") 'Replace Sheet with Tab
xSheet.Name = xName
Next
xWorkBook.Save
xWorkBook.Close
xFileName = Dir()
Loop
Application.DisplayAlerts = True
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
This comment was minimized by the moderator on the site
how do you make to code leave a blank if cell is empty?
This comment was minimized by the moderator on the site
Thank you - how would one be able to copy and paste (special values) from each worksheet within a workbook into separate sheets within a main Master file?
This comment was minimized by the moderator on the site
Hi - This code works very well for the first 565 lines for every file, but all lines after are overlapped by the next file.
is there a way to fix this?
This comment was minimized by the moderator on the site
Hi - This code is perfect for what I'm trying to achieve.

Is there a way to loop through all folders and subfolders and perform the copy?


Thanks!
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations