473,394 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Compare 2 excel worksheet, consolidate differences in 3rd sheet

Hi All,

I would like some help regarding my code. I want a function that compares 2 excel sheets in a workbook and consolidate their differences in the 3rd sheet. Right now my code can compare the 2 sheets and highlight their differences. All i want in the 3rd sheet is to show what are the changes, e.g (added data, deleted data, changed data). Thanks in advance for helping me :D Here is my code :
Expand|Select|Wrap|Line Numbers
  1. Dim xlApp As New Excel.Application
  2.         Dim xlPreviousWorkBook, xlCurrentWorkbook, xlOutputWorkBook As Excel.Workbook
  3.         Dim xlPreviousWorkSheet, xlCurrentWorksheet, xlPreviousOutputWorksheet, xlCurrentOutputWorksheet, xlResultWorksheet As Excel.Worksheet
  4.         Dim xlPreviousRange, xlCurrentRange, xlPreviousDestRange, xlCurrentDestRange, xlResultRange As Excel.Range
  5.         Dim lastline As String
  6.  
  7.  
  8.  
  9.         ' Opens Previous Workbook. Change path and filename as applicable
  10.         xlPreviousWorkBook = xlApp.Workbooks.Open("C:\Users\Desktop\Previous Version\cola")
  11.  
  12.         'Opens Cuurent Workbook.  Change path and filename as applicable
  13.         xlCurrentWorkbook = xlApp.Workbooks.Open("C:\Users\Desktop\Current Version\cola2")
  14.  
  15.         ' Opens Destination Workbook. Change path and filename as applicable
  16.         xlOutputWorkBook = xlApp.Workbooks.Open("C:\Users\Desktop\Output File Location\Differences")
  17.  
  18.         ' Display Excel
  19.         xlApp.Visible = True
  20.  
  21.         ' Set the previous worksheet
  22.         xlPreviousWorkSheet = xlPreviousWorkBook.Sheets("Sheet1")
  23.         ' Set the current worksheet
  24.         xlCurrentWorksheet = xlCurrentWorkbook.Sheets("Sheet1")
  25.         ' Set the destination worksheet
  26.         xlPreviousOutputWorksheet = xlOutputWorkBook.Sheets("Sheet1")
  27.         xlCurrentOutputWorksheet = xlOutputWorkBook.Sheets("Sheet2")
  28.         xlResultWorksheet = xlOutputWorkBook.Sheets("Sheet3")
  29.  
  30.         ' Set the previous range
  31.         xlPreviousRange = xlPreviousWorkSheet.UsedRange
  32.         ' Set the current range
  33.         xlCurrentRange = xlCurrentWorksheet.UsedRange
  34.         ' Set the previous destination range
  35.         xlPreviousDestRange = xlPreviousOutputWorksheet.UsedRange
  36.         ' Set the current destination range
  37.         xlCurrentDestRange = xlCurrentOutputWorksheet.UsedRange
  38.         ' Set the result destination range
  39.         xlResultRange = xlResultWorksheet.UsedRange
  40.  
  41.         ' Copy and paste the range
  42.         xlPreviousRange.Copy(xlPreviousDestRange)
  43.         xlCurrentRange.Copy(xlCurrentDestRange)
  44.  
  45.         ' Compare sheet1 and sheet2 but only highlights the difference between the two sheets
  46.         Try
  47.             lastline = Math.Max(xlPreviousOutputWorksheet.UsedRange.Rows.Count, xlCurrentOutputWorksheet.UsedRange.Rows.Count)
  48.  
  49.             For row As Integer = 1 To lastline
  50.                 Dim lastcol As Integer = Math.Max(xlPreviousOutputWorksheet.UsedRange.Rows.Count, xlCurrentOutputWorksheet.UsedRange.Rows.Count)
  51.                 For column As Integer = 1 To lastcol
  52.  
  53.                     ' Highlights the difference on both sheets
  54.                     If xlPreviousOutputWorksheet.Cells(row, column).Value <> xlCurrentOutputWorksheet.Cells(row, column).Value Then
  55.  
  56.                         DirectCast(xlPreviousOutputWorksheet.Cells(row, column), Excel.Range).Interior.Color = 5296274
  57.                         DirectCast(xlCurrentOutputWorksheet.Cells(row, column), Excel.Range).Interior.Color = 5296274
  58.  
  59.                     End If
  60.  
  61.                 Next
  62.             Next
  63.  
  64.  ' Save and close excel apps
  65.             xlOutputWorkBook.Save()
  66.             xlOutputWorkBook.Close()
  67.             xlPreviousWorkBook.Close()
  68.             xlCurrentWorkbook.Close()
  69.             xlApp.Quit()
  70.  
  71.             MessageBox.Show("Finished Processing")
  72.  
  73.         Catch ex As Exception
  74.             MsgBox("ERROR")
  75.         End Try
  76.     End Sub
  77.  
  78.  
Jun 13 '14 #1
3 4589
Luk3r
300 256MB
When your program highlights a line, you should also just add that line to a list of strings and then loop through the list of strings and write each string to your new workbook.
Jun 13 '14 #2
good day Luk3r. i will try to do what you are suggesting. thank you. i'll get back to you if there any progress on my code.
Jun 16 '14 #3
hi again actually what im trying to do is to consolidate the difference in the 3rd sheet.

for example:

in the cells 3rd sheet it shows,

Added - present in sheet 2 but not in sheet 1

Deleted - present in sheet 1 but not in sheet 2

Changed - there is value on same cell in both sheet but the values are different
Jun 17 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ishan Bhalla | last post by:
Hello all, Am not sure if this is the right newgroup. I want to open and excel worksheet from VB.Net, populate data and save it. Any idea how it can be done? Thanks in advance Regards
5
by: Elena | last post by:
I need the VB.NET code to change the header/footer of an Excel spread sheet, ASAP. I am doing it through a VB application. I can change the ranges/cell values using code, but I do not know how to...
18
by: Frank M. Walter | last post by:
Hello, I have made an small AddIn with udf for excel 2003. I use vs2003. The point of view is the function __T() I call it in excel sheet writing =__T() I am not able to set a value to a...
0
by: vinidimple | last post by:
Hi i have a serious problem while i was working in Excel.I want to fetch columns from an excel worksheet and i need to compare it with an sql querry fields,so i tried to open an excle...
0
by: Craigbob | last post by:
Hi all, I'm using the code below to open a work book and move to a particular worksheet in the work book. The problem arises in the Else Clause of the IF Then statement. I'm not sure how to set...
5
by: hmiller | last post by:
Hey there folks: I have been trying to get this work for about a week now. I'm new to VBA... I am trying to transfer a populated table in Access to an existing, but blank, Excel worksheet. I...
0
by: jwmaiden | last post by:
Have an application that opens up a webbrowser control to display data in an Excel spreadsheet (using VB.NET in VS 2005). No problem with opening the spreadsheet or displaying the data, but I'm...
0
by: bfrank1972 | last post by:
I want to be able to get a list of all custom named fields in an Excel worksheet, but I am having trouble with this. In the code below, access to a field that I named "DEALCODE" works fine - I get...
1
by: Joe Humburg | last post by:
Hi everyone, Looking for some help or ideas, on the folloiwng problem. Have an Access 2003 application that creates an Excel file containing data from an Access parameter query. This is...
0
by: titli | last post by:
I have created the excel worksheet from MS Access using the following code: Dim xlApp As Excel.Application Dim xlWB As Excel.Workbook Set xlApp = New Excel.Application With xlApp ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.