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

I'm trying to set up a macro that will:

1. Open another spreadsheet and copy a set of data...
2. then return to the original spreadsheet (where the macro is recorded)
and paste the data...
3. then close the spreadsheet where the data was copied from.

The issue is, the names of the original spreadsheet and the spreadsheet
containing the data to be copied will always change. How do I set up the
macro to recognize these changes?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 336
Default Excel Macro

Sub ...()
Dim myBook1 As Workbook
Dim myBook2 As Workbook
Set myBook1 = ActiveWorkbook
....(code to open other file)
Set myBook2 = ActiveWorkbook

You can then merrily move between the two books with myBook1.Activate or
myBook2.Activate.

You can also refer to individual sheets without having to activate them, e.g.
myBook1.Worksheets("Sheet2"). etc.

"gregory" wrote:

I'm trying to set up a macro that will:

1. Open another spreadsheet and copy a set of data...
2. then return to the original spreadsheet (where the macro is recorded)
and paste the data...
3. then close the spreadsheet where the data was copied from.

The issue is, the names of the original spreadsheet and the spreadsheet
containing the data to be copied will always change. How do I set up the
macro to recognize these changes?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 184
Default Excel Macro

Hi, just to add to martin's response--you can also use 'thisworkbook',
which will refer to the workbook from which the macro is running.

ThisWorkbook.Activate ' will return to the workbook which is running
the macro.

HTH--Lonnie M.

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

Nice. Worked perfectly.

Thanks, Martin!

"Martin" wrote:

Sub ...()
Dim myBook1 As Workbook
Dim myBook2 As Workbook
Set myBook1 = ActiveWorkbook
...(code to open other file)
Set myBook2 = ActiveWorkbook

You can then merrily move between the two books with myBook1.Activate or
myBook2.Activate.

You can also refer to individual sheets without having to activate them, e.g.
myBook1.Worksheets("Sheet2"). etc.

"gregory" wrote:

I'm trying to set up a macro that will:

1. Open another spreadsheet and copy a set of data...
2. then return to the original spreadsheet (where the macro is recorded)
and paste the data...
3. then close the spreadsheet where the data was copied from.

The issue is, the names of the original spreadsheet and the spreadsheet
containing the data to be copied will always change. How do I set up the
macro to recognize these changes?

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Excel Macro

How do I set up the
macro to recognize these changes?


Assume the workbook name and path are in A1 of sheet1 and in A2 is the sheet
name.

Dim rng as Range, bk as Workbook
set rng = thisworkbook.Worksheets("Sheet1").Range("A1")
set bk = Workbooks.Open(rng.Value)
bk.worksheets(rng.offset(1,0)).Range("B9:C30").cop y _
Destination.ThisWorkbook.Worksheets("Sheet2").Rang e("F2")
bk.Close SaveChanges:=False

above would be an example of how you might want to approach the problem.

--
Regards,
Tom Ogilvy



"gregory" wrote:

I'm trying to set up a macro that will:

1. Open another spreadsheet and copy a set of data...
2. then return to the original spreadsheet (where the macro is recorded)
and paste the data...
3. then close the spreadsheet where the data was copied from.

The issue is, the names of the original spreadsheet and the spreadsheet
containing the data to be copied will always change. How do I set up the
macro to recognize these changes?

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
Macro Help Needed - Excel 2007 - Print Macro with Auto Sort Gavin Excel Worksheet Functions 0 May 17th 07 01:20 PM
My excel macro recorder no longer shows up when recording macro jack Excel Discussion (Misc queries) 1 February 5th 07 09:31 PM
translate lotus 1-2-3 macro into excel macro using excel 2000 krutledge0209 Excel Programming 1 November 2nd 04 05:50 PM
Excel Macro Issue Trying to autorun Macro Upon Opening Worksheet wyndman Excel Programming 2 May 25th 04 06:59 PM


All times are GMT +1. The time now is 02:00 PM.

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

About Us

"It's about Microsoft Excel"