Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default comparing worksheets


Hi

I have two worksheets, which both have similar data.

I need to compare sheet1 to sheet2 and the data that in sheet1 that is
not in sheet2 place into sheet3.

e.g.

sht1 sht2 sheet3
aaa aaa ddd
bbb bbb
ccc ccc
ddd eee

Please help!

I tried the compare macro in forum, but error occurs and cannot debug!


--
unsworthcl
------------------------------------------------------------------------
unsworthcl's Profile: http://www.excelforum.com/member.php...o&userid=23946
View this thread: http://www.excelforum.com/showthread...hreadid=375745

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default comparing worksheets


This macro is a modified version of a similar solution I provided (se
below for link) that should work for you:

Code
-------------------
Sub compareSheets()
' Declare variables/data types...
Dim origFile, origSheet, copySheet As Worksheet
Dim origRange, copyRange, compRange, errLoc As String
Dim x, y, compCount, errCount, iRow As Long
Dim origRows, minOrigR, minOrigC, minCopyR, minCopyC As Long
Dim copyRows, rowLim, colLim, rowMin, colMin, compMin, compLim As Long
Dim origCols, copyCols As Integer
Dim origVal, copyVal As Variant
Dim Msg, Title As String, Style, Response As Variant
Dim errArray() As Variant

' Set 'original' workbook variable...
Set origFile = ActiveWorkbook
' Compare sheet 1 vs. sheet 2...
Set origSheet = origFile.Sheets(1)
Set copySheet = origFile.Sheets(2)
' Get 'original' data range (in "A1" format)...
origRange = origSheet.UsedRange.Address(RowAbsolute:=False, ColumnAbsolute:=False)
' Get 'copy' data range (in "A1" format)...
copyRange = copySheet.UsedRange.Address(RowAbsolute:=False, ColumnAbsolute:=False)
' Get 'original' & 'copy' data range limits to process...
origRows = origSheet.UsedRange.Rows.Count
origCols = origSheet.UsedRange.Columns.Count
minOrigR = origSheet.UsedRange.Cells(1, 1).Row
minOrigC = origSheet.UsedRange.Cells(1, 1).Column
copyRows = copySheet.UsedRange.Rows.Count
copyCols = copySheet.UsedRange.Columns.Count
minCopyR = copySheet.UsedRange.Cells(1, 1).Row
minCopyC = copySheet.UsedRange.Cells(1, 1).Column
' Determine data range 'size' and adjust range to ensure comparison
' will be accurate (use the greatest row & column count)...
rowLim = Application.WorksheetFunction.Max(origRows, copyRows)
colLim = Application.WorksheetFunction.Max(origCols, copyCols)
rowMin = Application.WorksheetFunction.Min(minOrigR, minCopyR)
colMin = Application.WorksheetFunction.Min(minOrigC, minCopyC)
compMin = Application.WorksheetFunction.Min(rowMin, colMin)
compLim = Application.WorksheetFunction.Max(RowMax, ColMax)
compRange = origSheet.Range(origSheet.Cells(rowMin, colMin), _
origSheet.Cells(rowLim, colLim)).Address(RowAbsolute:=False, ColumnAbsolute:=False)
' Initialize mismatch counter...
errCount = 0
' Initialize comparison counter...
compCount = 0
' Loop through each cell in 'resized' data range by row index...
For x = 1 To rowLim
' Loop through each cell in 'resized' data range by column index...
For y = 1 To colLim
' Start comparison counter...
compCount = compCount + 1
' Perform comparison & load array if compared cells differ...
If origSheet.Cells(x, y).Value < copySheet.Cells(x, y).Value Then
' Increment mismatch counter...
errCount = errCount + 1
' If 'original' value is blank, assign it to variable...
If origSheet.Cells(x, y).Value = "" Then
origVal = "<blank"
Else
' Otherwise, use 'original' value...
origVal = origSheet.Cells(x, y).Value
End If
' If 'copy' cell is blank, assign it to variable...
If copySheet.Cells(x, y).Value = "" Then
copyVal = "<blank"
Else
' Otherwise, use 'copy' value...
copyVal = copySheet.Cells(x, y).Value
End If
' Redimension array that stores mismatches (add 1st row)
If errCount = 1 Then
ReDim errArray(1)
Else
' Retain existing array data and add new row to array...
ReDim Preserve errArray(UBound(errArray) + 1)
End If
' Add mismatch info (using variable) to array by subtracting 1
' from mismatch count to equal row index of array (Option Base 0)
errArray(UBound(errArray) - 1) = origVal
End If
' Loop to next column in 'resized' data range...
Next y
' Loop to next row in 'resized' data range...
Next x
' If differences exist, create new sheet and list them...
If errCount 0 Then
ThisWorkbook.Worksheets.Add After:=ThisWorkbook.Worksheets(2)
For iRow = 0 To UBound(errArray)
ActiveSheet.Cells(iRow + 1, 1).Value = errArray(iRow)
Next iRow
Else
' Otherwise, alert user no differences were found...
Msg = "No differences were found in the comparison."
Style = vbOKOnly + vbInformation + vbDefaultButton1
Title = "File Comparison Results"
Response = MsgBox(Msg, Style, Title)
End If
End Sub
--------------------

For the original solution, here’s the link:
http://www.excelforum.com/showthread...417#post955417

Hope this helps,
theDude


--
theDude
------------------------------------------------------------------------
theDude's Profile: http://www.excelforum.com/member.php...o&userid=16550
View this thread: http://www.excelforum.com/showthread...hreadid=375745

Reply
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
Comparing two worksheets Jules Excel Worksheet Functions 1 October 5th 07 10:32 PM
Comparing 5 worksheets thewildleo Excel Worksheet Functions 0 July 27th 06 09:47 AM
Comparing Two Worksheets for changes Jugglertwo Excel Discussion (Misc queries) 1 December 7th 05 08:56 PM
Comparing Across Worksheets Dave F.[_2_] Excel Programming 0 March 4th 04 11:56 PM
Comparing 2 worksheets Smoky2010 Excel Programming 1 February 3rd 04 01:50 PM


All times are GMT +1. The time now is 11:47 PM.

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"