0

我想根据一列中的以下条件将行从一张纸复制到另一张纸:

  1. 如果一行在列中包含值“AB”,则将该行复制到工作表中,但如果下一行在同一列中具有相同的值,则忽略该行并继续
  2. 如果一行在列中包含值“NULL”,则将该行复制到工作表中 - 这里没有限制

我写了一些逻辑,但它不起作用 - 这是相关的片段:

If wks.Range("N" & CStr(LSearchRow)).Value = "NULL" Or Not _
wks.Range("N" & CStr(LSearchRow)).Value = optionCode Then

    'Select row in master to copy
    Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select

    Selection.Copy

    'Paste row into new worksheet in next row
    wksCopyTo.Select

    wksCopyTo.Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select

    wksCopyTo.Paste

    optionCode = wks.Range("N" & CStr(LSearchRow)).Value

    'Move counter to next row
    LCopyToRow = LCopyToRow + 1

    'Remove blank rows
    wksCopyTo.Columns(1).SpecialCells(xlCellTypeBlanks).EntireRow.Delete

    'Go back to Master Sheet to continue searching
    wks.Select
End If

任何人都可以帮忙吗?

4

1 回答 1

0

Here's a small Sub that you can pass your Range reference and Row on copy worksheet into that should do the trick. If you can't figure out how to incorporate it, blow out your code a bit and I will show you how to squeeze it in.

Sub copyrow(rng As Range, LCopyToRow As Long)

If rng = vbNullString Then rng.EntireRow.Copy wksCopyTo.Cells(LCopyToRow, 1)

If rng.Offset(1) <> rng Then rng.EntireRow.Copy wksCopyTo.Cells(LCopyToRow, 1)

End Sub

Also, in above code, no need to use select when writing VBA. It's slower and less efficient. Line 5 to 14 (counting blank lines and comments) above can be written simply like this

Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Copy wksCopyTo.Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow))
于 2012-05-16T19:23:32.477 回答