0

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.

Sheet 1 example

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
Jake
  • 1
  • 1
  • Make life easy and just Filter for X and then copy the resultant visible cells. [This Post](https://stackoverflow.com/a/60660686/6706419) does exactly that – urdearboy Feb 25 '21 at 19:57

2 Answers2

0

maybe you will need some changes, but I hope improve my answer

Sub FilterAndCopy()
    Dim SheetSource As String, SheetDestination As String
    Dim nRowsSource As String
    Dim SourceWithHeaders As Boolean
    
    SheetSource = "Sheet 2"
    SheetDestination = "Hoja3"
    SourceWithHeaders = False
    
    Sheets(SheetSource).Select
    If SourceWithHeaders = False Then
        Rows("1:1").Select
        Selection.Insert Shift:=xlDown
        Range("A1") = "header 1"
        Range("B1") = "header 2"
    End If
    nRowsSource = CountRowsWithData(SheetSource, 1) '1 = A, 2=B, 3=C etc
    Range("B1").Select
    Selection.AutoFilter
    'Change A1:Bn as you need
    ActiveSheet.Range("$A$1:$B$" & nRowsSource).AutoFilter Field:=2,    Criteria1:="x" '<- X or your criteria
    Rows("1:1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets(SheetDestination).Select
    Range("A1").Select 'or
    'Range(FindNextEmptyRow(SheetDestination, "A")).Select 'or
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Sheets(SheetSource).Select
    Range("A1").Select 'or
    Selection.AutoFilter
End Sub

Public Function CountRowsWithData(SheetToCheck_ As String, columnToCheck_ As Double) As Double
    Sheets(SheetToCheck_).Select
    Cells(1, columnToCheck_).Select
    CountRowsWithData = Cells(Rows.Count, 1).End(xlUp).Row
End Function
Public Function FindNextEmptyRow(SheetToLookUp_ As String, columnToCheck_ As            String) As String
    Dim nRows  As Double
    Sheets(SheetToLookUp_).Select
    Range(columnToCheck_ & "1").Select
    nRows = Cells(Rows.Count, 1).End(xlUp).Row + 1
    FindNextEmptyRow = columnToCheck_ & CStr(nRows)
End Function
  • Miguel, Thanks for the reply above. One thing I would like to do with this code is to only copy the items I've identified with an "x" in the "identifier" cell. I have a list of 3-4k items so I would like to quickly identify the items I've checked ("x"ed) in a quick list so I don't have to scroll through the whole list. Jake – Jake Feb 26 '21 at 00:45
0

Something like this, perhaps.

Sub CopyData10()
Dim Rng As Range, cell As Range
Dim rw As Long
Set Rng = Worksheets("Sheet1").Range("B1:B10")
rw = 1
    For Each cell In Rng
        If LCase(cell.Value) = "x" Then
        Worksheets("Sheet2").Cells(rw, "A") = cell.Offset(0, -1)
        rw = rw + 1
        End If
    Next
End Sub

Before:

After:

enter image description here

ASH
  • 20,759
  • 19
  • 87
  • 200