【发布时间】: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