简体   繁体   中英

Copy entire rows based on multiple cell values

I am trying to copy entire rows where two values are blank and the third cell value is MS-NORT ,
My code is below but I am getting a syntax error .

please help

Set MK = Sheets("data dump").Range("P1:Q5000")
For Each cell In MK

If cell.Value = "" And Sheets("Data Dump").Range("M1:M5000") <> "MS-NORT" 
Then cell.EntireRow.copy
Sheets("working data").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial
End If
Application.CutCopyMode = False
Next 

I'm not exactly sure what you mean by "where two values are blank and the third cell value is MS-NORT"

I've assumed you want to check each row and if column M <> "MS-NORT" and both columns P and Q are blank then copy the row. This code will do that

Sub test()
Dim rngRow As Range, MK As Range, lngrow As Long
Dim varCheckCol1 As Variant, varCheckCol2 As Variant, varCheckCol3 As Variant

Set MK = Sheets("data dump").Range("M1:Q5000")
For Each rngRow In MK.Rows

  varCheckCol1 = rngRow.Value2(1, 1) 'col M
  varCheckCol2 = rngRow.Value2(1, 4) 'col P
  varCheckCol3 = rngRow.Value2(1, 5) 'col Q

  If IsEmpty(varCheckCol2) And IsEmpty(varCheckCol3) And varCheckCol1 <> "MS-NORT" Then
     rngRow.EntireRow.Copy
     Sheets("working data").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial
  End If

Next

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM