moonlight22
New Member
- Joined
- Aug 15, 2014
- Messages
- 24
Hi,
I have the following macro which I'm using to find a value that is in my ControlSheet in cell B11, in column M of the Datasheet, and then it deletes the row. It also copies the row to the RecordsRemoved tab before completing the macro.
My requirements have changed and now i need this macro to find 4 values, not just B11. The following chart summarizes where the value is in the ControlSheet and which column in the Datasheet it needs to search. I need the same action to occur that is happening today, where any row which has the 4 criteria is deleted from the Datasheet, and the row is copied to the RecordsRemoved.
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]ControlSheet
[/TD]
[TD]DataSheet[/TD]
[/TR]
[TR]
[TD]I12[/TD]
[TD]Column B[/TD]
[/TR]
[TR]
[TD]I13[/TD]
[TD]Column F[/TD]
[/TR]
[TR]
[TD]I14[/TD]
[TD]Column I[/TD]
[/TR]
[TR]
[TD]I15[/TD]
[TD]Column J[/TD]
[/TR]
</tbody>[/TABLE]
I could not figure out the loop...
any help will be greatly appreciated!
I have the following macro which I'm using to find a value that is in my ControlSheet in cell B11, in column M of the Datasheet, and then it deletes the row. It also copies the row to the RecordsRemoved tab before completing the macro.
My requirements have changed and now i need this macro to find 4 values, not just B11. The following chart summarizes where the value is in the ControlSheet and which column in the Datasheet it needs to search. I need the same action to occur that is happening today, where any row which has the 4 criteria is deleted from the Datasheet, and the row is copied to the RecordsRemoved.
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]ControlSheet
[/TD]
[TD]DataSheet[/TD]
[/TR]
[TR]
[TD]I12[/TD]
[TD]Column B[/TD]
[/TR]
[TR]
[TD]I13[/TD]
[TD]Column F[/TD]
[/TR]
[TR]
[TD]I14[/TD]
[TD]Column I[/TD]
[/TR]
[TR]
[TD]I15[/TD]
[TD]Column J[/TD]
[/TR]
</tbody>[/TABLE]
I could not figure out the loop...
any help will be greatly appreciated!
Code:
Sub DeletingRows()
Dim Rng As Variant, CriteriaValue As String
CriteriaValue = Sheets("ControlSheet").Range("B11").Value
If CriteriaValue = "" Then
MsgBox "No Criteria Entered", vbOKOnly
Exit Sub
End If
Err.Clear
On Error Resume Next
Rng = Sheets("DataSheet").Range("M:M").Find(CriteriaValue, Sheets("DataSheet").Range("M1"), xlValues, xlWhole)
If Err.Number > 0 Then
MsgBox "No instances were found.", vbOKOnly, "Nothing Found"
Exit Sub
End If
With Sheets("DataSheet")
Do Until Rng Is Nothing
Set Rng = Nothing
Set Rng = .Range("M:M").Find(CriteriaValue, .Range("M1"), xlValues, xlWhole)
If Not Rng Is Nothing Then
Rng.EntireRow.Copy
Sheets("Records Removed").Range("A" & Sheets("Records Removed").Range("M" & Rows.Count).End(xlUp).Row + 1).PasteSpecial
Rng.EntireRow.Delete
End If
Loop
End With
End Sub