[英]Copying entire row from one excel sheet to next empty row of another sheet
I have two sheets data
and PrevErrCheck
. 我有两个工作表
data
和PrevErrCheck
。 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.