radsok8199
New Member
- Joined
- Dec 4, 2020
- Messages
- 24
- Office Version
- 2016
- Platform
- Windows
- MacOS
- Mobile
Dear VBA Masters
I tried to find solution within forum and manage to create code, but can not get what have i done wrong
I have two sets of data on same worksheet. Both looks similar. Have same number of rows and columns ( column headers in same order). Headers are same. Amount of rows and Headers for both is dynamic. Values for each cell in both should match, however it never does. That's why i need to have this kind of comparison.
So Set of data on left side that's values from Requirements where the middle from Extract. I want my code to compare all cells from Requirements with Extract and place value TRUE or FALSE and display them right of Extract data under correct Header.
Some of rows between Extract data contains #N/A as result of previous VLOOKUP function. I need to keep those and display comparison result as #N/A instead of FALSE
Please let me know what i am doing wrong here?
I tried to find solution within forum and manage to create code, but can not get what have i done wrong
I have two sets of data on same worksheet. Both looks similar. Have same number of rows and columns ( column headers in same order). Headers are same. Amount of rows and Headers for both is dynamic. Values for each cell in both should match, however it never does. That's why i need to have this kind of comparison.
So Set of data on left side that's values from Requirements where the middle from Extract. I want my code to compare all cells from Requirements with Extract and place value TRUE or FALSE and display them right of Extract data under correct Header.
Some of rows between Extract data contains #N/A as result of previous VLOOKUP function. I need to keep those and display comparison result as #N/A instead of FALSE
Please let me know what i am doing wrong here?
VBA Code:
Sub CompareCells()
Dim ws As Worksheet
Dim j As Long
Dim i As Long
Dim lrowR As Long
Dim lcolR As Long
Dim lcolE As Long
Dim LvalueR As String
Dim LvalueE As String
Dim CompInput As Range
Dim fcolE As Long
Set ws = ThisWorkbook.Sheets("Comparison")
lrowR = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' Last row requirments
lcolR = ws.Cells(1, 1).End(xlToRight).Column ' Last column requirments
For j = 2 To lcolR
For i = 2 To lrowR Step 1
LvalueR = ws.Cells(i, 1).Value 'Requirments look up value
fcolE = lcolR + j
LvalueE = ws.Cells(i, fcolE).Value 'Extract lookup value
lcolE = ws.Cells(1, lcolR + 2).End(xlToRight).Column 'Extract last column
CompInput = ws.Cells(i, lcolE + j) 'Comparison result first cell
If IsError(LvalueE) Then
CompInput.Value = "#N/A"
If LvalueR = LvalueE Then
CompInput.Value = "True"
Else
CompInput.Value = "False"
End If
End If
Next i
Next j
End Sub