Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
| Search | Today's Posts | Mark Forums Read
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 November 30th, 2006, 08:42 AM
Registered User
 
Join Date: Nov 2006
Location: NY, , USA.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to anup.bihani
Default Comparing two different excel files


Hi,
I have two excel files on the my machine. the name is excel1.xls & excel2.xls. Pls note excel1.xls has two columns with name on spreadsheet : Deptname,Empname & excel2.xls has two columns with name on Spreadsheet : Empname, Salary.(Empname is unique)
I want to compare Empname from two excel sheets(excel1.xls & excel2.xls). using SELECT statement , want the output Deptname,Empname,Salary)in the file final.xls with the same column name.I m writing VBA code in the same file final.xls to do the job.
Is that feasible using VBA to write a select statement to compare the columns Empname like we do in database tables using connection string & recordset objects. If yes then please provide the SELECT statement or any other suggestion with solution is needed.
 
Old November 30th, 2006, 09:27 AM
Authorized User
 
Join Date: Mar 2006
Location: London, , United Kingdom.
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

the easiest way is to merge the two sheets together, then import that data. If that is not possible then you may need to do a 2 stage approach, by getting a list of deptnames & empnames, then getting the salaries for each, depending on how many records you have there may be a performance issue. You could also import the Deptname & empname then use a vlookup to get the salaries

 
Old December 4th, 2006, 02:07 AM
Registered User
 
Join Date: Nov 2006
Location: NY, , USA.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to anup.bihani
Default

Thanks for the Reply,
I can do that merging two sheets & putting the contents on the same sheet on the same excel file but the same can be done using obook & osheet like below without merging :

Dim obook1 as Workbooks
Dim osheet1 as Worksheet
Dim obook2 as Workbooks
Dim osheet2 as Worksheet
Application.ScreenUpdating = False
Set obook1 = Workbooks.Open("File Path of excel1.xls")
Set obook2 = Workbooks.Open("File Path of excel2.xls")
Set osheet1 = obook1.Worksheets(1)
Set osheet2 = obook2.Worksheets(1)

then on comparing the Columns.
but the problem is to get the job done using Connection String & Getting the recordset with the expected output using the query(SELECT statement). Help needed.
 
Old December 4th, 2006, 06:00 AM
Authorized User
 
Join Date: Mar 2006
Location: London, , United Kingdom.
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The problem is you cannot query multiple xls files through the jet drivers.. if it was in one sheet you could use the following. Make sure you reference Microsoft ActiveX Data Objects 2.8 library

dim cn as new ADODB.Connection
dim rs as new ADODB.Recordset
dim StrSQL as String

cn.open "Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=C:\MyExcel.xls;DefaultDi r=c:\mypath;"

strsql = "Select Deptname,Empname,Salary from [Sheet1$]"
rs.open strsql,cn,adOpenStatic,adLockReadOnly

range("A1").copyfromrecordset rs

rs.close
set rs = nothing
cn.close
set cn = nothing






Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing two excel sheets nbkn8ct Excel VBA 5 August 28th, 2009 05:20 PM
Comparing Files... Nick Y BOOK: Ivor Horton's Beginning Visual C++ 2005 0 July 30th, 2006 02:48 PM
comparing files Adam H-W Dreamweaver (all versions) 2 May 13th, 2006 06:15 AM
comparing Two text files rajkumar007 Classic ASP Basics 0 October 15th, 2005 07:22 AM
Comparing Two Text Files rajkumar007 Classic ASP Professional 0 October 14th, 2005 10:01 AM





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