#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Macro Help

I have 2 Workbooks (file1.xls & File2.xls).

In file1 columns a,b,c & d are populated with data, this is the same for
file2.

I need to extract the data in colums a & d in file1 and a & b in file2 and
place it into a new workbook, can this be done via macro?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Macro Help


I sd column A on boths sheets an ID and you want to put in the new
workbook the ID in column A , the data from book1 in column b and the
data from book2 in column C?


--
joel
------------------------------------------------------------------------
joel's Profile: http://www.thecodecage.com/forumz/member.php?userid=229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=146809

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Macro Help

Hi,

Try this

Sub Merge()
Set newBook = Workbooks.Add
With newBook
.SaveAs Filename:="newbook.xls"
End With
mypath = "C:\" 'change to suit
Workbooks.Open Filename:=mypath & "File1.xls"
Lastrow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
Range("A1:D" & Lastrow).Copy _
Destination:=Workbooks("Newbook.xls").Sheets("Shee t1").Range("A1")
Workbooks("Newbook.xls").Sheets("Sheet1").Columns( "B:C").Delete
Shift:=xlToLeft

Workbooks.Open Filename:=mypath & "File2.xls"
Lastrow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
Range("A1:B" & Lastrow).Copy _
Destination:=Workbooks("Newbook.xls").Sheets("Shee t1").Range("C1")
End Sub


Mike

"TONYr" wrote:

I have 2 Workbooks (file1.xls & File2.xls).

In file1 columns a,b,c & d are populated with data, this is the same for
file2.

I need to extract the data in colums a & d in file1 and a & b in file2 and
place it into a new workbook, can this be done via macro?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Macro Help

Cheers Mike,

it worked a treat, thanks for the help. One other question if in File2 I
had 2 worksheets (Sheet1 & Sheet2) what code would I need to add to get it to
pull the data from both sheets (Same format applies to both sheets ie. I
would need to pull data from column a in sheet1 and colum a in sheet 2)


Cheers

T

"Mike H" wrote:

Hi,

Try this

Sub Merge()
Set newBook = Workbooks.Add
With newBook
.SaveAs Filename:="newbook.xls"
End With
mypath = "C:\" 'change to suit
Workbooks.Open Filename:=mypath & "File1.xls"
Lastrow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
Range("A1:D" & Lastrow).Copy _
Destination:=Workbooks("Newbook.xls").Sheets("Shee t1").Range("A1")
Workbooks("Newbook.xls").Sheets("Sheet1").Columns( "B:C").Delete
Shift:=xlToLeft

Workbooks.Open Filename:=mypath & "File2.xls"
Lastrow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
Range("A1:B" & Lastrow).Copy _
Destination:=Workbooks("Newbook.xls").Sheets("Shee t1").Range("C1")
End Sub


Mike

"TONYr" wrote:

I have 2 Workbooks (file1.xls & File2.xls).

In file1 columns a,b,c & d are populated with data, this is the same for
file2.

I need to extract the data in colums a & d in file1 and a & b in file2 and
place it into a new workbook, can this be done via macro?

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Macro Help

Hi,

Explicitly name the sheets when working with File 2

Workbooks.Open Filename:=mypath & "File2.xls"
Lastrow = Sheets("Sheet1").Cells(Cells.Rows.Count, "A").End(xlUp).Row
Sheets("Sheet1").Range("A1:B" & Lastrow).Copy _
Destination:=Workbooks("Newbook.xls").Sheets("Shee t1").Range("C1")


Lastrow = Sheets("Sheet2").Cells(Cells.Rows.Count, "A").End(xlUp).Row
Sheets("Sheet2").Range("A1:B" & Lastrow).Copy _
Destination:=Workbooks("Newbook.xls").Sheets("Shee t1").Range("F1")

Mike

"TONYr" wrote:

Cheers Mike,

it worked a treat, thanks for the help. One other question if in File2 I
had 2 worksheets (Sheet1 & Sheet2) what code would I need to add to get it to
pull the data from both sheets (Same format applies to both sheets ie. I
would need to pull data from column a in sheet1 and colum a in sheet 2)


Cheers

T

"Mike H" wrote:

Hi,

Try this

Sub Merge()
Set newBook = Workbooks.Add
With newBook
.SaveAs Filename:="newbook.xls"
End With
mypath = "C:\" 'change to suit
Workbooks.Open Filename:=mypath & "File1.xls"
Lastrow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
Range("A1:D" & Lastrow).Copy _
Destination:=Workbooks("Newbook.xls").Sheets("Shee t1").Range("A1")
Workbooks("Newbook.xls").Sheets("Sheet1").Columns( "B:C").Delete
Shift:=xlToLeft

Workbooks.Open Filename:=mypath & "File2.xls"
Lastrow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
Range("A1:B" & Lastrow).Copy _
Destination:=Workbooks("Newbook.xls").Sheets("Shee t1").Range("C1")
End Sub


Mike

"TONYr" wrote:

I have 2 Workbooks (file1.xls & File2.xls).

In file1 columns a,b,c & d are populated with data, this is the same for
file2.

I need to extract the data in colums a & d in file1 and a & b in file2 and
place it into a new workbook, can this be done via macro?

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
AutoRun Macro with a delay to give user the choice to cancel the macro wanderlust Excel Programming 2 September 28th 07 04:09 PM
Macro Help Needed - Excel 2007 - Print Macro with Auto Sort Gavin Excel Worksheet Functions 0 May 17th 07 01:20 PM
Need syntax for RUNning a Word macro with an argument, called from an Excel macro Steve[_84_] Excel Programming 3 July 6th 06 07:42 PM
how to count/sum by function/macro to get the number of record to do copy/paste in macro tango Excel Programming 1 October 15th 04 01:16 PM
Start Macro / Stop Macro / Restart Macro Pete[_13_] Excel Programming 2 November 21st 03 05:04 PM


All times are GMT +1. The time now is 04:15 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"