+ Reply to Thread
Results 1 to 4 of 4

VBA Deleting every other row

  1. #1
    Registered User
    Join Date
    01-09-2015
    Location
    Stockholm, Sverige
    MS-Off Ver
    2010
    Posts
    2

    VBA Deleting every other row

    I want this VBA to copy every row where the cell in column A is the colour red to a new sheet and then delete the row. This VBA is successful but does only delete every second row, is there anybody who can help me figure out whats wrong?





    Option Explicit
    Sub Copy()
    Dim i As Long, j As Long, ws1 As Worksheet, ws2 As Worksheet
    On Error GoTo Err_Execute
    Application.ScreenUpdating = False
    Set ws1 = Worksheets("Farg"): Set ws2 = Worksheets("Rod")
    ws2.UsedRange.Clear ' clear Sheet 2 previous output
    For i = 1 To ws1.Cells(Rows.Count, "A").End(xlUp).Row
    If ws1.Range("A" & i).Interior.Color = RGB(255, 0, 0) Then 'copy every red ones and paste it to a new sheet
    ws1.Range("A" & i).EntireRow.Copy
    ws2.Range("A" & ws2.Cells(Rows.Count, "A").End(xlUp).Row + 1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
    End If
    Next

    For j = 1 To ws1.Cells(Rows.Count, "A").End(xlUp).Row
    If ws1.Range("A" & j).Interior.Color = RGB(255, 0, 0) Then 'Deteling every red ones
    ws1.Range("A" & j).EntireRow.Delete
    End If
    Next


    ws2.Range("A1") = "Results"
    Application.ScreenUpdating = True
    MsgBox "The data has been successfully copied."
    On Error GoTo 0
    Exit Sub
    Err_Execute:
    MsgBox "An error occurred. Error number " & Err.Number & " - " & Err.Description
    End Sub

  2. #2
    Registered User
    Join Date
    06-04-2012
    Location
    Michigan
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: VBA Deleting every other row

    In the j For loop add in a j=j-1 inside the if statement (This is were you delete rows)

    What is happening is that when you find a row with red and delete it all the rows shift up. Then you advance to the next row. But really the next row is where you were originally at. So you need the j=j-1 to make sure you check the row that just moved.

  3. #3
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA Deleting every other row

    Delete going backwards in loop not forwards. You only need 1 loop

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    01-09-2015
    Location
    Stockholm, Sverige
    MS-Off Ver
    2010
    Posts
    2

    Re: VBA Deleting every other row

    Thanks! Now it works!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Deleting part of a column without deleting whole rows.
    By dstrdOne in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-30-2013, 11:42 AM
  2. Deleting Raw Data = Deleting Pivot Tables?
    By taltalk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-10-2008, 07:41 AM
  3. deleting values in a worksheet without deleting the formulas
    By patti in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-28-2005, 05:05 PM
  4. how prevent formula in cell from deleting when deleting value????
    By sh-boom in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 09-30-2005, 02:05 PM

Tags for this Thread

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