我正在构建一个包含三个过程工作表(FTP,ATP,CS)和一个失败报告工作表的模板。我尝试做的是对每个过程表应用高级过滤器,仅复制过滤后的结果,并将它们连续粘贴到失败报告的命名范围内。
我让高级过滤器正常工作,但是当我尝试将结果复制到失败报告中时会导致问题。 FTP结果将正确粘贴,但ATP和CS将粘贴到命名范围的底部(并扩展范围)。我需要告诉它粘贴在A列的下一个空单元格中(在命名范围内)。我附上了一份返回的结果副本。
Sub AdvancedFilterCopyAttempt()
' Script to apply an advanced filter to multiple worksheets and copy those results to copy to the Failure Report.
'Declare Variables
Dim rngCopy As Range, rngCopyNotes As Range
Dim NextRow As Long
Dim wsFTP As Worksheet, wsATP As Worksheet, wsFail As Worksheet, wsCS As Worksheet
Set wsFTP = Sheets("Results")
Set wsATP = Sheets("ATP Results")
Set wsFail = Sheets("Failure Report")
Set wsCS = Sheets("CS Results")
Sheets("Results").Activate
Range("Results").AdvancedFilter Action:=xlFilterInPlace, _
CriteriaRange:=Range("Criteria"), Unique:=False
Sheets("ATP Results").Activate
Range("A:I").AdvancedFilter Action:=xlFilterInPlace, _
CriteriaRange:=Range("APTCriteria"), Unique:=True
Sheets("CS Results").Activate
Range("A:I").AdvancedFilter Action:=xlFilterInPlace, _
CriteriaRange:=Range("CSCriteria"), Unique:=True
wsFTP.Activate
'copy FTP results to Failure Report
Set rngCopy = wsFTP.Range("Results_Part1").SpecialCells(xlCellTypeVisible)
Set rngCopyNotes = wsFTP.Range("Results_Part2").SpecialCells(xlCellTypeVisible)
Sheets("Failure Report").Range("A:A").ClearContents
NextRow = wsFail.Range("Fail_Report_Table").Cells(1, 1).Row
rngCopy.Copy wsFail.Range("A" & NextRow)
rngCopyNotes.Copy wsFail.Range("H" & NextRow)
wsATP.Activate
'copy ATP results to Failure Report
Set rngCopy = wsATP.Range("APTResults1").SpecialCells(xlCellTypeVisible)
Set rngCopyNotes = wsATP.Range("APTResults2").SpecialCells(xlCellTypeVisible)
NextRow = wsFail.Range("Fail_Report_Table").Cells(1, 1).End(xlDown).Offset(1).Row
rngCopy.Copy wsFail.Range("A" & NextRow)
rngCopyNotes.Copy wsFail.Range("H" & NextRow)
wsCS.Activate
Set rngCopy = wsCS.Range("CSResults1").SpecialCells(xlCellTypeVisible)
Set rngCopyNotes = wsCS.Range("CSResults2").SpecialCells(xlCellTypeVisible)
NextRow = wsFail.Range("Fail_Report_Table").Cells(1, 1).End(xlDown).Offset(1).Row
rngCopy.Copy wsFail.Range("A" & NextRow)
rngCopyNotes.Copy wsFail.Range("H" & NextRow)
Sheets("Failure Report").Activate
End Sub
答案 0 :(得分:0)
好的,现在我知道Fail_Report_Table
从A22开始,更改行
NextRow = wsFail.Range("Fail_Report_Table").Cells(1, 1).End(xlDown).Offset(1).Row
到
NextRow = wsFail.Range("Fail_Report_Table").Cells(1, 1).Offset(-1).End(xlDown).Offset(1).Row
两次都会发生。
注意:我知道你一直在研究这个问题,所以它真的会让你研究为什么会发生这些变化,而不是仅仅将它们复制到你的代码中并继续前进,所以你真正理解它在做什么,所以你可以在将来复制它,或者根据需要进行操作。