宏复制并粘贴到新工作表。运行文件

问题描述 投票:0回答:1

我想将过滤后的数据从“ChangeList”工作表粘贴到“Status”工作表,而不覆盖粘贴的旧数据。但是,当我尝试下面的公式时,它没有粘贴到“B7”,数据被粘贴到不同的行中。 例如 1. B8 从变更列表表到 B7 状态表 2. C8从Changelist Sheet到C7 Status Sheet等等

Sub paste2()
' paste2 Macro
ThisWorkbook.Sheets("CHANGELIST").Range("$A$5:$AJ$106").AutoFilter Field:=5, Criteria1:="Submitted"
    Dim lastRow As Long
' this finds the number of the last row
   lastRow = Cells(Rows.Count, 1).End(xlUp).Row
   
    Sheets("CHANGELIST").Select Range("B8:B1000").Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    Application.CutCopyMode = False
    Selection.Copy
    
    Sheets("STATUS").Select
    Range("B7").Select
    Cells(lastRow, 2).PasteSpecial

将更改列表工作表 B8 中的数据粘贴到工作表状态 B7 中,如果更改列表工作表中有其他过滤数据,则新数据将粘贴到新行中

excel vba macros
1个回答
0
投票

如果您不确定哪个是活动工作表或脚本位置对引用的影响是什么,请始终完全限定范围对象。

Sub paste2()
' paste2 Macro

Set targetsheet = Worksheets("Status")   'added

ThisWorkbook.Sheets("CHANGELIST").Range("$A$5:$AJ$106").AutoFilter Field:=5, Criteria1:="Submitted"
    Dim lastRow As Long
' this finds the number of the last row
   lastRow = targetsheet.Cells(Rows.Count, 1).End(xlUp).Row   'modified
   
    Sheets("CHANGELIST").Select Range("B8:B1000").Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    Application.CutCopyMode = False
    Selection.Copy
    
    Sheets("STATUS").Select
    Range("B7").Select       
    targetsheet.Cells(lastRow + 1, 2).PasteSpecial         'modified
© www.soinside.com 2019 - 2024. All rights reserved.