View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Matt Matt is offline
external usenet poster
 
Posts: 516
Default Compare Two Workbooks

I'm trying to write a piece of VBA that will check each cell in a workbook
against each corresponding cell in another workbook, and highlight where
there are differences.

I'm really falling over at the first hurdle, as I cant get my head around
how to reference the cells.

I've got variables that tell me the workbook, the worksheet, the row and the
column, but I don't appear able to so something as simple as check if
wb1.ws1.cell1 = wb2.ws2.cell2.

The code I have so far is below:

Sub test()

Dim wb1 As Workbook
Dim wb1name As String
Dim wb2 As Workbook
Dim wb2name As String
Dim ws1 As Worksheet
Dim ws1name As String
Dim ws2 As Worksheet
Dim ws2name As String
Dim cell1 As Range
Dim cell2 As Range
Dim cell1row As Long
Dim cell1column As Long
Dim cell2row As Long
Dim cell2column As Long
Dim filelocation As String
Dim strCurrentworkbook As String
Dim strpreviousworkbook As String

filelocation = Workbooks("Position Check
Model20100305.xls").Worksheets("phase2").Range("d" & 16)
strCurrentworkbook = Workbooks("Position Check
Model20100305.xls").Worksheets("phase2").Range("d" & 17)
strpreviousworkbook = Workbooks("Position Check
Model20100305.xls").Worksheets("phase2").Range("d" & 18)

'open currentworkbook
ChDir filelocation
Workbooks.Open Filename:=strCurrentworkbook
'open previousworkbook
Workbooks.Open Filename:=strpreviousworkbook

Set wb1 = Workbooks(strCurrentworkbook)
Set wb2 = Workbooks(strpreviousworkbook)

For Each ws1 In wb1.Worksheets
ws1.Activate
ws1name = ActiveSheet.Name
ws2name = ws1name

For Each cell1 In ws1.UsedRange
cell1row = cell1.Row
cell1column = cell1.Column

MsgBox (ws1name & ", " & cell1row & ", " & cell1column)

Next cell1

Next ws1

End Sub