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 : -
Dim xlApp As New Excel.Application
-
Dim xlPreviousWorkBook, xlCurrentWorkbook, xlOutputWorkBook As Excel.Workbook
-
Dim xlPreviousWorkSheet, xlCurrentWorksheet, xlPreviousOutputWorksheet, xlCurrentOutputWorksheet, xlResultWorksheet As Excel.Worksheet
-
Dim xlPreviousRange, xlCurrentRange, xlPreviousDestRange, xlCurrentDestRange, xlResultRange As Excel.Range
-
Dim lastline As String
-
-
-
-
' Opens Previous Workbook. Change path and filename as applicable
-
xlPreviousWorkBook = xlApp.Workbooks.Open("C:\Users\Desktop\Previous Version\cola")
-
-
'Opens Cuurent Workbook. Change path and filename as applicable
-
xlCurrentWorkbook = xlApp.Workbooks.Open("C:\Users\Desktop\Current Version\cola2")
-
-
' Opens Destination Workbook. Change path and filename as applicable
-
xlOutputWorkBook = xlApp.Workbooks.Open("C:\Users\Desktop\Output File Location\Differences")
-
-
' Display Excel
-
xlApp.Visible = True
-
-
' Set the previous worksheet
-
xlPreviousWorkSheet = xlPreviousWorkBook.Sheets("Sheet1")
-
' Set the current worksheet
-
xlCurrentWorksheet = xlCurrentWorkbook.Sheets("Sheet1")
-
' Set the destination worksheet
-
xlPreviousOutputWorksheet = xlOutputWorkBook.Sheets("Sheet1")
-
xlCurrentOutputWorksheet = xlOutputWorkBook.Sheets("Sheet2")
-
xlResultWorksheet = xlOutputWorkBook.Sheets("Sheet3")
-
-
' Set the previous range
-
xlPreviousRange = xlPreviousWorkSheet.UsedRange
-
' Set the current range
-
xlCurrentRange = xlCurrentWorksheet.UsedRange
-
' Set the previous destination range
-
xlPreviousDestRange = xlPreviousOutputWorksheet.UsedRange
-
' Set the current destination range
-
xlCurrentDestRange = xlCurrentOutputWorksheet.UsedRange
-
' Set the result destination range
-
xlResultRange = xlResultWorksheet.UsedRange
-
-
' Copy and paste the range
-
xlPreviousRange.Copy(xlPreviousDestRange)
-
xlCurrentRange.Copy(xlCurrentDestRange)
-
-
' Compare sheet1 and sheet2 but only highlights the difference between the two sheets
-
Try
-
lastline = Math.Max(xlPreviousOutputWorksheet.UsedRange.Rows.Count, xlCurrentOutputWorksheet.UsedRange.Rows.Count)
-
-
For row As Integer = 1 To lastline
-
Dim lastcol As Integer = Math.Max(xlPreviousOutputWorksheet.UsedRange.Rows.Count, xlCurrentOutputWorksheet.UsedRange.Rows.Count)
-
For column As Integer = 1 To lastcol
-
-
' Highlights the difference on both sheets
-
If xlPreviousOutputWorksheet.Cells(row, column).Value <> xlCurrentOutputWorksheet.Cells(row, column).Value Then
-
-
DirectCast(xlPreviousOutputWorksheet.Cells(row, column), Excel.Range).Interior.Color = 5296274
-
DirectCast(xlCurrentOutputWorksheet.Cells(row, column), Excel.Range).Interior.Color = 5296274
-
-
End If
-
-
Next
-
Next
-
-
' Save and close excel apps
-
xlOutputWorkBook.Save()
-
xlOutputWorkBook.Close()
-
xlPreviousWorkBook.Close()
-
xlCurrentWorkbook.Close()
-
xlApp.Quit()
-
-
MessageBox.Show("Finished Processing")
-
-
Catch ex As Exception
-
MsgBox("ERROR")
-
End Try
-
End Sub
-
-
3 4589
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.
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
| | |