【问题标题】:Consolidating Workbooks with two sheets into One workbook with the same sheet. Second sheet will not copy correctly将具有两张工作表的工作簿合并为一个具有相同工作表的工作簿。第二张纸无法正确复印
【发布时间】:2020-01-03 20:22:27
【问题描述】:

我正在尝试将数据从 3 个单独的工作簿复制到一个带有两张工作表的工作簿中。所有工作簿都具有相同的设置,只是数据量不同。当我运行我的代码时,第二张“所有被保险人”的金额仅与保险员工的金额相同。从 3 个单独的工作簿复制时,好像 EndxlUp 不起作用

Sub simpleXlsMerger()
Dim bookList As Workbook
Dim mergeObj As Object, dirObj As Object, filesObj As Object, everyObj As Object
Application.ScreenUpdating = False
Set mergeObj = CreateObject("Scripting.FileSystemObject")

'change folder path of excel files here
Set dirObj = mergeObj.Getfolder("M:\Active Clients\HBS Inc Clients\NBP - National Beef Packing\Enrollments\Selerix combination\Copy Files Here")
Set filesObj = dirObj.Files
For Each everyObj In filesObj
Set bookList = Workbooks.Open(everyObj)

'change "A2" with cell reference of start point for every files here
'for example "B3:IV" to merge all files start from columns B and rows 3
'If you're files using more than IV column, change it to the latest column
'Also change "A" column on "A65536" to the same column as start point
Sheets("Coverage by Employee").Range("A2:AR" & Range("A65536").End(xlUp).Row).Copy
ThisWorkbook.Worksheets("Coverage by Employee").Activate

'Do not change the following column. It's not the same column as above
Sheets("Coverage by Employee").Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial
Application.CutCopyMode = False
bookList.Close
Next

Dim ibookList As Workbook
Dim imergeObj As Object, idirObj As Object, ifilesObj As Object, ieveryObj As Object

Set imergeObj = CreateObject("Scripting.FileSystemObject")

'change folder path of excel files here
Set idirObj = imergeObj.Getfolder("M:\Active Clients\HBS Inc Clients\NBP - National Beef Packing\Enrollments\Selerix combination\Copy Files Here")
Set ifilesObj = idirObj.Files
For Each ieveryObj In ifilesObj
Set ibookList = Workbooks.Open(everyObj)

'change "A2" with cell reference of start point for every files here
'for example "B3:IV" to merge all files start from columns B and rows 3
'If you're files using more than IV column, change it to the latest column
'Also change "A" column on "A65536" to the same column as start point
Sheets("All Insureds").Range("A2:AV" & Range("A65536").End(xlUp).Row).Copy
Workbooks("Selerix Combination Macro.xlsm").Worksheets("All Insureds").Activate

'Do not change the following column. It's not the same column as above
Workbooks("Selerix Combination Macro.xlsm").Sheets("All Insureds").Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial
Application.CutCopyMode = False
ibookList.Close
Next



End Sub

【问题讨论】:

    标签: excel vba loops copy consolidation


    【解决方案1】:

    您需要限定每个范围的 WorksheetWorkbook

    Sheets("All Insureds").Range("A2:AV" & Range("A65536").End(xlUp).Row).Copy
    

    应该是这样的:

    With ibookList.Sheets("All Insureds")
        Dim lastRow as Long
        lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    
        .Range("A2:AV" & lastRow).Copy
    End With
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      • 2017-06-20
      • 1970-01-01
      相关资源
      最近更新 更多