简体   繁体   中英

Copying data from one page to another depending on cell value

This code copies the entire row to another when the word 'ordered' is in a certain column.

However, I need to adapt this code to not copy the entire row for another function but requires only copying columns A:J over into the next sheet but I'm having difficulty achieving this.

Sub MovingOrderedItems()

    Dim xRg As Range
    Dim xCell As Range
    Dim X As Long
    Dim Y As Long
    Dim Z As Long
    X = Worksheets("Engineer-Items to be ordered").UsedRange.Rows.Count
    Y = Worksheets("Admin").UsedRange.Rows.Count
    If Y = 1 Then
       If Application.WorksheetFunction.CountA(Worksheets("Admin").UsedRange) = 0 Then Y = 0
    End If
    Set xRg = Worksheets("Engineer-Items to be ordered").Range("N3:N" & X)
    On Error Resume Next
    Application.ScreenUpdating = False
    For Z = 1 To xRg.Count
        If CStr(xRg(Z).Value) = "ordered" Then
            xRg(Z).EntireRow.Copy Destination:=Worksheets("Admin").Range("A" & Y + 1)
            xRg(Z).EntireRow.Delete
            If CStr(xRg(Z).Value) = "ordered" Then
                 Z = Z - 1
            End If
            Y = Y + 1
        End If
    Next
    Application.ScreenUpdating = True
End Sub

There's probably a more elegant way to do this, but you can replace

xRg(Z).EntireRow.Copy Destination:=Worksheets("Admin").Range("A" & Y + 1)

With

Range(xRg(Z).EntireRow.Cells(1, 1), xRg(Z).EntireRow.Cells(1, 10)).Copy Destination:=Worksheets("Admin").Range("A" & Y + 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM