简体   繁体   English

将整个行从一个Excel工作表复制到另一工作表的下一个空行

[英]Copying entire row from one excel sheet to next empty row of another sheet

I have two sheets data and PrevErrCheck . 我有两个工作表dataPrevErrCheck I am checking all occurrence of variable VarVal (this variable has data in E1 cell of PrevErrCheck ) in sheet data and copy entire row to sheet PrevErrCheck . 我正在检查工作表数据中变量VarVal所有出现(此变量在PrevErrCheck E1单元中具有数据),并将整行复制到工作表PrevErrCheck But the problem I am facing here is running macro multiple times overwriting data. 但是我在这里面临的问题是运行宏多次覆盖数据。 I would like to keep the copied rows in sheet data and whenever I run next time, it should copy to next blank row. 我想将复制的行保留在工作表数据中,每当下次运行时,它都应复制到下一个空白行。

I am using below code currently but bit confused to how to integrate the the option to find last row on PrevErrCheck and copy lines below that 我目前正在使用下面的代码,但对如何集成选项以在PrevErrCheck上查找最后一行并复制该行下面的内容感到有些困惑

Sub PrevErrCheck()    
    Dim spem As Workbook
    Dim PrevErrCheck As Worksheet
    Dim data As Worksheet
    Dim xRg As Range
    Dim xCell As Range
    Dim I As Long
    Dim J As Long
    Dim K As Long

    Set spem = Excel.Workbooks("SwitchPortErrorMonitor.xlsm")
    Set PrevErrCheck = spem.Worksheets("PrevErrCheck")
    Set data = spem.Worksheets("data")

    spem.Worksheets("PrevErrCheck").Activate 
    VarVal = PrevErrCheck.Cells(1, "E").Value
    I = data.UsedRange.Rows.count
    J = PrevErrCheck.UsedRange.Rows.count

    If J = 1 Then
        If Application.WorksheetFunction.CountA(PrevErrCheck.UsedRange) = 0 Then J = 0
    End If

    Set xRg = data.Range("X:X")
    On Error Resume Next

    Application.ScreenUpdating = False

    J = 3
    For K = 1 To xRg.count      
        If CStr(xRg(K).Value) = VarVal And Not IsEmpty(VarVal) Then
            xRg(K).EntireRow.Copy Destination:=PrevErrCheck.Range("A" & J + 1)
            PrevErrCheck.Range("X" & J + 1).ClearContents
            J = J + 1
        End If    
    Next

    Application.ScreenUpdating = True
End Sub

You have J = 3 before the loop, that may be a problem. 循环之前您有J = 3 ,这可能是一个问题。 xRg.count always returns 1048576, you should use something more specific. xRg.count始终返回1048576,您应该使用更具体的内容。 Try this: 尝试这个:

Set spem = Excel.Workbooks("SwitchPortErrorMonitor.xlsm")
Set PrevErrCheck = spem.Worksheets("PrevErrCheck")
VarVal = PrevErrCheck.Cells(1, "E").Value
If IsEmpty(VarVal) Then Exit Sub

Set data = spem.Worksheets("data")

spem.Worksheets("PrevErrCheck").Activate
I = data.UsedRange.Rows.Count
J = PrevErrCheck.UsedRange.Rows.Count + 1
If J = 2 Then
    If IsEmpty(PrevErrCheck.Cells(1, 1)) Then J = 1
End If

'    If J = 1 Then
'        If Application.WorksheetFunction.CountA(PrevErrCheck.UsedRange) = 0 Then J = 0
'    End If

'    Set xRg = data.Range("X:X")
'    On Error Resume Next

'    Application.ScreenUpdating = False

'    J = 3
For K = 1 To I
    If CStr(data.Cells(K, "X").Value) = VarVal Then
        data.Cells(K, 1).EntireRow.Copy Destination:=PrevErrCheck.Range("A" & J)
        PrevErrCheck.Range("X" & J).ClearContents
        J = J + 1
    End If
Next

'   Application.ScreenUpdating = True
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将excel行从一个工作表复制到另一个工作表 - Copying excel row from one sheet to another 复制整行并将其粘贴到新工作表中的下一个空行 - Copying a entire row and pasting it into the next empty row in a new sheet Excel:数据从一张纸传输到另一张纸上的下一个空行错误 - Excel: transfer data from one sheet to next empty row on another sheet error 使用VBA excel从一张工作表中复制表格并将其粘贴到另一张工作表的下一个空行中 - copy a table from one Sheet and paste it in the next empty row of another Sheet using VBA excel 比较从一个Excel工作表到另一行的整行 - Comparing an entire row from one excel sheet to another 将数据从一张纸复制到下一个空行的另一张纸上 - copy data from one sheet to another on the next empty row Excel VBA-将范围从一张纸复制到另一张纸,下一个空行 - Excel VBA - Copy range from one sheet to another, next empty row Excel将数据从一张纸移动到另一张纸并将其放置在下一个空行中 - Excel moving data from one sheet to another and placing in next empty row 复制列直到一张纸的最后一行,然后粘贴到另一张纸的下一个空行 - Copy columns until last row from one sheet and paste to the next empty row of another sheet 库存宏-将信息从一页纸复制到另一页 - Inventory Macro - copying information from one row of sheet to another sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM