I am trying to write some VBA code to check numbers in one worksheet against numbers in another worksheet. I am trying to get excel to hide rows in one worksheet, if it is not present in another worksheet. I have written some code but I can not seem to get it functioning, any advice would be appreciated. The VBA code is attached underneath.
Sub HideCells()
Dim xlRange As Range
Dim xlCell As Range
Dim xlSheet As Worksheet
Dim valueToFind
Dim i As Long
For i = 2 To Rows.Count
valueToFind = Sheets("køb total").Cells(i, 1).Value
Set xlSheet = ActiveWorkbook.Worksheets("Køb VT nummer")
Set xlRange = xlSheet.Range("A1:A50000")
For Each xlCell In xlRange
If xlCell.Value = valueToFind Then
Else
Worksheets("Køb total").Rows("i").EntireRow.Hidden = True
End If
Next xlCell
Next i
End Sub
Couple changes - removed the quotation marks from around
i
in yourIf
statement, moved someSet
s outside the loop, and changed yourIf/Else
statement to one case ofIf Not
:Although I think you still need to replace
Rows.Count
withlastrow
, you should make sure that is equaling what you think it is (by stepping through your code with F8, running your cursor overRows.Count
and seeing what its value is).