Question: How to find all cells in column G which are not contained in column A?
Situation: I have 2 columns of data. They consist (apart from +/- 3000 rows) of the same data. One of the columns is approximatly 7k rows, the other is about 10k rows long. There are no blank spaces. Both columns consist of cells of 10 digit numbers and are ordered ascending. A cellvalue in column A may or may not be in column G and vice versa.
I need to delete all cells in column G which are not contained in column A.
I have tried the following:
Sub Delete_rows()
Dim p As Long
Dim LastRow As Long
Dim g As Long
Worksheets("Vergleich").Activate
Range("A2").Select
LastRow = Cells.SpecialCells(xlCellTypeLastCell).row
For g = 1 To LastRow
'ActiveCell is the first cell in column A, 6 positions to the right is
'column G
If ActiveCell.value = ActiveCell.Offset(0, 6).value Then
ActiveCell.Offset(1, 0).Select
Else
'As I ve said there are numbers in column A that are no contained in
'column G which is why I ve tried to work arround this one but ofc
'this is not an elegant solution.
If ActiveCell.value = 4225201001# Then
ActiveCell.Offset(1, 0).Select
Else
'selects the cell in column G that is not contained in column A.
Range(ActiveCell.Offset(0, 6), ActiveCell.Offset(0, 9)).Select
Selection.Delete
ActiveCell.Offset(0, -6).Select
End If
End If
Next g
End Sub
This doesnt work since it doesnt take the few values in column A that are not contained in column G into account.
Note that it is possible that there are repeating numbers in a column. I know that i have a lot of selects and activecells in my code but keep in mind i m fairly new to vba and i find it easier to debug the code this way since you can visually see what the program is doing step by step.
Since the excel file is quite big i need an efficient solution to my problem, othwerwise the macro will run forever.
Thanks in advance for your help.
Best regards
Here is a screenshot after running the recommended macro.