Excel VBA查找行,复制内容,粘贴到下一页,然后删除原始数据

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

我正在努力识别工作表1中A列中非空白且第V列中没有Y或L的行。然后我需要复制该行的内容,然后将值粘贴到打开的行上。下一个工作表。最后,我需要清除该行原始工作表上的内容。粘贴的时候我卡住了。错误1004 - 对象'_Worksheet'的方法'范围'失败。我感谢任何帮助。

Option Explicit
Option Compare Text
Sub EndMove()

Dim rowCount As Long, i As Long
Dim ws As Worksheet: Set ws = ActiveSheet
ws.Range("A11").Select
rowCount = ws.Cells(ws.Rows.Count, 1).End(xlUp).row
Application.ScreenUpdating = False: Application.EnableEvents = False
Call ShowAllRecords
For i = 11 To rowCount
    If ws.Range("V" & i) <> "y" And ws.Range("V" & i) <> "l" Then
        If ws.Range("A" & i) <> "" Then

Dim rowCount2 As Long, j As Long
Dim sRng As Range
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets(ActiveSheet.Index + 1)
Dim wAct As Worksheet
Dim lRow As Long
Dim End_Row As Long

Set wAct = ws
Set sRng = ws.Range("V" & i)

If Not IsDate("01 " & wAct.Name & " 2017") Or wAct.Name = "Dec" Then MsgBox "Not applicable for this sheet.": Exit Sub
If ActiveSheet.Index = ThisWorkbook.Worksheets.Count Then MsgBox "This is the last worksheet cannot move forward.": Exit Sub


wAct.unprotect

With ws2
    .unprotect

If rowCount2 = "1" Then

        For j = 11 To Rows.Count
        If .Range("A" & j) = "" Then
            End_Row = j
            Exit For
        End If
    Next j
Else

End If
    wAct.Range("A" & sRng.row & ":AD" & sRng.row + sRng.Rows.Count - 1).Copy
    .Range("A" & End_Row).PasteSpecial xlPasteValuesAndNumberFormats
    wAct.Range("A" & sRng.row & ":AD" & sRng.row + sRng.Rows.Count - 1).ClearContents
    .Range("A1000").Value = End_Row
    .protect DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFiltering:=True
End With

wAct.protect DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFiltering:=True

Application.CutCopyMode = False


        End If
      End If
Next i
Application.EnableEvents = True: Application.ScreenUpdating = True
Call FilterBlanks
MsgBox "Move Complete"
End If
End Sub
excel vba excel-vba
1个回答
1
投票

您的代码中似乎没有可以为rowCount2赋值的行。因此,当您在下面的代码中检查它时,它始终为false,因此跳过此部分

If rowCount2 = "1" Then

        For j = 11 To Rows.Count
        If .Range("A" & j) = "" Then
            End_Row = j
            Exit For
        End If
    Next j
Else

但这部分是必不可少的,因为它是End_Row被赋予价值的唯一部分。那么当你尝试做这个.Range("A" & End_Row)时,End_Row什么也没有。在该行上设置断点并检查End_Row的Locals屏幕以确保它是这个。

© www.soinside.com 2019 - 2024. All rights reserved.