Deleting Rows and moving all rows together












0















I am needing to work out best way and quickest way to be able to achieve Deleting rows with an Y in column A and then moving all remaining rows up so no blank lines are left, as illustrated below.



Col A    Col B    Col C
Y TOM 12
O JOHN 11
Y FRED 12
TOM 12
O JOHN 12
TOM 12
Y JOHN 12
Y TOM 12
Y JOHN 12
FRED 10
JOHN 12


Above is what the spread sheet looks like at first. I wish to be able to press a button or two and have it look like this:



Col A    Col B    Col C
O JOHN 11
TOM 12
O JOHN 12
TOM 12
FRED 10
JOHN 12


This is how I need it to look at end.



Hoping someone can help me out.



Kindest Regards
Natasha Willson










share|improve this question




















  • 1





    Have you thought of using filters? Or do you really need to delete those rows?

    – cybernetic.nomad
    Feb 20 at 22:46











  • Define a vba macro, then with Alt-F8, view the Macros dialogue and double click the deletion macro. Done. I will provide an answer with this macro altered for deletion as it was originally written to hide and unhide rows.

    – Ted D.
    Feb 21 at 0:04













  • #Natasha, you have asked similar question just few days back,, check this link superuser.com/questions/1406892/delete-only-certain-rows/… and there I've responded it with MACRO as solution. Therefore this one should be considered as DUPLICATE. So my suggestion is Delete this one to avoid Vote to Close .

    – Rajesh S
    Feb 21 at 5:07






  • 1





    Possible duplicate of Delete only certain Rows

    – Rajesh S
    Feb 21 at 5:08
















0















I am needing to work out best way and quickest way to be able to achieve Deleting rows with an Y in column A and then moving all remaining rows up so no blank lines are left, as illustrated below.



Col A    Col B    Col C
Y TOM 12
O JOHN 11
Y FRED 12
TOM 12
O JOHN 12
TOM 12
Y JOHN 12
Y TOM 12
Y JOHN 12
FRED 10
JOHN 12


Above is what the spread sheet looks like at first. I wish to be able to press a button or two and have it look like this:



Col A    Col B    Col C
O JOHN 11
TOM 12
O JOHN 12
TOM 12
FRED 10
JOHN 12


This is how I need it to look at end.



Hoping someone can help me out.



Kindest Regards
Natasha Willson










share|improve this question




















  • 1





    Have you thought of using filters? Or do you really need to delete those rows?

    – cybernetic.nomad
    Feb 20 at 22:46











  • Define a vba macro, then with Alt-F8, view the Macros dialogue and double click the deletion macro. Done. I will provide an answer with this macro altered for deletion as it was originally written to hide and unhide rows.

    – Ted D.
    Feb 21 at 0:04













  • #Natasha, you have asked similar question just few days back,, check this link superuser.com/questions/1406892/delete-only-certain-rows/… and there I've responded it with MACRO as solution. Therefore this one should be considered as DUPLICATE. So my suggestion is Delete this one to avoid Vote to Close .

    – Rajesh S
    Feb 21 at 5:07






  • 1





    Possible duplicate of Delete only certain Rows

    – Rajesh S
    Feb 21 at 5:08














0












0








0








I am needing to work out best way and quickest way to be able to achieve Deleting rows with an Y in column A and then moving all remaining rows up so no blank lines are left, as illustrated below.



Col A    Col B    Col C
Y TOM 12
O JOHN 11
Y FRED 12
TOM 12
O JOHN 12
TOM 12
Y JOHN 12
Y TOM 12
Y JOHN 12
FRED 10
JOHN 12


Above is what the spread sheet looks like at first. I wish to be able to press a button or two and have it look like this:



Col A    Col B    Col C
O JOHN 11
TOM 12
O JOHN 12
TOM 12
FRED 10
JOHN 12


This is how I need it to look at end.



Hoping someone can help me out.



Kindest Regards
Natasha Willson










share|improve this question
















I am needing to work out best way and quickest way to be able to achieve Deleting rows with an Y in column A and then moving all remaining rows up so no blank lines are left, as illustrated below.



Col A    Col B    Col C
Y TOM 12
O JOHN 11
Y FRED 12
TOM 12
O JOHN 12
TOM 12
Y JOHN 12
Y TOM 12
Y JOHN 12
FRED 10
JOHN 12


Above is what the spread sheet looks like at first. I wish to be able to press a button or two and have it look like this:



Col A    Col B    Col C
O JOHN 11
TOM 12
O JOHN 12
TOM 12
FRED 10
JOHN 12


This is how I need it to look at end.



Hoping someone can help me out.



Kindest Regards
Natasha Willson







microsoft-excel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 20 at 23:06









cybernetic.nomad

2,481517




2,481517










asked Feb 20 at 22:43









NatashaNatasha

1




1








  • 1





    Have you thought of using filters? Or do you really need to delete those rows?

    – cybernetic.nomad
    Feb 20 at 22:46











  • Define a vba macro, then with Alt-F8, view the Macros dialogue and double click the deletion macro. Done. I will provide an answer with this macro altered for deletion as it was originally written to hide and unhide rows.

    – Ted D.
    Feb 21 at 0:04













  • #Natasha, you have asked similar question just few days back,, check this link superuser.com/questions/1406892/delete-only-certain-rows/… and there I've responded it with MACRO as solution. Therefore this one should be considered as DUPLICATE. So my suggestion is Delete this one to avoid Vote to Close .

    – Rajesh S
    Feb 21 at 5:07






  • 1





    Possible duplicate of Delete only certain Rows

    – Rajesh S
    Feb 21 at 5:08














  • 1





    Have you thought of using filters? Or do you really need to delete those rows?

    – cybernetic.nomad
    Feb 20 at 22:46











  • Define a vba macro, then with Alt-F8, view the Macros dialogue and double click the deletion macro. Done. I will provide an answer with this macro altered for deletion as it was originally written to hide and unhide rows.

    – Ted D.
    Feb 21 at 0:04













  • #Natasha, you have asked similar question just few days back,, check this link superuser.com/questions/1406892/delete-only-certain-rows/… and there I've responded it with MACRO as solution. Therefore this one should be considered as DUPLICATE. So my suggestion is Delete this one to avoid Vote to Close .

    – Rajesh S
    Feb 21 at 5:07






  • 1





    Possible duplicate of Delete only certain Rows

    – Rajesh S
    Feb 21 at 5:08








1




1





Have you thought of using filters? Or do you really need to delete those rows?

– cybernetic.nomad
Feb 20 at 22:46





Have you thought of using filters? Or do you really need to delete those rows?

– cybernetic.nomad
Feb 20 at 22:46













Define a vba macro, then with Alt-F8, view the Macros dialogue and double click the deletion macro. Done. I will provide an answer with this macro altered for deletion as it was originally written to hide and unhide rows.

– Ted D.
Feb 21 at 0:04







Define a vba macro, then with Alt-F8, view the Macros dialogue and double click the deletion macro. Done. I will provide an answer with this macro altered for deletion as it was originally written to hide and unhide rows.

– Ted D.
Feb 21 at 0:04















#Natasha, you have asked similar question just few days back,, check this link superuser.com/questions/1406892/delete-only-certain-rows/… and there I've responded it with MACRO as solution. Therefore this one should be considered as DUPLICATE. So my suggestion is Delete this one to avoid Vote to Close .

– Rajesh S
Feb 21 at 5:07





#Natasha, you have asked similar question just few days back,, check this link superuser.com/questions/1406892/delete-only-certain-rows/… and there I've responded it with MACRO as solution. Therefore this one should be considered as DUPLICATE. So my suggestion is Delete this one to avoid Vote to Close .

– Rajesh S
Feb 21 at 5:07




1




1





Possible duplicate of Delete only certain Rows

– Rajesh S
Feb 21 at 5:08





Possible duplicate of Delete only certain Rows

– Rajesh S
Feb 21 at 5:08










1 Answer
1






active

oldest

votes


















0














Copy the following into a regular vba Module (Use Alt-F11 to open the Visual Basic editor). After adding the code to an existing or new regular module, edit the Const properties as needed. The sheet name is the name appearing on the worksheet tab. To run the code, use Alt-F8, to open the Macro dialogue and double click (run) Delete_Rows.



WARNING Cannot undo deletions made by this macro and therefore the undo buffer is cleared. In other words; this action will not appear in Edit - Undo and Ctrl-z will have no effect.



Option Explicit
Public Sub Delete_Rows()

' WARNING: Cannot undo deletions made by this macro and therefore
' the Undo Buffer Is Cleared. In other words; this action will
' not appear in "Edit - Undo" and Ctrl-z will have no effect.

' Delete Rows if row value in value_Column is delete_On_Value.
' value_Column must include start_on row number (e.g. A1 or C3)

' For speed, only process rows being used <= max_Row.
Const valueColumn = "A2" ' Beginning Cell (row and column) to consider.
Const maxRow = "" ' Last row number. If "", rest of rows in use.
Const deleteOnValue = "Y"
Const deletionSheetName = "Sheet12"
Const deleteWarn = True

'-------------------------------------------------------
'All code from here on - no more user modifiable setting
'

Dim dSht As Worksheet
Dim resetLastCell As Range
Dim deleteRange As Range
Dim r As Range
Dim rangeStart() As String
Dim lastRow As String
Dim tmpEnableEvents As Boolean
Dim tmpScreenUpdating As Boolean

On Error Resume Next
Set dSht = Worksheets(deletionSheetName)
On Error GoTo 0
If dSht Is Nothing Then
MsgBox "Worksheet Named: '" & deletionSheetName & "' not found.", _
vbExclamation, "Deletion Macro - Error"
Exit Sub
End If

Set resetLastCell = dSht.UsedRange ' call and discard to reset LastCell
lastRow = dSht.Range(valueColumn).SpecialCells(xlCellTypeLastCell).Row
If maxRow <> "" Then
If Val(lastRow) > Val(maxRow) Then lastRow = maxRow
End If
rangeStart = Split(dSht.Range(valueColumn).Address(True, False), "$")
If Val(rangeStart(1)) > Val(lastRow) Then
If deleteWarn Then
MsgBox "No used rows beginnig at start row '" & rangeStart(1) _
& "'.", vbInformation, "Deletion Macro - Exiting"
End If
Exit Sub
End If
tmpEnableEvents = Application.EnableEvents
Application.EnableEvents = False
tmpScreenUpdating = Application.ScreenUpdating
Application.ScreenUpdating = False

For Each r In dSht.Range(valueColumn & ":" & rangeStart(0) & lastRow)
If IsEmpty(r) Then
ElseIf CStr(r.Value2) = vbNullString Then
ElseIf r = deleteOnValue Then
If deleteRange Is Nothing Then
Set deleteRange = r
Else
Set deleteRange = Union(deleteRange, r)
End If
End If
Next r
If deleteRange Is Nothing Then
If deleteWarn Then
MsgBox "No rows to delete.", vbInformation, _
"Deletion Macro - Exiting"
End If
ElseIf deleteWarn Then
If 1 = MsgBox("Delete " & deleteRange.Count & " row(s) from '" & _
deletionSheetName & "' tab?", vbQuestion + vbOKCancel, _
"Deletion Macro - Confirmation") _
Then
deleteRange.EntireRow.Delete
End If
Else ' no warning - just delete
deleteRange.EntireRow.Delete
End If
Application.ScreenUpdating = tmpScreenUpdating
Application.EnableEvents = tmpEnableEvents
End Sub





share|improve this answer

























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1407957%2fdeleting-rows-and-moving-all-rows-together%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Copy the following into a regular vba Module (Use Alt-F11 to open the Visual Basic editor). After adding the code to an existing or new regular module, edit the Const properties as needed. The sheet name is the name appearing on the worksheet tab. To run the code, use Alt-F8, to open the Macro dialogue and double click (run) Delete_Rows.



    WARNING Cannot undo deletions made by this macro and therefore the undo buffer is cleared. In other words; this action will not appear in Edit - Undo and Ctrl-z will have no effect.



    Option Explicit
    Public Sub Delete_Rows()

    ' WARNING: Cannot undo deletions made by this macro and therefore
    ' the Undo Buffer Is Cleared. In other words; this action will
    ' not appear in "Edit - Undo" and Ctrl-z will have no effect.

    ' Delete Rows if row value in value_Column is delete_On_Value.
    ' value_Column must include start_on row number (e.g. A1 or C3)

    ' For speed, only process rows being used <= max_Row.
    Const valueColumn = "A2" ' Beginning Cell (row and column) to consider.
    Const maxRow = "" ' Last row number. If "", rest of rows in use.
    Const deleteOnValue = "Y"
    Const deletionSheetName = "Sheet12"
    Const deleteWarn = True

    '-------------------------------------------------------
    'All code from here on - no more user modifiable setting
    '

    Dim dSht As Worksheet
    Dim resetLastCell As Range
    Dim deleteRange As Range
    Dim r As Range
    Dim rangeStart() As String
    Dim lastRow As String
    Dim tmpEnableEvents As Boolean
    Dim tmpScreenUpdating As Boolean

    On Error Resume Next
    Set dSht = Worksheets(deletionSheetName)
    On Error GoTo 0
    If dSht Is Nothing Then
    MsgBox "Worksheet Named: '" & deletionSheetName & "' not found.", _
    vbExclamation, "Deletion Macro - Error"
    Exit Sub
    End If

    Set resetLastCell = dSht.UsedRange ' call and discard to reset LastCell
    lastRow = dSht.Range(valueColumn).SpecialCells(xlCellTypeLastCell).Row
    If maxRow <> "" Then
    If Val(lastRow) > Val(maxRow) Then lastRow = maxRow
    End If
    rangeStart = Split(dSht.Range(valueColumn).Address(True, False), "$")
    If Val(rangeStart(1)) > Val(lastRow) Then
    If deleteWarn Then
    MsgBox "No used rows beginnig at start row '" & rangeStart(1) _
    & "'.", vbInformation, "Deletion Macro - Exiting"
    End If
    Exit Sub
    End If
    tmpEnableEvents = Application.EnableEvents
    Application.EnableEvents = False
    tmpScreenUpdating = Application.ScreenUpdating
    Application.ScreenUpdating = False

    For Each r In dSht.Range(valueColumn & ":" & rangeStart(0) & lastRow)
    If IsEmpty(r) Then
    ElseIf CStr(r.Value2) = vbNullString Then
    ElseIf r = deleteOnValue Then
    If deleteRange Is Nothing Then
    Set deleteRange = r
    Else
    Set deleteRange = Union(deleteRange, r)
    End If
    End If
    Next r
    If deleteRange Is Nothing Then
    If deleteWarn Then
    MsgBox "No rows to delete.", vbInformation, _
    "Deletion Macro - Exiting"
    End If
    ElseIf deleteWarn Then
    If 1 = MsgBox("Delete " & deleteRange.Count & " row(s) from '" & _
    deletionSheetName & "' tab?", vbQuestion + vbOKCancel, _
    "Deletion Macro - Confirmation") _
    Then
    deleteRange.EntireRow.Delete
    End If
    Else ' no warning - just delete
    deleteRange.EntireRow.Delete
    End If
    Application.ScreenUpdating = tmpScreenUpdating
    Application.EnableEvents = tmpEnableEvents
    End Sub





    share|improve this answer






























      0














      Copy the following into a regular vba Module (Use Alt-F11 to open the Visual Basic editor). After adding the code to an existing or new regular module, edit the Const properties as needed. The sheet name is the name appearing on the worksheet tab. To run the code, use Alt-F8, to open the Macro dialogue and double click (run) Delete_Rows.



      WARNING Cannot undo deletions made by this macro and therefore the undo buffer is cleared. In other words; this action will not appear in Edit - Undo and Ctrl-z will have no effect.



      Option Explicit
      Public Sub Delete_Rows()

      ' WARNING: Cannot undo deletions made by this macro and therefore
      ' the Undo Buffer Is Cleared. In other words; this action will
      ' not appear in "Edit - Undo" and Ctrl-z will have no effect.

      ' Delete Rows if row value in value_Column is delete_On_Value.
      ' value_Column must include start_on row number (e.g. A1 or C3)

      ' For speed, only process rows being used <= max_Row.
      Const valueColumn = "A2" ' Beginning Cell (row and column) to consider.
      Const maxRow = "" ' Last row number. If "", rest of rows in use.
      Const deleteOnValue = "Y"
      Const deletionSheetName = "Sheet12"
      Const deleteWarn = True

      '-------------------------------------------------------
      'All code from here on - no more user modifiable setting
      '

      Dim dSht As Worksheet
      Dim resetLastCell As Range
      Dim deleteRange As Range
      Dim r As Range
      Dim rangeStart() As String
      Dim lastRow As String
      Dim tmpEnableEvents As Boolean
      Dim tmpScreenUpdating As Boolean

      On Error Resume Next
      Set dSht = Worksheets(deletionSheetName)
      On Error GoTo 0
      If dSht Is Nothing Then
      MsgBox "Worksheet Named: '" & deletionSheetName & "' not found.", _
      vbExclamation, "Deletion Macro - Error"
      Exit Sub
      End If

      Set resetLastCell = dSht.UsedRange ' call and discard to reset LastCell
      lastRow = dSht.Range(valueColumn).SpecialCells(xlCellTypeLastCell).Row
      If maxRow <> "" Then
      If Val(lastRow) > Val(maxRow) Then lastRow = maxRow
      End If
      rangeStart = Split(dSht.Range(valueColumn).Address(True, False), "$")
      If Val(rangeStart(1)) > Val(lastRow) Then
      If deleteWarn Then
      MsgBox "No used rows beginnig at start row '" & rangeStart(1) _
      & "'.", vbInformation, "Deletion Macro - Exiting"
      End If
      Exit Sub
      End If
      tmpEnableEvents = Application.EnableEvents
      Application.EnableEvents = False
      tmpScreenUpdating = Application.ScreenUpdating
      Application.ScreenUpdating = False

      For Each r In dSht.Range(valueColumn & ":" & rangeStart(0) & lastRow)
      If IsEmpty(r) Then
      ElseIf CStr(r.Value2) = vbNullString Then
      ElseIf r = deleteOnValue Then
      If deleteRange Is Nothing Then
      Set deleteRange = r
      Else
      Set deleteRange = Union(deleteRange, r)
      End If
      End If
      Next r
      If deleteRange Is Nothing Then
      If deleteWarn Then
      MsgBox "No rows to delete.", vbInformation, _
      "Deletion Macro - Exiting"
      End If
      ElseIf deleteWarn Then
      If 1 = MsgBox("Delete " & deleteRange.Count & " row(s) from '" & _
      deletionSheetName & "' tab?", vbQuestion + vbOKCancel, _
      "Deletion Macro - Confirmation") _
      Then
      deleteRange.EntireRow.Delete
      End If
      Else ' no warning - just delete
      deleteRange.EntireRow.Delete
      End If
      Application.ScreenUpdating = tmpScreenUpdating
      Application.EnableEvents = tmpEnableEvents
      End Sub





      share|improve this answer




























        0












        0








        0







        Copy the following into a regular vba Module (Use Alt-F11 to open the Visual Basic editor). After adding the code to an existing or new regular module, edit the Const properties as needed. The sheet name is the name appearing on the worksheet tab. To run the code, use Alt-F8, to open the Macro dialogue and double click (run) Delete_Rows.



        WARNING Cannot undo deletions made by this macro and therefore the undo buffer is cleared. In other words; this action will not appear in Edit - Undo and Ctrl-z will have no effect.



        Option Explicit
        Public Sub Delete_Rows()

        ' WARNING: Cannot undo deletions made by this macro and therefore
        ' the Undo Buffer Is Cleared. In other words; this action will
        ' not appear in "Edit - Undo" and Ctrl-z will have no effect.

        ' Delete Rows if row value in value_Column is delete_On_Value.
        ' value_Column must include start_on row number (e.g. A1 or C3)

        ' For speed, only process rows being used <= max_Row.
        Const valueColumn = "A2" ' Beginning Cell (row and column) to consider.
        Const maxRow = "" ' Last row number. If "", rest of rows in use.
        Const deleteOnValue = "Y"
        Const deletionSheetName = "Sheet12"
        Const deleteWarn = True

        '-------------------------------------------------------
        'All code from here on - no more user modifiable setting
        '

        Dim dSht As Worksheet
        Dim resetLastCell As Range
        Dim deleteRange As Range
        Dim r As Range
        Dim rangeStart() As String
        Dim lastRow As String
        Dim tmpEnableEvents As Boolean
        Dim tmpScreenUpdating As Boolean

        On Error Resume Next
        Set dSht = Worksheets(deletionSheetName)
        On Error GoTo 0
        If dSht Is Nothing Then
        MsgBox "Worksheet Named: '" & deletionSheetName & "' not found.", _
        vbExclamation, "Deletion Macro - Error"
        Exit Sub
        End If

        Set resetLastCell = dSht.UsedRange ' call and discard to reset LastCell
        lastRow = dSht.Range(valueColumn).SpecialCells(xlCellTypeLastCell).Row
        If maxRow <> "" Then
        If Val(lastRow) > Val(maxRow) Then lastRow = maxRow
        End If
        rangeStart = Split(dSht.Range(valueColumn).Address(True, False), "$")
        If Val(rangeStart(1)) > Val(lastRow) Then
        If deleteWarn Then
        MsgBox "No used rows beginnig at start row '" & rangeStart(1) _
        & "'.", vbInformation, "Deletion Macro - Exiting"
        End If
        Exit Sub
        End If
        tmpEnableEvents = Application.EnableEvents
        Application.EnableEvents = False
        tmpScreenUpdating = Application.ScreenUpdating
        Application.ScreenUpdating = False

        For Each r In dSht.Range(valueColumn & ":" & rangeStart(0) & lastRow)
        If IsEmpty(r) Then
        ElseIf CStr(r.Value2) = vbNullString Then
        ElseIf r = deleteOnValue Then
        If deleteRange Is Nothing Then
        Set deleteRange = r
        Else
        Set deleteRange = Union(deleteRange, r)
        End If
        End If
        Next r
        If deleteRange Is Nothing Then
        If deleteWarn Then
        MsgBox "No rows to delete.", vbInformation, _
        "Deletion Macro - Exiting"
        End If
        ElseIf deleteWarn Then
        If 1 = MsgBox("Delete " & deleteRange.Count & " row(s) from '" & _
        deletionSheetName & "' tab?", vbQuestion + vbOKCancel, _
        "Deletion Macro - Confirmation") _
        Then
        deleteRange.EntireRow.Delete
        End If
        Else ' no warning - just delete
        deleteRange.EntireRow.Delete
        End If
        Application.ScreenUpdating = tmpScreenUpdating
        Application.EnableEvents = tmpEnableEvents
        End Sub





        share|improve this answer















        Copy the following into a regular vba Module (Use Alt-F11 to open the Visual Basic editor). After adding the code to an existing or new regular module, edit the Const properties as needed. The sheet name is the name appearing on the worksheet tab. To run the code, use Alt-F8, to open the Macro dialogue and double click (run) Delete_Rows.



        WARNING Cannot undo deletions made by this macro and therefore the undo buffer is cleared. In other words; this action will not appear in Edit - Undo and Ctrl-z will have no effect.



        Option Explicit
        Public Sub Delete_Rows()

        ' WARNING: Cannot undo deletions made by this macro and therefore
        ' the Undo Buffer Is Cleared. In other words; this action will
        ' not appear in "Edit - Undo" and Ctrl-z will have no effect.

        ' Delete Rows if row value in value_Column is delete_On_Value.
        ' value_Column must include start_on row number (e.g. A1 or C3)

        ' For speed, only process rows being used <= max_Row.
        Const valueColumn = "A2" ' Beginning Cell (row and column) to consider.
        Const maxRow = "" ' Last row number. If "", rest of rows in use.
        Const deleteOnValue = "Y"
        Const deletionSheetName = "Sheet12"
        Const deleteWarn = True

        '-------------------------------------------------------
        'All code from here on - no more user modifiable setting
        '

        Dim dSht As Worksheet
        Dim resetLastCell As Range
        Dim deleteRange As Range
        Dim r As Range
        Dim rangeStart() As String
        Dim lastRow As String
        Dim tmpEnableEvents As Boolean
        Dim tmpScreenUpdating As Boolean

        On Error Resume Next
        Set dSht = Worksheets(deletionSheetName)
        On Error GoTo 0
        If dSht Is Nothing Then
        MsgBox "Worksheet Named: '" & deletionSheetName & "' not found.", _
        vbExclamation, "Deletion Macro - Error"
        Exit Sub
        End If

        Set resetLastCell = dSht.UsedRange ' call and discard to reset LastCell
        lastRow = dSht.Range(valueColumn).SpecialCells(xlCellTypeLastCell).Row
        If maxRow <> "" Then
        If Val(lastRow) > Val(maxRow) Then lastRow = maxRow
        End If
        rangeStart = Split(dSht.Range(valueColumn).Address(True, False), "$")
        If Val(rangeStart(1)) > Val(lastRow) Then
        If deleteWarn Then
        MsgBox "No used rows beginnig at start row '" & rangeStart(1) _
        & "'.", vbInformation, "Deletion Macro - Exiting"
        End If
        Exit Sub
        End If
        tmpEnableEvents = Application.EnableEvents
        Application.EnableEvents = False
        tmpScreenUpdating = Application.ScreenUpdating
        Application.ScreenUpdating = False

        For Each r In dSht.Range(valueColumn & ":" & rangeStart(0) & lastRow)
        If IsEmpty(r) Then
        ElseIf CStr(r.Value2) = vbNullString Then
        ElseIf r = deleteOnValue Then
        If deleteRange Is Nothing Then
        Set deleteRange = r
        Else
        Set deleteRange = Union(deleteRange, r)
        End If
        End If
        Next r
        If deleteRange Is Nothing Then
        If deleteWarn Then
        MsgBox "No rows to delete.", vbInformation, _
        "Deletion Macro - Exiting"
        End If
        ElseIf deleteWarn Then
        If 1 = MsgBox("Delete " & deleteRange.Count & " row(s) from '" & _
        deletionSheetName & "' tab?", vbQuestion + vbOKCancel, _
        "Deletion Macro - Confirmation") _
        Then
        deleteRange.EntireRow.Delete
        End If
        Else ' no warning - just delete
        deleteRange.EntireRow.Delete
        End If
        Application.ScreenUpdating = tmpScreenUpdating
        Application.EnableEvents = tmpEnableEvents
        End Sub






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 21 at 4:49

























        answered Feb 21 at 2:36









        Ted D.Ted D.

        75028




        75028






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1407957%2fdeleting-rows-and-moving-all-rows-together%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How do I know what Microsoft account the skydrive app is syncing to?

            When does type information flow backwards in C++?

            Grease: Live!