Copy a row from one sheet to another sheet if the row contains a certain value

半世苍凉 提交于 2019-11-29 23:51:32

问题


This shouldn't be complicated code, but I am new to Excel VBA. I've tried many different methods resulting in bugs, infinite loops, and wrong selections.

I need to go row by row through "Sheet1" selecting one row at a time, check if the value in Column J is correct (value = 131125), if it is then copy - paste the row to "Sheet2" (into the same row as it was in Sheet1).

Help is much appreciated! :)


回答1:


Sub Test()
For Each Cell In Sheets(1).Range("J:J")
    If Cell.Value = "131125" Then
        matchRow = Cell.Row
        Rows(matchRow & ":" & matchRow).Select
        Selection.Copy

        Sheets("Sheet2").Select
        ActiveSheet.Rows(matchRow).Select
        ActiveSheet.Paste
        Sheets("Sheet1").Select
    End If
Next
End Sub


来源:https://stackoverflow.com/questions/20269725/copy-a-row-from-one-sheet-to-another-sheet-if-the-row-contains-a-certain-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!