Results 1 to 4 of 4

Thread: Macro for deleting every alternate row in Excel 2007

  1. #1
    Join Date
    May 2009
    Posts
    218

    Macro for deleting every alternate row in Excel 2007

    Hi, i need some urgent help from you all. I have a Excel 2007 worksheet containing around 3000 rows. Now due to some reason, i need to delete every alternate row from the same worksheet. While workaround i got that it can be done with a macro but am unable to find out the same. Could you guys please let me know the macros needed or please let me know how can i delete every alternate row in Excel 2007 ?

  2. #2
    Join Date
    Apr 2008
    Posts
    2,139

    Re: Macro for deleting every alternate row in Excel 2007

    Microsoft provides programming examples for illustration only, without warranty either expressed or implied. Here is the needed Macro for deleting every alternate row in Excel 2007:-

    Code:
    Sub Delete_Every_Other_Row()
    
       ' Dimension variables.
       Y = False              ' Change this to True if you want to
                              ' delete rows 1, 3, 5, and so on.
       I = 1
       Set xRng = Selection
    
       ' Loop once for every row in the selection.
       For xCounter = 1 To xRng.Rows.Count
    
           ' If Y is True, then...
           If Y = True Then
    
               ' ...delete an entire row of cells.
               xRng.Cells(I).EntireRow.Delete
    
           ' Otherwise...
           Else
    
               ' ...increment I by one so we can cycle through range.
               I = I + 1
    
           End If
    
           ' If Y is True, make it False; if Y is False, make it True.
           Y = Not Y
    
       Next xCounter
    
    End Sub

  3. #3
    Join Date
    May 2008
    Posts
    4,570

    Re: Macro for deleting every alternate row in Excel 2007

    In order to deleting every alternate row in Excel 2007, set up an Excel workbook as follows:-

    • Create a new Microsoft Excel workbook.
    • In a new worksheet, in the range A1:A9, type the numbers 1 to 5 skipping rows
    • On the Tools menu, click Visual Basic in the Code group on the Developer tab.
    • On the Insert menu, click Module.
    • In the new module, type the following macro:
      Code:
      Sub Delete_Every_Other_Row()
      
         ' Dimension variables.
         Y = False              ' Change this to True if you want to
                                ' delete rows 1, 3, 5, and so on.
         I = 1
         Set xRng = Selection
      
         ' Loop once for every row in the selection.
         For xCounter = 1 To xRng.Rows.Count
      
             ' If Y is True, then...
             If Y = True Then
      
                 ' ...delete an entire row of cells.
                 xRng.Cells(I).EntireRow.Delete
      
             ' Otherwise...
             Else
      
                 ' ...increment I by one so we can cycle through range.
                 I = I + 1
      
             End If
      
             ' If Y is True, make it False; if Y is False, make it True.
             Y = Not Y
      
         Next xCounter
      
      End Sub
    • Switch to the worksheet that contains the data, and then select the range A1:A9.
    • To run the macro, click Macros in the Code group on the Developer tab.
    • Select the Delete_Every_Other_Row macro, and then click Run.

  4. #4
    Join Date
    Feb 2009
    Posts
    266

    Re: Macro for deleting every alternate row in Excel 2007

    Before using the above procedures, i hope that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

Similar Threads

  1. Excel 2003 Macro doesn't work in Excel 2007
    By jjaw in forum Windows Software
    Replies: 3
    Last Post: 03-01-2014, 03:28 PM
  2. Excel 2007 macro help needed
    By AZUL in forum Software Development
    Replies: 4
    Last Post: 19-11-2009, 06:10 PM
  3. Cannot find macro working in excel 2007
    By Visala in forum Windows Software
    Replies: 3
    Last Post: 06-08-2009, 11:47 PM
  4. Set the Macro Security Level in Excel 2007
    By NIcaBoy in forum Windows Software
    Replies: 3
    Last Post: 21-04-2009, 02:04 PM
  5. Macro sum problem in excel 2007
    By Radley in forum Windows Software
    Replies: 2
    Last Post: 07-04-2009, 09:46 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
  •  
Page generated in 1,713,554,827.93579 seconds with 17 queries