LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default VBA code to copy named range in closed workbook

I'm trying to use the following script to copy a variable named range
in an unopened workbook into the active workbook that the code is
running in.
I have a couple of problems.
I got a good part of the script from the MS KB article
herehttp://support.microsoft.com/kb/257819)

I have two problems with the below code:
I get a "Run time error 424; Object required" error for the line "Set
Proj_Conn = CurrentProject.Connection"
while I can reference the named range in the 2nd file, I can't seem to
get it to copy the range into the active sheet.
Any ideas would be greately appreciated
Code follows:

Sub copy_3()
'Declare variables
Dim strQuery As String
Dim Proj_Conn As ADODB.Connection
Set Proj_Conn = CurrentProject.Connection
Dim userInput As String
Dim rst As New ADODB.Recordset
rst.ActiveConnection = Proj_Conn
' Set variable "file_name" to data source file name
'
file_name = "Excel ('Excel (*.xls*), *.xls*')"
'
' Prompt user for file name and path for data source
'
fileToOpen = Application.GetOpenFilename(file_name)
'
' Get Variable "Current_Month" for use in selecting active sheet in
opened file
'
userInput = Application.InputBox(prompt:="Enter Month", Type:=2)
'current_month = Application.InputBox(prompt:="Enter Month",
Type:=2)
current_month = userInput

With Proj_Conn
..Provider = "Microsoft.Jet.OLEDB.4.0"
..ConnectionString = "Data Source='" & fileToOpen & " '; Extended
Properties=HTML Import;"
..Open
End With

'copy named range into clipboard

strQuery = "SELECT * FROM '" & current_month & "';"

' select active workbook and reference worksheet
Windows("PM Schedule 2009.xlsm").Activate
Sheets("reference").Select

Range("a1").Activate

Range("a1").CopyFromRecordset rst
Range("a1").Select

' close connection
rst.Close
conn.Close

End Sub
 
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
Help...Code to copy sheet from closed workbook [email protected] Excel Discussion (Misc queries) 1 March 28th 07 08:52 PM
Workbook named range not seen by ADO when workbook closed Tim Lund[_2_] Excel Programming 2 March 16th 07 08:59 AM
ADO - closed workbook - Named range MattShoreson[_16_] Excel Programming 2 August 31st 05 10:37 AM
referencing a named range from a closed workbook Gixxer_J_97[_2_] Excel Programming 4 April 8th 05 08:45 PM


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