Excel VBA – 仅复制不同工作簿中列中包含特定值的行范围

如果列DA中的单元格包含特定值,我想要创build一个复制行范围的macros。

例如:

如果列DA10包含值3-未完成 ,N10 包含任何内容,则CI10不包含任何内容 – 则macros应将单元格F10,H10和DA10复制到工作簿报告WB中 。 我不知道该怎么做

进步到目前为止(感谢Shai – 我仍然愚蠢地做它的工作):

Sub insertINCOMPLETION() Dim dataWB As Workbook Dim reportWB As Workbook Dim workB As Workbook Dim incomplRNG As Range Dim LastRow6 As Long Dim LastRow7 As Long For Each workB In Application.Workbooks If Left(workB.Name, 5) = "15B2" Then Set dataWB = workB Exit For End If Next If Not dataWB Is Nothing Then Set reportWB = ThisWorkbook With reportWB.Sheets("getDATA") LastRow6 = .Cells(.Rows.Count, "B").End(xlUp).Offset(1).Row End With With dataWB.Sheets("Data") LastRow7 = .Cells(.Rows.Count, "F").End(xlUp).Row If InStr(.Range("DA" & LastRow7).Value2, "3-Incompletion") > 0 And _ Trim(.Range("N" & LastRow7).Value2) = "" And _ Trim(.Range("CI" & LastRow7).Value2) = "" Then Set incomplRNG = Application.Union(.Range("F8:F" & LastRow7), .Range("H8:H" & LastRow7), .Range("DA8:DA" & LastRow7)) incomplRNG.Copy reportWB.Sheets("getDATA").Range("B" & LastRow6).PasteSpecial xlPasteValues End If End With End If End Sub 

依然不起作用。 检查列DA后结束。

您需要添加代码部分,您将单元格的文本与“3-incompletion”进行比较。 你可以使用InStr函数来实现这一点。

 With dataWB.Sheets("Data") ' === not sure why you want to look for the last row ??? === 'LastRow7 = .Cells(.Rows.Count, "F").End(xlUp).Row ' compare the value in row 10 (as in your post example) LastRow7 = 10 If InStr(.Range("AD" & LastRow7).Value2, "3-incompletion") > 0 And _ Trim(.Range("AD" & LastRow7).Value2) = "" And _ Trim(.Range("CI" & LastRow7).Value2) = "" Then Set incomplRNG = Application.Union(.Range("F8:F" & LastRow7), .Range("H8:H" & LastRow7), .Range("DA8:DA" & LastRow7)) incomplRNG.Copy reportWB.Sheets("getDATA").Range("B" & LastRow6).PasteSpecial xlPasteValues End If End With