+ Reply to Thread
Results 1 to 8 of 8

Deleting range of rows in subroutine loop

  1. #1
    Registered User
    Join Date
    03-25-2008
    Location
    Evansville, IN
    Posts
    38

    Deleting range of rows in subroutine loop

    I have developed a subroutine to delete a single row if a particular cell content is zero and it works fine. I now need to adapt that subroutine so that if a particular cell is zero the current row + the next row is deleted and then it moves on to the next row to test again. Can someone tell me how to change the below subroutine to accomplish that? Thanks.

    Denny Riffert
    Evansville, IN


    Range("D1").Select
    Set CurrentCell = Range("D1")

    For Count = 1 To 500
    Set nextCell = CurrentCell.Offset(2, 0)
    If CurrentCell.Value = 0 Then
    CurrentCell.EntireRow.Delete
    End If
    Set CurrentCell = nextCell
    Next Count
    Last edited by driffert; 06-09-2010 at 01:57 PM.

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Deleting range of rows in subroutine loop

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    03-25-2008
    Location
    Evansville, IN
    Posts
    38

    Re: Deleting range of rows in subroutine loop

    Thanks Bob but I'm new to VBA and need some clarification. What exactly occurs when your code is used? Thanks

  4. #4
    Registered User
    Join Date
    03-25-2008
    Location
    Evansville, IN
    Posts
    38

    Re: Deleting range of rows in subroutine loop

    Bob,

    I'm confused by the "i" in your code. What exactly does it signify? Thanks

  5. #5
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Deleting range of rows in subroutine loop

    It deletes two rows when the condition is met, as requested.

  6. #6
    Registered User
    Join Date
    03-25-2008
    Location
    Evansville, IN
    Posts
    38

    Re: Deleting range of rows in subroutine loop

    Bob,

    When I use your code, I receive a compile error "invalid next control variable reference" and it highlights "Next i". What am I doing wrong? Thanks.

  7. #7
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Deleting range of rows in subroutine loop

    It has to be in a procedure, a Sub or a Function. Wasn't your original code in such a procedure?

  8. #8
    Registered User
    Join Date
    03-25-2008
    Location
    Evansville, IN
    Posts
    38

    Re: Deleting range of rows in subroutine loop

    Bob,

    I figured it out. All my code is listed below. I had to change "For Count = 500 To 1 Step -1" in your code to "For i = 500 To 1 Step -1". Thanks for all your help on this.

    Code

    Sub CreateVoucher()
    '
    ' CreateVoucher Macro
    ' Create A/P voucher
    '
    ' Keyboard Shortcut: Ctrl+e
    '
    Name = ThisWorkbook.Worksheets("Variables").Range("filename")

    Application.Goto Reference:="voucher"
    Selection.Copy
    Workbooks.Add
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Range("D1").Select

    For i = 500 To 1 Step -1
    If Cells(i, "D").Value = 0 Then
    Rows(i).Resize(2).Delete
    End If
    Next i


    ActiveWorkbook.SaveAs Filename:=Name, FileFormat:=xlExcel8, CreateBackup:=False
    ActiveWorkbook.Close savechanges:=False
    ThisWorkbook.Activate
    Sheets("Vectren").Select
    Range("A1").Select
    End Sub

+ 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