Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 28th, 2007, 09:57 AM
Registered User
 
Join Date: Aug 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Import spreadsheets for Sarbanes-Oxley reporting

Hi:)

First time poster here, so apologies in advance....;)

I am looking for code to import multiple spreadsheets into on single sheet from different locations on the network.

The spreadsheets will all have similar column headers but may have multiple tabs.

Also, is there a way of making the import start at a specific row?

Many thanks

 
Old August 28th, 2007, 11:48 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

The easies way is to have query tables. This will be the dynamic replica of the data from individual sheets

Here is an example that you can finetune for your requirement

Sub Excel_QueryTable()

Dim oCN As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim ConnString As String
Dim SQL As String

Dim qt As QueryTable

ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\SubFile.xls;Extended Properties=Excel 8.0;Persist Security Info=False"
Set oCN = New ADODB.Connection
oCN.ConnectionString = ConnString
oCN.Open

SQL = "Select * from [Sheet1$]"

Set oRS = New ADODB.Recordset
oRS.Source = SQL
oRS.ActiveConnection = oCN
oRS.Open

Set qt = Worksheets(1).QueryTables.Add(Connection:=oRS, _
        Destination:=Range("B1"))

qt.Refresh

If oRS.State <> adStateClosed Then
    oRS.Close
End If


If Not oRS Is Nothing Then Set oRS = Nothing
If Not oCN Is Nothing Then Set oCN = Nothing

End Sub

Regards
Shasur

http://www.dotnetdud.blogspot.com

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old August 29th, 2007, 03:51 AM
Registered User
 
Join Date: Aug 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Thanks heaps for your reply, however I get a Compile Error; User Defined type not defined.

Thanks man

 
Old August 29th, 2007, 04:02 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Have you added the ActiveX Data Objects in your reference. It might be probably because of that.

Try adding the reference and if the error still comes, please post the line where the error comes

Cheers
Shasur


http://www.dotnetdud.blogspot.com

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old August 29th, 2007, 06:03 AM
Registered User
 
Join Date: Aug 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey, thanks for that Shasur,

However, it will only get the file I specify, but will bring in all fields of that file ok, so next question;

Quote:
quote:can I specify a row to start importing from? .. and also is there a way of using a wild card to get ALL excel files merged?
Many thanks again;)
 
Old August 29th, 2007, 06:14 AM
Registered User
 
Join Date: Aug 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sub Excel_QueryTable()

Dim oCN As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim ConnString As String
Dim SQL As String

Dim qt As QueryTable

ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\A555714\Desktop\New App\spreadsheet import\try\1copy.xls;Extended Properties=Excel 8.0;Persist Security Info=False"
Set oCN = New ADODB.Connection
oCN.ConnectionString = ConnString
oCN.Open

SQL = "Select * from [RCM$]"

Set oRS = New ADODB.Recordset
oRS.Source = SQL
oRS.ActiveConnection = oCN
oRS.Open

Set qt = Worksheets(1).QueryTables.Add(Connection:=oRS, _
        Destination:=Range("B1"))

qt.Refresh

If oRS.State <> adStateClosed Then
    oRS.Close
End If


If Not oRS Is Nothing Then Set oRS = Nothing
If Not oCN Is Nothing Then Set oCN = Nothing

End Sub

 
Old August 29th, 2007, 08:03 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

You can filter the columns using the query

If you want to select a range here is the hint

"Select Min([Entry Date]) as MinDate From [" & sSheetName & "$A13:J" & CStr(lMaxRow) & "]"

where lMAxRow is my ending row and sSheetName is the sheet I wanted.

WildCards .. hmm .. I haven't tried it yet

http://www.dotnetdud.blogspot.com

VBA Tips & Tricks (http://www.vbadud.blogspot.com)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting data from excel spreadsheets Neil1234567 Access VBA 2 October 16th, 2007 07:28 AM
Pasting into spreadsheets IainAL VB How-To 0 April 23rd, 2007 04:33 AM
Extracting data from Spreadsheets in a folder IainAL VB How-To 2 April 20th, 2007 02:57 AM
Insert filename in several spreadsheets nori233 Excel VBA 1 January 19th, 2007 09:33 AM
Import Multiple Unknown Excel Spreadsheets in .Net bcarmen Excel VBA 1 January 31st, 2005 04:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.