Results 1 to 7 of 7

Thread: [RESOLVED] Compare rows and copy to new sheet (VBA)

  1. #1

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Resolved [RESOLVED] Compare rows and copy to new sheet (VBA)

    Hi, me again :P

    If Value in Column A > then Column B then copy the row to a new sheet (for example sheet2). Any chance someone knows how to code it in VBA or willing to give me a kickstart?
    The sheet isn't always the same and contains 100's or rows, that's why I want to automate it. Had enough of it :P


    Thanks in advance.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Compare rows and copy to new sheet (VBA)

    Hi, me again :P

    Try this kickoff

    Code:
    Sub FilterRows()
    
      Dim cDestination As Worksheet
      Dim cSource As Worksheet
      Dim intRow As Integer
      
      Set cSource = Worksheets("Sheet1") ' Replace "Sheet1" with actual name.
      Set cDestination = CreateNewSheet
      
      For j = 1 To cSource.UsedRange.Rows.Count
        If Cells(j, 1) <> vbNullString And Cells(j, 2) <> vbNullString Then
          If Val(Cells(j, 1)) > Val(Cells(j, 2)) Then
            Cells(j, 1).EntireRow.Copy
            intRow = intRow + 1
            cDestination.Cells(intRow, 1).PasteSpecial Paste:=xlPasteValues
          End If
        End If
      Next
      
    End Sub
    
    Private Function CreateNewSheet() As Worksheet
        Dim ret As Worksheet
        On Error GoTo CreateSheet
        Set ret = Worksheets("destination") ' Replace "destination" with desired name.
        Set CreateNewSheet = ret
        
        Exit Function
    CreateSheet:
          Set ret = Worksheets.Add(After:=Worksheets(Worksheets.Count))
          ret.Name = "destination"
          Set CreateNewSheet = ret
    
    End Function
    Any rep+ is appriciated



  3. #3

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Compare rows and copy to new sheet (VBA)

    Don't worry about the +REP m8. I'm generous. Will try it tomorrow and let you know


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  4. #4

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Compare rows and copy to new sheet (VBA)

    Creating the sheet gave me an "index out of range" error during the creation of the new sheet in my official workbook. Tried to solve it for a long time. Someone helped me and we got the following code working:
    Code:
        With Sheets("IK_WRONGT")
            .Range("A1").PasteSpecial
            For rw = 2 To Cells.SpecialCells(xlCellTypeLastCell).Row
                If Cells(rw, "AE").Value > Cells(rw, "AF").Value Then Rows(rw).Copy .Range("A" & .Cells(Rows.Count, 1).End(xlUp).Offset(1).Row)
            Next
        End With
    +REP for all the troubles and help m8. Much appreciated.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [RESOLVED] Compare rows and copy to new sheet (VBA)

    Try this:

    vb Code:
    1. Sub CopySpecial()
    2.     Dim SourceSheet As Worksheet, DestinationSheet As Worksheet
    3.     Dim LastRow As Long, R As Long, R1 As Long
    4.  
    5.     '' change this as required
    6.     Set SourceSheet = ActiveWorkbook.Sheets("Sheet1")
    7.     Set DestinationSheet = ActiveWorkbook.Sheets("Sheet2")
    8.    
    9.     LastRow = SourceSheet.Range("A1").SpecialCells(xlLastCell).Row
    10.     For R = 1 To LastRow
    11.         If SourceSheet.Range("A" & R) > SourceSheet.Range("B" & R) Then
    12.             R1 = R1 + 1
    13.             SourceSheet.Range(R & ":" & R).Copy DestinationSheet.Range("A" & R1)
    14.         End If
    15.     Next
    16. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6
    New Member
    Join Date
    May 2013
    Posts
    1

    Re: [RESOLVED] Compare rows and copy to new sheet (VBA)

    Your code is EXACTLY what I needed for a macro I am creating at work. However, how would I change the SourceSheet to the active workbook and active worksheet, which will not always be named sheet1? Also, how do I change the DestinationSheet to a different workbook?

    Thanks for writing the above code.

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [RESOLVED] Compare rows and copy to new sheet (VBA)

    Quote Originally Posted by jmcco002 View Post
    Your code is EXACTLY what I needed for a macro I am creating at work. However, how would I change the SourceSheet to the active workbook and active worksheet, which will not always be named sheet1? Also, how do I change the DestinationSheet to a different workbook?

    Thanks for writing the above code.
    You can use ActiveWorkbook.ActiveSheet in that case.

    Code:
    Set SourceSheet = ActiveWorkbook.ActiveSheet
    Same way you can change the DestinationSheet to some other sheet. You will need to know either the sheet name, or the ordinal position etc.

    Here are various ways to reference any worksheet/worksheet:

    ActiveWorkBook --> The workbook that is active.
    ThisWorkBook --> The workbook in which this macro is running from.
    ActiveSheet --> The active sheet
    Sheets("SheetName") --> The sheet "SheetName". e.g. Sheets("Sheet1"), Sheets("Sheet2")
    Sheets(SheetNumber) --> The sheet at the position specified by SheetNumber. e.g. Sheets(1), Sheets(2)

    If you don't prefix WorkBook before WorkSheet, then the ActiveWorkBook is assumed.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

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



Click Here to Expand Forum to Full Width