i'm trying to write a code in VBA to copy a cell value based on two criteria.
I'm trying to write a VBA code that will copy case file numbers (= cell values) to another worksheet if they are not present anymore om the worksheet where the daily updated data of case numbers is pasted. I want to filter out those case numbers that I have reviewed in the past and have been deleted (not anymore in the exported data).
I've searched the forum and tried a couple of things, but I keep receiving a error: next without For. I'm still an novice and find a lot of information about this error, but I don't seem to comprehend how to solve my code.
I have two criteria that need to be meet before the cell value may be copied.
Criteria 1: the cell value doesn't exists anymore in the range off daily updated data => This is don't by an excel formula that generates a "1" or "0". If it generates a "1", this means it is missing and this is the first criteria to copy.
Criteria 2: The cell value to copy doesn't already exist in the sheet to copy to. I want to run the code every day and don't want to end up with the same value being copied again and again and again.
My code at the moment looks like this:
Dim shCD As Worksheet, shVD As Worksheet
Dim VDCell As Range
Dim lrwCD As Long, lrwI As Long
Dim foundTrue As Boolean
Dim i As Integer, j As Integer
Application.ScreenUpdating = False
Set shCD = Sheets("Checked Dossiers")
Set shVD = Sheets("Verdwenen Dossiers")
Set VDCell = FreeCell(shVD.Range("A5"))
lrwCD = shCD.Cells(shCD.Rows.Count, "B").End(xlUp).Row
lrwVD = shVD.Cells(shVD.Rows.Count, "A").End(xlUp).Row
foundTrue = False 'my on/off switch to copy or not copy the cell value
For i = 2 To lrwCD
For j = 2 To lrwVD
'checking criteria 1, if this is true set switch to true and check if cell value already exists on sheet shVD in the range (A2:lrwVD)
If shCD.Cells(i, 1).Value = "1" Then
foundTrue = True
If shCD.Cells(i, 2).Value = shVD.Cells(j, 1).Value Then
foundTrue = False ' If the cell value already exist -> set switch to false
Next j
'After comparing cell value to the range (A2:lrwCD) and the switch is still on True -> start the copy command
If foundTrue = True Then
shCD.Cells(i, 2).Copy
VDCell.PasteSpecial Paste:=xlPasteValues
VDCell = VDCell.Offset(1, 0)
foundTrue = False 'set switch back to off (false) to start loop for next i
Next i
Application.ScreenUpdating = True