Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Open Workbook in current directory

Hi, I'm using 2 workbooks in my forecast model. Can some help with a macro
to transfer data between the workbooks?

I have so far recorded a copy-paste special macro to transfer data, but what
I want to do is:
- If the workbook is open then activate the relevant sheet and copy and
paste data between workbooks, otherwise open the relevant workbook.
- Be able to store the 2 workbooks in any directory and still have the macro
work.

I've tried the following macro - but I get an error with the use of the Me.
construct:

Sub CopyData()

Dim sFileName As String
sFileName = "SpreadsheetB.xls"

On Error Resume Next
Workbooks("SpreadsheetB.xls").Activate
If Err.Number 0 Then
Workbooks.Open Me.Path & "\" & sFileName
End If
On Error GoTo 0

Sheets("Forecast").Select
Rows("2:270").Select
Selection.Copy
......

Does anyone tell me what I've done wrong here?

Thanks

Elijah


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Open Workbook in current directory

Me refers to a container, such as a form or a worksheet class. You probably
want ActiveWorkbook here, or ThisWorkbook if it will be the workbook that
the macro is stored in.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Elijah" wrote in message
...
Hi, I'm using 2 workbooks in my forecast model. Can some help with a macro
to transfer data between the workbooks?

I have so far recorded a copy-paste special macro to transfer data, but

what
I want to do is:
- If the workbook is open then activate the relevant sheet and copy and
paste data between workbooks, otherwise open the relevant workbook.
- Be able to store the 2 workbooks in any directory and still have the

macro
work.

I've tried the following macro - but I get an error with the use of the

Me.
construct:

Sub CopyData()

Dim sFileName As String
sFileName = "SpreadsheetB.xls"

On Error Resume Next
Workbooks("SpreadsheetB.xls").Activate
If Err.Number 0 Then
Workbooks.Open Me.Path & "\" & sFileName
End If
On Error GoTo 0

Sheets("Forecast").Select
Rows("2:270").Select
Selection.Copy
.....

Does anyone tell me what I've done wrong here?

Thanks

Elijah




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
open file in current directory Tomo Excel Worksheet Functions 1 January 12th 08 12:04 AM
open file in current directory Tomo Excel Discussion (Misc queries) 1 January 11th 08 11:48 PM
current directory Ross[_2_] Excel Discussion (Misc queries) 1 April 2nd 07 10:00 PM
changing current directory to that of the current open file unnameable Excel Programming 2 May 19th 04 11:14 AM
opening a workbook in the current directory Richard[_16_] Excel Programming 2 August 30th 03 02:46 AM


All times are GMT +1. The time now is 05:34 AM.

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"