【问题标题】:Copying data from one page to another depending on cell value根据单元格值将数据从一页复制到另一页
【发布时间】:2020-10-21 13:02:49
【问题描述】:

当单词“ordered”在某一列中时,此代码将整行复制到另一行。

但是,我需要调整此代码,以便不为另一个函数复制整行,而只需将 A:J 列复制到下一张表中,但我很难做到这一点。

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

【问题讨论】:

  • 最好使用Range.AutoFilterRange.AdvancedFilter,而不是循环。
  • 另请注意,您需要删除On Error Resume Next。此行隐藏 所有 错误消息,直到 End Sub 但错误仍然存​​在,您只是看不到它们的消息。因此,如果发生错误并且您没有收到通知,您将无法修复这些错误,如果您不修复它们,您的代码当然无法正常工作。像你一样使用这条线是危险的。删除它并修复您的错误。另请参阅VBA Error Handling – A Complete Guide
  • 我认为问题可能在xRg(Z).EntireRow.Copy这一行。您正在应对整行。因此,您将粘贴整行。目的地是否仅限于 A-J 列并不重要。尝试只应对您需要的范围。

标签: excel vba copying


【解决方案1】:

可能有更优雅的方式来做到这一点,但你可以替换

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

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

【讨论】:

    猜你喜欢
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多