Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Referencing Spreadsheet in VBA


I want to create a macro that imports data from a spreadsheet delivered
occasionally.

How can I set up the macro in such a way that the user can input the
name of the spreadsheet from which the data is to be copied over?


--
Timmy Mac1
------------------------------------------------------------------------
Timmy Mac1's Profile: http://www.excelforum.com/member.php...o&userid=15188
View this thread: http://www.excelforum.com/showthread...hreadid=535579

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Referencing Spreadsheet in VBA

See application.GetOpenfileName()

it puts up the file open dialog and allows the user to select a file. It
returns the users selection as a string and then your code can open that file
with the workbook.open method.

--
Regards,
Tom Ogilvy


"Timmy Mac1" wrote:


I want to create a macro that imports data from a spreadsheet delivered
occasionally.

How can I set up the macro in such a way that the user can input the
name of the spreadsheet from which the data is to be copied over?


--
Timmy Mac1
------------------------------------------------------------------------
Timmy Mac1's Profile: http://www.excelforum.com/member.php...o&userid=15188
View this thread: http://www.excelforum.com/showthread...hreadid=535579


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Referencing Spreadsheet in VBA


Thanks Tom.:)

Can I do it in such a way that the macro will keep referring to that
file, rather than have it come up each time?

What I want to do is get the User to select the file which will contain
various ranges I want to copy over to the "working" file?


--
Timmy Mac1
------------------------------------------------------------------------
Timmy Mac1's Profile: http://www.excelforum.com/member.php...o&userid=15188
View this thread: http://www.excelforum.com/showthread...hreadid=535579

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 96
Default Referencing Spreadsheet in VBA

What Tom suggested will work for you. You will have to assign what the user
inputs in the GetOpenFilename to a variable. The value in the variable will
stay the same until you change it and/or the macro finishes running.

If you want the variable to retain its value between instances of the macro
I would suggest entering whatever the user enters in GetOpenFilename into a
cell in the workbook. You can then refer to that cell whenever you want.
And the value in the cell can be changed by whatever the user types in the
GetOpenFilename if need be.

"Timmy Mac1" wrote:


Thanks Tom.:)

Can I do it in such a way that the macro will keep referring to that
file, rather than have it come up each time?

What I want to do is get the User to select the file which will contain
various ranges I want to copy over to the "working" file?


--
Timmy Mac1
------------------------------------------------------------------------
Timmy Mac1's Profile: http://www.excelforum.com/member.php...o&userid=15188
View this thread: http://www.excelforum.com/showthread...hreadid=535579


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Referencing Spreadsheet in VBA

Just to elaborate on Williams good answer:

Dim sName as Variant
Dim sh as worksheet
Dim bk as Workbook
set sh = activesheet
sName = application.GetOpenFileName()
if sName = False then exit sub
set bk = Workbooks.Open(sName)

' now you use bk to refer to the workbook

bk.worksheets(1).Range("A1:A10").copy _
destination:=sh.Range("B9")
bk.worksheets(1).Range("F3:G10").copy _
destination:=sh.Range("M5")

'and so forth.
bk.close Savechange:=False

--
Regards,
Tom Ogilvy

"William Horton" wrote:

What Tom suggested will work for you. You will have to assign what the user
inputs in the GetOpenFilename to a variable. The value in the variable will
stay the same until you change it and/or the macro finishes running.

If you want the variable to retain its value between instances of the macro
I would suggest entering whatever the user enters in GetOpenFilename into a
cell in the workbook. You can then refer to that cell whenever you want.
And the value in the cell can be changed by whatever the user types in the
GetOpenFilename if need be.

"Timmy Mac1" wrote:


Thanks Tom.:)

Can I do it in such a way that the macro will keep referring to that
file, rather than have it come up each time?

What I want to do is get the User to select the file which will contain
various ranges I want to copy over to the "working" file?


--
Timmy Mac1
------------------------------------------------------------------------
Timmy Mac1's Profile: http://www.excelforum.com/member.php...o&userid=15188
View this thread: http://www.excelforum.com/showthread...hreadid=535579




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Referencing Spreadsheet in VBA


Tom / William

Many many thanks for your assistance. You'll have guessed I am a
complete amateur and what you have showed me has given me great
motivation to learn and use VB more.

If I may be so bold I have one further question.

Instead of having the user open a file from windows explorer, how can I
do it so that they are prompted to pick from a list of files already
opened?

many thanks

timmy mac


--
Timmy Mac1
------------------------------------------------------------------------
Timmy Mac1's Profile: http://www.excelforum.com/member.php...o&userid=15188
View this thread: http://www.excelforum.com/showthread...hreadid=535579

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Referencing Spreadsheet in VBA

The is no built in capability to offer a list to select from. You could do
this by creating a userform. If you want to pursue that, start a new thread
for best response.

Otherwise, you can use the application.Input box function to have user
select a cell (any cell) on the sheet in the destination workbook where they
want to put the data.

Dim MyOpenBook as Workbook
Dim MyRange as Range
Dim MySheet as Worksheet
Dim sh as worksheet

set sh = activesheet

On error resume Next
set MyRange = Application.InputBox("Select a cell in the " & _
"destination sheet with the mouse", Type:=8)
On error goto 0
if MyRange is Nothing then
MsgBox "No range selected. Quitting. . ."
exit sub
end if
set MyBook = rng.parent.parent
set MySheet = rng.Parent
MyBook.worksheets(1).Range("A1:A10").copy _
destination:=sh.Range("B9")
MyBook.worksheets(1).Range("F3:G10").copy _
destination:=sh.Range("M5")


or
MySheet.Range("A1:A10").copy _
Destination:=sh.Range("B9")
MySheet.Range("F3:G10").copy _
Destination:=sh.Range("M5")


--
Regards,
Tom Ogilvy


"Timmy Mac1" wrote
in message ...

Tom / William

Many many thanks for your assistance. You'll have guessed I am a
complete amateur and what you have showed me has given me great
motivation to learn and use VB more.

If I may be so bold I have one further question.

Instead of having the user open a file from windows explorer, how can I
do it so that they are prompted to pick from a list of files already
opened?

many thanks

timmy mac


--
Timmy Mac1
------------------------------------------------------------------------
Timmy Mac1's Profile:

http://www.excelforum.com/member.php...o&userid=15188
View this thread: http://www.excelforum.com/showthread...hreadid=535579



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
referencing a different tab by referencing a list in the current s Kevin Excel Worksheet Functions 3 July 6th 07 07:57 PM
Referencing Tab Name in Excel Spreadsheet? DLO Excel Discussion (Misc queries) 5 February 20th 07 07:48 PM
#VALUE error when referencing spreadsheet on sharepoint library Lois Johns Excel Worksheet Functions 2 May 19th 05 05:38 PM
Inter-spreadsheet Referencing - Continual need to locate linked fi Atreides Excel Discussion (Misc queries) 0 February 22nd 05 07:01 AM
Is there a way to insert a formula, password or macro in an excel spreadsheet that will automatically delete the spreadsheet? oil_driller Excel Discussion (Misc queries) 1 February 8th 05 09:34 AM


All times are GMT +1. The time now is 06:37 AM.

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"