excel - 需要 VBA 协助根据表 1 中的值将数据复制到表 2 中的一行,并从表 1 中删除原始数据

标签 excel vba

我在 VBA 代码方面相对缺乏经验,但是有一个我试图根据行中列出的项目的“状态”来组织的工作簿。

我在“M”列中有一个下拉框,允许用户选择“HOLD”或“RELEASED”...根据此值,需要将行项目复制并粘贴到工作表 2 中的同一行中,如果value = "RELEASED"(行项目范围示例为 ("B7:N7")(工作表的格式完全相同)。我有这方面的代码,但我需要从工作表中删除 "RELEASED"行项目1 复制并粘贴到工作表 2 后。

这是我所拥有的...(零件暂存日志 = 表 1 ...已发布的部件日志 = 表 2)

Public Sub CopyPasteRows()

Sheets("Parts Staging Log").Select
' Find the last row of data
FinalRow = Range("B828").End(xlUp).Row
' Loop through each row
For x = 7 To FinalRow
' Decide to copy based on column M value "RELEASED"
ThisValue = Range("M" & x).Value
If ThisValue = "RELEASED" Then
Range("B" & x & ":BM" & x).Copy
Sheets("Released Parts Log").Select
NextRow = Range("B828").End(xlUp).Row + 1
Range("B" & NextRow).Select
ActiveSheet.Paste
Sheets("Parts Staging Log").Select
End If
Next x

End Sub

如果有人可以帮助我删除原始订单项,那就太好了。其他意见也接受!

谢谢!

最佳答案

我对此的看法:删除循环(太慢)并用自动过滤器替换

Public Sub CopyPasteRows()
Dim FinalRow As Long

With Sheets("Parts Staging Log")
' Find the last row of data
    FinalRow = .Range("B828").End(xlUp).Row
    .Range("B6:BM" & FinalRow).AutoFilter
    .Range("B6:BM" & FinalRow).AutoFilter field:=12, Criteria1:="RELEASED"
    .Range("B7:BM" & FinalRow).SpecialCells(xlCellTypeVisible).Copy Sheets("Released Parts Log").Range("B828").End(xlUp).Offset(1)
    .Range("B7:BM" & FinalRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    .Range("B6:BM" & FinalRow).AutoFilter    
End With


End Sub

关于excel - 需要 VBA 协助根据表 1 中的值将数据复制到表 2 中的一行,并从表 1 中删除原始数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13240536/

相关文章:

excel - 如何将 1 添加到 Excel 中最近的重复行?

excel - 从文本中解析子字符串

excel - 如何使用特定的行数将excel数据从垂直拆分为水平

使用 Power 查询将 Json 转为 Excel

excel - 使用 vba 脚本将 .txt 中的数据挖掘到 excel 电子表格

sql - 两列去重

vb.net - Excel 公式中基于数组的数学运算

excel - 使用 VBA 过滤工作表数据

excel - 根据列表框选择从表中删除行

excel - 应用公式,从选定单元格偏移三列