I have an excel workbook with two sheets. Each has exactly the same format in terms of columns. The columns are ColA - ID ColB - Desc/dept ColC - Name
Sheet2 has all the information in all columns. Sheet1 only has information in ColC.
I would like to write VBA code to compare the values in Column C for both sheets and if it matches (as they might not be in the same order or even the same number of records) then the code copies the ID and Desc from columns A and B of Sheet2(forId) into Columns A and B of sheet1(1099(2)). I wrote the following but it just runs and excel stops responding after I click on it. When I select to restart excel, nothing changed.
Thanks for your help.
Sub insertId()
Dim Rng As Range
Dim compRngI As Range, compRngO As Range
Dim wsI As Worksheet, wsO As Worksheet
Dim x As Variant, y As Variant
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
'setting sheet where id to be checked
Set wsI = ThisWorkbook.Sheets("forId")
'set sheet where id to be pasted
Set wsO = ThisWorkbook.Sheets("1099(2)")
'Set ranges to be compared between sheets
With wsI
lastRow = .Range("C" & .Rows.Count).End(xlUp).Row
Set compRngI = .Range("C1:C" & lastRow)
End With
With wsO
lastRow = .Range("C" & .Rows.Count).End(xlUp).Row
Set compRngO = .Range("C1:C" & lastRow)
End With
'compare cells in both columns C of each spreadsheet
For Each x In compRngI
For Each y In compRngO
If x = y Then
y.Offset(0, -2).Value = x.Offset(0, -2).Value
y.Offset(0, -1).Value = x.Offset(0, -1).Value
End If
Next y
Next x
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With
End Sub