Consulting

Results 1 to 9 of 9

Thread: Copying rows in range based on criteria

  1. #1
    VBAX Regular
    Joined
    Mar 2010
    Posts
    20
    Location

    Copying rows in range based on criteria

    I have the following code written but it is not copying the correct cells. I would like to only copy rows in the row range 25:66 with "Buy" in column D and "Initiating" in column E. Can anyone help me modify this code? Thanks in advance.


     
    Private Sub getreport(Code As Integer)
    
        
    
        Range("A25").Select
        celltocopy = ("A25:IU25") & ActiveCell.Row
        Range(celltocopy).Select
        Selection.Copy
         If (ActiveCell.Offset(0, 3).Value = "Buy") And (ActiveCell.Offset(0, 4).Value = "Initiating") Then
            Range("A87").Select
            Selection.PasteSpecial paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    
    
       End If
       
        Do Until ActiveCell.Value = "Stop"
        OffsetRow = 0
        OffsetRow = OffsetRow + 1
            
        Loop

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Try this:
    [VBA]Dim Rng As Range, MyCell As Range
    Set Rng = Range("A25:A66")
    For Each MyCell In Rng
    If MyCell.Offset(0, 3).Value = "Buy" And MyCell.Offset(0, 4).Value = "Initiating" Then
    MyCell.EntireRow.Copy
    Range("A86").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    End If
    Next MyCell
    Application.CutCopyMode = False[/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    VBAX Regular
    Joined
    Mar 2010
    Posts
    20
    Location
    That works but instead of pasting the rows starting in row 87 it starts pasting in row 80. I tried changing the range("A86") to range("A87") but then the macro continuosly copies and pastes into row 87

  4. #4
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Use this instead:
    [VBA]Dim Rng As Range, MyCell As Range, i As Long
    Set Rng = Range("A1:A66")
    i = 87
    For Each MyCell In Rng
    If MyCell.Offset(0, 3).Value = "Buy" And MyCell.Offset(0, 4).Value = "Initiating" Then
    MyCell.EntireRow.Copy
    Range("A" & i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    i = i + 1
    End If
    Next MyCell
    Application.CutCopyMode = False[/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  5. #5
    VBAX Regular
    Joined
    Mar 2010
    Posts
    20
    Location
    Thanks! I adjusted what you just wrote to ("A25:A66') and it works perfectly.

  6. #6
    VBAX Regular
    Joined
    Mar 2010
    Posts
    20
    Location
    One more question. I would like to reference AF3 and AG3 instead of writing in "Buy" and "Initiating" how can this be done?

  7. #7
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Use this:
    [VBA]Dim Rng As Range, MyCell As Range, i As Long
    Set Rng = Range("A25:A66")
    i = 87
    For Each MyCell In Rng
    If MyCell.Offset(0, 3).Value = Range("AF3").Value And MyCell.Offset(0, 4).Value = Range("AF4").Value Then
    MyCell.EntireRow.Copy
    Range("A" & i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    i = i + 1
    End If
    Next MyCell
    Application.CutCopyMode = False[/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  8. #8
    VBAX Regular
    Joined
    Mar 2010
    Posts
    20
    Location
    Thanks!

  9. #9
    VBAX Regular
    Joined
    Mar 2010
    Posts
    20
    Location
    Can you help me finish this macro?

    If you run the macro in my sheet it copies and pastes the data for the first example. I need the macro to loop through every example 1 through 5 (AE3:AE7) based on the criteria

Posting Permissions

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