Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 59
Default Open Workbook Macro???

I created a macro that copies the data from one workbook to another. This
only works if both workbooks are open. Is it possible to run this without
opening the workbook I am getting the data from? If I do need to open it
first, when to I need to add to the beginning of the macro to open this
workbook?
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Open Workbook Macro???

Dim Wkbk as workbook
dim myPathName as string
dim myFileName as string

mypathname = "C:\yourpath\to\yourfile\"
myfilename = "someworkbookname.xls"

set wkbk = nothing
on error resume next
set wkbk = workbooks(myfilename)
on error goto 0

if wkbk is nothing then
'it wasn't open, so try to open it
on error resume next
set wkbk = workbooks.open(filename:=mypathname & myfilename)
on error goto 0

if wkbk is nothing then
msgbox "That file couldn't be opened" & vblf & mypathname & myfilename
exit sub '???
end if
end if

'do something with wkbk
msgbox wkbk.worksheets(1).range("a1").value

====
Untested, uncompiled. Watch for typos!



Supe wrote:

I created a macro that copies the data from one workbook to another. This
only works if both workbooks are open. Is it possible to run this without
opening the workbook I am getting the data from? If I do need to open it
first, when to I need to add to the beginning of the macro to open this
workbook?


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,123
Default Open Workbook Macro???

You can try this example that copy a sheet.
It open the workbook, copy and close the workbook

If it is possible that the workbook is already open you must add a test for that
If you need with that help post back

Sub test()
Dim Wb1 As Workbook
Dim Wb2 As Workbook
Application.ScreenUpdating = False
Set Wb1 = ActiveWorkbook
Set Wb2 = Workbooks.Open("C:\data\ron.xls")
Wb2.Sheets("Sheet1").copy _
after:=Wb1.Sheets(Wb1.Sheets.Count)
Wb2.Close False
Application.ScreenUpdating = True
End Sub


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Supe" wrote in message ...
I created a macro that copies the data from one workbook to another. This
only works if both workbooks are open. Is it possible to run this without
opening the workbook I am getting the data from? If I do need to open it
first, when to I need to add to the beginning of the macro to open this
workbook?

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
Run a macro when I open a workbook CraigJ Excel Discussion (Misc queries) 1 June 13th 07 04:04 PM
Run a macro automatically on workbook open tweacle Excel Worksheet Functions 2 February 25th 06 06:38 PM
How to Run Macro in open workbook/s hni Excel Discussion (Misc queries) 1 October 31st 05 10:14 AM
run macro on workbook open kevin Excel Discussion (Misc queries) 3 March 4th 05 10:12 AM
how can I run a macro when I open a workbook? filo666 Excel Discussion (Misc queries) 2 March 2nd 05 07:56 PM


All times are GMT +1. The time now is 12:54 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"