Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code where it will merge all the files in a selected folder.

VB
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("C:\Documents and Settings\MergeData")

Set fileObj = dirObj.Files
For Each everyObj In fileObj
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 row 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
Range("A2:IV" & Range("A65536").End(xlUp).Row).Copy

ThisWorkbook.Worksheets(1).Activate

'Do not change the following column. It's not the same column as above
Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial

Application.CutCopyMode = False
bookList.Close
Next
End Sub


But I only want selected columns to be copied to the new workbook. I don't know what function or code to use to make that happen.

Example:
Workbook1
Name, Address, Postal Code, Age

Workbook2
Name, Address, Age, Occupation

In the new workbook I want the columns with
Name and Address only to be copied
Posted
Updated 10-Dec-13 16:08pm
v3
Comments
joginder-banger 10-Dec-13 22:00pm    
which type of error you getting?
mitchiee1226 10-Dec-13 22:07pm    
It doesn't have a particular error. I just want the code to copy certain data is workbook1 and workbook2 then put it in the new work book.

Example:
Workbook1
Name, Address, Postal Code, Age
Workbook2
Name, Address, Age, Occupation
In the new workbook I wan the columns with
Name and Address only.

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900