[英]VBA Copy and paste entire row if cell matches list of IDs, but do not paste if list contains blank cell or cell with ""
I have what I thought would be a simple script, but I have some some strange results.我有一个我认为会很简单的脚本,但我有一些奇怪的结果。
Goal: Identify specific IDs in a SOURCE sheet using a list of IDs on a Translator Sheet.目标:使用翻译表上的 ID 列表识别SOURCE表中的特定 ID。 When found, copy the entire row to and OUTPUT sheet.
找到后,将整行复制到OUTPUT 工作表。
The output has strange results that I can't figure out. output 有我无法弄清楚的奇怪结果。
Full code is below attempts:完整代码如下:
Attempts:尝试:
Old Code:旧代码:
For K = 1 To xRg.Count
If CStr(xRg(K).Value) = Worksheets("D62D627EB404207DE053D71C880A3E05") Then
xRg(K).EntireRow.Copy Destination:=Worksheets("Output").Range("A2" & J + 1)
J = J + 1
End If
New code:新代码:
For K = 1 To xRg.Count
If CStr(xRg(K).Value) = Worksheets("Translator").Range("z21:z" & I)** Then
xRg(K).EntireRow.Copy Destination:=Worksheets("Output").Range("A2" & J + 1)
J = J + 1
End If
Results in wrong location: When the script is complete, my first result does not start on Row 2 as expected.结果位置错误:脚本完成后,我的第一个结果没有按预期从第 2 行开始。 I thought the clear contents would fix this, but maybe a different clear function is required?
我认为清晰的内容可以解决这个问题,但也许需要一个不同的清晰的 function? or the clear function is in the wrong place?
或者清除 function 在错误的地方? Below screenshot shows how it should show up.
下面的屏幕截图显示了它应该如何显示。 It is in the same columns but doesn't start until row 21. enter image description here
它在同一列中,但直到第 21 行才开始。在此处输入图像描述
Slow code: I have a command that copies and pastes of the first row from SOURCE to OUTPUT .慢代码:我有一个命令将第一行从SOURCE复制并粘贴到OUTPUT 。 My code is cumbersome.
我的代码很麻烦。 There has to be an easier way.
必须有更简单的方法。 I am doing this copy and paste just in case the source file adds new columns in the future.
我正在执行此复制和粘贴操作,以防源文件将来添加新列。
Worksheets("Output").Cells.ClearContents Sheets("SOURCE").Select Rows("1:1").Select Selection.Copy Sheets("Output").Select Rows("1:1").Select ActiveSheet.Paste
Thank you for all your help.谢谢你的帮助。
Option Explicit
Sub MoveRowBasedOnCellValuefromlist()
'Updated by xxx 2023.01.18
Dim xRg As Range
Dim xCell As Range
Dim I As Long
Dim J As Long
Dim K As Long
I = Worksheets("SOURCE").UsedRange.Rows.Count
J = Worksheets("Output").UsedRange.Rows.Count
If J = 1 Then
If Application.WorksheetFunction.CountA(Worksheets("Output").UsedRange) = 0 Then J = 0
End If
Worksheets("Output").Cells.ClearContents
Sheets("SOURCE").Select
Rows("1:1").Select
Selection.Copy
Sheets("Output").Select
Rows("1:1").Select
ActiveSheet.Paste
Set xRg = Worksheets("SOURCE").Range("B2:B" & I)
On Error Resume Next
Application.ScreenUpdating = False
'NOTE - There are duplicates in the Translator list. I only want it to paste the first instance.
'Otherwise, I need to create an =Unique() formula and that seems like unnecessary work.
For K = 1 To xRg.Count
If CStr(xRg(K).Value) = Worksheets("Translator").Range("z21:z" & I) Then
xRg(K).EntireRow.Copy Destination:=Worksheets("Output").Range("A2" & J + 1)
J = J + 1
End If
Next
Application.ScreenUpdating = True
End Sub
Try this out - using Match as a fast way to check if a value is contained in your lookup list.试试这个 - 使用 Match 作为检查查找列表中是否包含值的快速方法。
Sub MoveRowBasedOnCellValuefromlist()
Dim c As Range, wsSrc As Worksheet, wsOut As Worksheet, wb As Workbook
Dim cDest As Range, wsTrans As Worksheet, rngList As Range
Set wb = ThisWorkbook 'for example
Set wsSrc = wb.Worksheets("SOURCE")
Set wsOut = wb.Worksheets("Output")
Set wsTrans = wb.Worksheets("Translator")
Set rngList = wsTrans.Range("Z21:Z" & wsTrans.Cells(Rows.Count, "Z").End(xlUp).Row)
ClearSheet wsOut
wsSrc.Rows(1).Copy wsOut.Rows(1)
Set cDest = wsOut.Range("A2") 'first paste destination
Application.ScreenUpdating = False
For Each c In wsSrc.Range("B2:B" & wsSrc.Cells(Rows.Count, "B").End(xlUp).Row).Cells
If Not IsError(Application.Match(c.Value, rngList, 0)) Then 'any match in lookup list?
c.EntireRow.Copy cDest
Set cDest = cDest.Offset(1) 'next paste row
End If
Next c
Application.ScreenUpdating = True
End Sub
'clear a worksheet
Sub ClearSheet(ws As Worksheet)
With ws.Cells
.ClearContents
.ClearFormats
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.