简体   繁体   English

从多个工作簿复制粘贴一行到主工作簿

[英]copy paste one row from multiple workbooks to master workbook

I Have the below code to perform certain action.我有以下代码来执行某些操作。 while I need to add an additional action of copying row 10 from sheet 2 with name "Site Creation Template(Project)" from multiple workbooks as in below.而我需要添加一个额外的操作,即从多个工作簿中复制第 2 行中名为“站点创建模板(项目)”的第 10 行,如下所示。

I have tried several other possible combinations available in the web but it returns either wrong value or just blank.我已经尝试了网络上可用的其他几种可能的组合,但它返回错误的值或只是空白。

Can anyone help me on this?谁可以帮我这个事?

PS: Im just a starter in VBA. PS:我只是 VBA 的初学者。

    Sub copyDataFromMultipleWorkbooksIntoMaster()

Dim FileItem As Object
Dim oFolder As Object
Dim FSO As Object
Dim BrowseFolder As String

Dim masterBook As Workbook
Dim sourceBook As Workbook

Dim insertRow As Long
Dim copyRow As Long

' add variables for blank check
Dim checkRange As Range, R As Range

insertRow = 22
Set masterBook = ThisWorkbook

Set FSO = CreateObject("Scripting.FileSystemObject")

        With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select the folder with source files"
        If Not .Show = 0 Then
            BrowseFolder = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With

    Application.ScreenUpdating = False


    Set oFolder = FSO.getfolder(BrowseFolder)

    masterBook.Sheets("Service Order Template").Cells.UnMerge


    For Each FileItem In oFolder.Files

       If FileItem.Name Like "*.xls*" Then

        Workbooks.Open (BrowseFolder & Application.PathSeparator & FileItem.Name)

       Set sourceBook = Workbooks(FileItem.Name)

           With sourceBook.Sheets("Service Order Template")
               .Cells.UnMerge
               copyRow = .Cells(Rows.Count, 18).End(xlUp).Row
               Range(.Cells(22, 1), .Cells(copyRow, 45)).Copy Destination:=masterBook.Sheets("Service Order Template").Cells(insertRow, 1)

               ' copy additional needed range D5 : D18 from source to range D5 on master
               Range(.Cells(5, 4), .Cells(18, 4)).Copy Destination:=masterBook.Sheets("Service Order Template").Cells(5, 4)

               Application.CutCopyMode = False
               .Parent.Close saveChanges:=False
          End With
        insertRow = masterBook.Sheets("Service Order Template").Cells(Rows.Count, 18).End(xlUp).Row + 2
       End If
    Next

    With masterBook.Sheets("Service Order Template")
        ' if you don't need to highlight the whole row - remove the ".EntireRow" part ?---?---?----?
        Range(.Cells(20, 18), .Cells(Rows.Count, 18).End(xlUp)).SpecialCells(xlCellTypeBlanks).EntireRow.Interior.Color = vbYellow
    End With


    Application.ScreenUpdating = True
    Application.DisplayAlerts = False
    Application.Dialogs(xlDialogSaveAs).Show ThisWorkbook.Name, 51


End Sub

I'm not sure which part you were having trouble with but try this我不确定你在哪个部分遇到了问题,但试试这个

Option Explicit

Sub CopyDataFromMultipleWorkbooksIntoMaster()

    Const TEMPLATE = "Service Order Template"
    Const SITE_TEMPLATE = "Site Creation Template(Project)"

    Dim FSO As Object
    Dim BrowseFolder As String
    Dim oFolder As Object

    ' select folder
    Set FSO = CreateObject("Scripting.FileSystemObject")
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select the folder with source files"
        If Not .Show = 0 Then
            BrowseFolder = .SelectedItems(1)
        Else
            MsgBox "Cancelled selection", vbCritical
            Exit Sub
        End If
    End With
    'Debug.Print "BrowseFolder = " & BrowseFolder

    Dim wbMaster As Workbook, wsMaster As Worksheet
    Dim wbSource As Workbook, wsSource As Worksheet, rngSource As Range
    Dim f As Object, fname As String
    Dim lastSrcRow As Long
    Dim insertRow1 As Long, insertRow2 As Long, count As Long

    Set wbMaster = ThisWorkbook
    Set wsMaster = wbMaster.Sheets(TEMPLATE)

    insertRow1 = 22
    insertRow2 = 1 ' start of row 10 copies on sheet 2 of master

    Set oFolder = FSO.getfolder(BrowseFolder)
    count = 0

    ' scan files
    For Each f In oFolder.Files

        If f.Name Like "*.xls*" Then

            fname = BrowseFolder & Application.PathSeparator & f.Name
            'Debug.Print fname

            Set wbSource = Workbooks.Open(fname, False, True) ' open no link update, read-only
            Set wsSource = wbSource.Sheets(TEMPLATE)

            lastSrcRow = wsSource.Cells(Rows.count, 18).End(xlUp).Row

            Set rngSource = wsSource.Range("A22:AS" & lastSrcRow) ' AS=col45
            Debug.Print f.Name, wsSource.Name, rngSource.Address

            rngSource.Copy wsMaster.Cells(insertRow1, 1)
            insertRow1 = insertRow1 + rngSource.Rows.count + 1

            ' copy additional needed range D5 : D18 from source to range D5 on master
            wsSource.Range("D5:D18").Copy wsMaster.Range("D5")

            'copying row 10 from sheet 2 with name "Site Creation Template(Project)"
            wbSource.Sheets(SITE_TEMPLATE).Rows(10).EntireRow.Copy wbMaster.Sheets(2).Range("A" & insertRow2)
            insertRow2 = insertRow2 + 1

            wbSource.Close False
            count = count + 1
        End If
    Next

    ' if you don't need to highlight the whole row - remove the ".EntireRow" part ?---?---?----?
    wsMaster.Range("R20:R" & insertRow1 - 1).SpecialCells(xlCellTypeBlanks).EntireRow.Interior.Color = vbYellow

    End
    MsgBox count & " files processed", vbInformation
End Sub

暂无
暂无

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

相关问题 如何从主工作簿复制并将数据粘贴到多个工作簿 - How to copy from master workbook and paste data on multiple workbooks 从多个工作簿复制并粘贴到单个工作簿中,再到下一个空白行 - Copy and Paste from multiple workbooks into a single workbook into the next blank row 将多行从多个工作簿复制到一个主工作簿 - Copy multiple rows from multiple workbooks to one master workbook 从一个工作簿复制并粘贴到文件夹中的多个CSV工作簿中 - Copy and Paste from one workbook into multiple CSV workbooks in a folder 如何基于可变条件从多个工作簿中复制数据行,然后粘贴到主工作簿中 - How to copy rows of data from multiple workbooks based on an variable criteria, then paste into a master workbook 如何将多个工作簿中的特殊内容粘贴到主工作簿 - How do I paste special from multiple workbooks to a master workbook VBA 代码,用于根据列标题将粘贴数据从多个源工作簿复制到主数据工作簿(主数据表) - VBA code to copy paste data from multiple source workbooks to a master data workbook (Master data sheet) based on column headers 将数据从多个工作簿的最后一行复制并粘贴到另一个工作簿中的工作表 - Copy and paste data from multiple workbooks last row to a worksheet in another Workbook Excel VBA:从另一个工作簿复制行并粘贴到主工作簿 - Excel VBA: Copy Row from another workbook and paste into master workbook Excel VBA。 从多个工作簿复制数据并粘贴到一个工作簿的同一工作表中 - Excel VBA. Copy data from multiple workbooks and paste in one workbook same worksheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM