+ Reply to Thread
Results 1 to 4 of 4

Copy Rows meeting a condition but not duplicate

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-04-2008
    Posts
    103

    Copy Rows meeting a condition but not duplicate

    I got this code form Ozgrid that works great other than everytime I run the macro it copies everything over again.

    Sub CopyRows()
        Dim rng        As Range
        Dim cl         As Range
        Dim str        As String
         
        Set rng = ActiveSheet.UsedRange 'the range to search ie the used range
        str = "Yes" 'string to look for
        For Each cl In rng 'checkeach cell
             
            If cl.Text = str Then
                 'if the ell contains the correct value copy it to next empty row on sheet 2 &  delete the row
                cl.EntireRow.Copy Destination:=Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
                End If
        Next cl
    End Sub
    What do I put in my code so it does not duplicate? Other words I run the macro once and it finds all the "Yes". Then I input some more "Yes" and run it again. Now it has copied everything even the same ones from the first copy. Please advise. Thank you.
    Last edited by taichi56; 01-18-2009 at 05:57 PM.

  2. #2
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Maybe add a line to delete sheet2 first then add a blank sheet named Sheet2

    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  3. #3
    Forum Contributor
    Join Date
    05-04-2008
    Posts
    103

    Copy Rows meeting a condition but not duplicate

    I tried some different deleting codes but did not seem to work. It would just hang up and copy 4 rows of data up to 2500 rows.

  4. #4
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Maybe

    Sub CopyRows()
        Dim rng        As Range
        Dim cl         As Range
        Dim str        As String
         
        Set rng = Sheets("Sheet1").UsedRange 'the range to search ie the used range
        str = "Yes" 'string to look for
            
            Application.DisplayAlerts = False
                Sheets("Sheet2").Delete
            Application.DisplayAlerts = True
            Sheets.Add.Name = "Sheet2"
        
        For Each cl In rng 'checkeach cell
            
            If cl.Text = str Then
                 'if the ell contains the correct value copy it to next empty row on sheet 2 &  delete the row
                cl.EntireRow.Copy Destination:=Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
                End If
        Next cl
    End Sub
    VBA Noob

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1