Vlookup loop for highlighting changes

Claptrap

New Member
Joined
Mar 26, 2017
Messages
1
Hello everyone!

I have been trying to construct a VBA code for the following problem:
I have several workbooks, each containing mutiple sheets with rather big tables (always 53 columns but varying number of rows) and these data sheets are getting updated every month (changes are done by multiple persons). Each month is saved as a separate sheet. After this update, I have to quick-check the data entries for validity and for this I would like to highlight (cell colour) those cells which have been changed. The table contains text, values, dates and formulas and some cells are also blank.

Non-VBA solutions I tried:
- track changes option but the small markings in the cell corners are not visible enough in my opinion
- conditional formatting linked to cell names/positions (A1,F6 etc) but this does not work if rows are changing as this causes the cell names to change

My VBA approach:

My idea is to use the cells in the A1 column of each sheet as identifiers as their content is unique (kind of a project name) and then compare the content of the cells to the right, marking changed contents with a colour. That way I can avoid the problem of changing row numbers.So I thought of using a vlookup function, starting with the first identifier in A2, looping through the 53 columns in that row and then repeating that for every sheet row until the end. As I am new to VBA and still lacking knowledge in many areas, I searched different internet forums and tried to learn from the examples I found there, but I cannot get it to work and it is getting frustrating :confused: Maybe there is also a much easier solution to my problem which I totally overlooked...

Code:
Sub lookup_Easy()


 Dim i As Long      'number of rows
 Dim j As Integer   'number of columns
 Dim Shname1 As String
 Dim Shname2 As String
 Dim myRange As Range
 Dim cell As Range
 Dim ret1
 Dim ret2
 Dim ID As Range
 Dim rownumber
 
 Shname1 = InputBox("Enter name of sheet containing NEW data")
 Shname2 = InputBox("Enter name of sheet containing OLD data")


 Set myRange = Sheets(Shname2).Range("A2:BB500") 'I would prefer to have this set to last used cell (BBX) but the last cell could be a blank so I don't know how to do this
 
      i = 2
      j = 1
     Set ID = Sheets(Shname1).Range("A2") 'start with ID as A2
     
    Do Until i = 500
            
      Do Until j = 55
        Set cell = Sheets(Shname1).Range(i, j)
     
       
       rownumber = Application.WorksheetFunction.Match(ID, Sheets(Shname2).Range("A:A"), 0) 'find rownumber
            
        ret1 = cell.Value
        ret2 = Application.WorksheetFunction.VLookup(cell, Sheets(Shname2).Range(rownumber:rownumber), j, 0)
     
        If ret1 <> ret2 Then
          cell.Interior.Color = RGB(255, 51, 204)
        End If
    
      j = j + 1
      Loop
         
    Set ID = ID.Offset(i / i, 0)
    i = i + 1
   Loop
    
        
End Sub

I know the current code does not have any error handling; I experimented with that but it did not work properly, especially when I tried to have several error handlers e.g. for sheet names and later the vlookup, so I left this out for this post here. By chance any tutorials/videos you can recommend for learning that?
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Forum statistics

Threads
1,215,461
Messages
6,124,957
Members
449,200
Latest member
indiansth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top