Read a value from spreadsheet X, compare adjacent values between spreadsheets X and Y
I have an Macro Based Excel file that generates a list of items received and their status (i.e. received, repaired, etc). This program runs daily, and right now I have it capture the previous day's list and place it in a spreadsheet called PreviousData before updating with the current day's list, which is placed in a spreadsheet called Data; this is used to compare what we believe we fixed/changed status on the previous day.
I'm basically self taught in VBA, so I'm not super efficient or experienced. What I want to do is the following:
On the Data Spreadsheet, grab the order number starting in J2
Switch to the PreviousData Spreadsheet, and search for the order number from step 1
Scenario A: If the order number is found on PreviousData, compare the status values next to the order number on both sheets; if they differ, run some code otherwise do nothing
Scenario B: If the order number is not found on PreviousData, do nothing
- Repeat until 1st blank cell encountered in Data Spreadsheet
I did some searching around the interwebs and found something (it might have been from this forum, actually) that would go row by row and compare cell values, but if scenario B came up the function would fail with "out of range." Here is the code I tried and have modified to try to get to work:
Sub output() Dim varSheetA As Variant Dim varSheetB As Variant Dim varSheetRMA As Variant Dim strRangeToCheck As String Dim strRangeRMA As String Dim Variable As String Dim iRow As Long Dim iCol As Long Dim Count As Integer strRangeToCheck = "K2:L1000" strRangeRMA = "J2:J1000" ' If you know the data will only be in a smaller range, reduce the size of the ranges above. Debug.Print Now varSheetA = Worksheets("PreviousData").Range(strRangeToCheck) varSheetB = Worksheets("Data").Range(strRangeToCheck) ' or whatever your other sheet is. varSheetRMA = Worksheets("Data").Range(strRangeRMA) Debug.Print Now Sheets("Data").Select Range("J2").Select Selection.Copy Sheets("PreviousData").Select Cells.Find(What:=Variable, After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False).Activate For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1) For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2) If varSheetA(iRow, iCol) = varSheetB(iRow, iCol) Then ' Cells are identical. ' Do nothing. Else ' Cells are different. ' Code goes here for whatever it is you want to do. End If Next iCol Next iRow End Sub
Please help :)
This code should be easier to understand + it does the job.
Option Explicit Sub CompareStatuses() Dim ws1 As Worksheet, ws2 As Worksheet, rng1 As Range, rng2 As Range Dim lr1&, lr2&, i&, j& Set ws1 = ThisWorkbook.Sheets("Data") Set ws2 = ThisWorkbook.Sheets("PreviousData") lr1 = ws1.Range("J" & Rows.Count).End(xlUp).Row lr2 = ws2.Range("J" & Rows.Count).End(xlUp).Row For i = 2 To lr1 For j = 2 To lr2 Set rng1 = ws1.Range("J" & i) Set rng2 = ws2.Range("J" & j) If StrComp(CStr(rng1.Value), CStr(rng2.Value), vbTextCompare) = 0 And _ StrComp(CStr(rng1.Offset(0, 1).Value), CStr(rng2.Offset(0, 1).Value) _ ,vbTextCompare) <> 0 Then ' found a matching Order + both statuses are different ' this is where you wanted to run some code End If Set rng1 = Nothing Set rng2 = Nothing Next j Next i End Sub