I am trying to copy data from "sheet 1 A1" to "sheet 2 A1" only when "sheet 1 B1" contains an "x". Prior to copying that data I want to clear "sheet 2" and start fresh.
Code is below. The code copies correctly on the first run but deletes "x" out of sheet 1 then only copies the last value on consecutive runs. Any help is appreciated.
Sub x
Dim xRg As Range
Dim xCell As Range
Dim I As Long
Dim J As Long
Dim K As Long
I = Worksheets("Sheet1").UsedRange.Rows.Count
J = Worksheets("Sheet2").UsedRange.Rows.Count
If J = 1 Then
If Application.WorksheetFunction.CountA(Worksheets("Sheet2").UsedRange) = 0 Then J = 0
End If
Set xRg = Worksheets("Sheet1").Range("B1:B" & I)
On Error Resume Next
Application.ScreenUpdating = False
For K = 1 To xRg.Count
If CStr(xRg(K).Value) = "x" Then
xRg(J).Cells.Clear
If CStr(xRg(K).Value) = "x" Then
xRg(K).EntireRow.Copy Destination:=Worksheets("Sheet2").Range("A" & J + 1)
If CStr(xRg(K).Value) = "x" Then
K = K - 1
End If
J = J + 1
End If
End If
Next
Application.ScreenUpdating = True
End Sub