Consulting

Results 1 to 8 of 8

Thread: Solved: Copy rows selectively

  1. #1

    Solved: Copy rows selectively

    I need some vba that will copy a row in "Sheet1" if it has a valid number in column B.

    Any suggestions?

    Thanks

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Sub Test()
    Dim cel As Range
    With ActiveSheet
    For Each cel In Intersect(.Columns(2), .UsedRange)
    If IsNumeric(cel) Then
    cel.EntireRow.Copy Sheets(2).Cells(Rows.Count, 1).End(xlUp).Offset(1)
    End If
    Next
    End With
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Wow. Thanks.

    I have a couple more things I need it to do:
    It should only copy cells from row 3 or below.

    Also, I need it to look at the numbers in column A for the row it's copying, and if there's an identical number in column A of Sheet2, it needs to overwrite it.

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try
    [vba]
    Option Explicit
    Sub Test()
    Dim cel As Range, Fnd As Range, colA As Single
    With ActiveSheet
    For Each cel In Intersect(.Columns(2), .UsedRange)
    If IsNumeric(cel) and cel.Row > 3 Then
    colA = cel.Offset(, -1)
    Set Fnd = Sheets(2).Columns(1).Find(colA, LookIn:=xlValues, lookat:=xlWhole)
    If Not Fnd Is Nothing Then
    cel.EntireRow.Copy Fnd
    Else
    cel.EntireRow.Copy Sheets(2).Cells(Rows.Count, 1).End(xlUp).Offset(1)
    End If
    End If
    Next
    End With
    End Sub

    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5

    Smile

    Awesome!

    Thanks so much.

  6. #6
    Pardon my ignorance, but why is it "Offset(, -1)" that and not "Offset(0, -1)"?

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You can use default values in code, where the default here is 0

    Similar to
    Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat)
    You can use
    Find(What, , , xlwhole)
    to reduce typing
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •