Macro appears to be deleting data but not line and shifting data up.

ghrek

Active Member
Joined
Jul 29, 2005
Messages
427
Hi

I have this macro and when I run it appears to be deleting the data and the gridlines but not the actual rows and thus not shifting all the data up together thats supposed to be there when complete.

Any ideas why?

VBA Code:
Sub MonthEnd()
    Dim rCl As Range, rDelete As Range
    Dim sSht As String

    sSht = InputBox("Enter new sheet name", "New Month Sheet")

    If Len(sSht) = 0 Then
        MsgBox "You must enter a name for the new sheet", vbCritical, "Quitting"
        Exit Sub
        ''/// copy current sheet
    Else: ActiveSheet.Copy After:=Worksheets(Worksheets.Count)
        ActiveSheet.Name = sSht
        For Each rCl In ActiveSheet.Range("AB10:AB9999").Cells
            If rCl.Value = 0 Then
                If rDelete Is Nothing Then
                    Set rDelete = Range("A" & rCl.Row, rCl)
                Else: Set rDelete = Union(rDelete, Range("A" & rCl.Row, rCl))
                End If
            End If
        Next rCl
        If Not rDelete Is Nothing Then rDelete.Delete
        MsgBox "Month End successfully completed", vbInformation, "Done"
    End If
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Here, made some fixing, now should do what you are after:
VBA Code:
Option Explicit

Sub MonthEnd()
    Dim rCl    As Range, rDelete As Range
    Dim sSht   As String

    sSht = InputBox("Enter new sheet name", "New Month Sheet")
    
    If Len(sSht) = 0 Then
        MsgBox "You must enter a name for the new sheet", vbCritical, "Quitting"
        Exit Sub
    Else
        '/// copy current sheet
        ActiveSheet.Copy After:=Worksheets(Worksheets.Count)
        ActiveSheet.Name = sSht
        For Each rCl In ActiveSheet.Range("AB10:AB9999").Cells '<- this is the range "ABxx:AByy"
            If rCl.Value = 0 Then
                If rDelete Is Nothing Then
                    Set rDelete = Range("AB" & rCl.Row) '<- changed, was "A"
                Else
                    Set rDelete = Union(rDelete, Range("AB" & rCl.Row)) '<- changed, was "A"
                End If
            End If
        Next rCl
        If Not rDelete Is Nothing Then rDelete.Delete Shift:=xlUp '<- changed, added shift up
        'If Not rDelete Is Nothing Then rDelete.EntireRow.Delete Shift:=xlUp '<- use instead of above if entire row has to be deleted
        MsgBox "Month End successfully completed", vbInformation, "Done"
    End If
    
End Sub
 
Upvote 0
Solution
Here, made some fixing, now should do what you are after:
VBA Code:
Option Explicit

Sub MonthEnd()
    Dim rCl    As Range, rDelete As Range
    Dim sSht   As String

    sSht = InputBox("Enter new sheet name", "New Month Sheet")
   
    If Len(sSht) = 0 Then
        MsgBox "You must enter a name for the new sheet", vbCritical, "Quitting"
        Exit Sub
    Else
        '/// copy current sheet
        ActiveSheet.Copy After:=Worksheets(Worksheets.Count)
        ActiveSheet.Name = sSht
        For Each rCl In ActiveSheet.Range("AB10:AB9999").Cells '<- this is the range "ABxx:AByy"
            If rCl.Value = 0 Then
                If rDelete Is Nothing Then
                    Set rDelete = Range("AB" & rCl.Row) '<- changed, was "A"
                Else
                    Set rDelete = Union(rDelete, Range("AB" & rCl.Row)) '<- changed, was "A"
                End If
            End If
        Next rCl
        If Not rDelete Is Nothing Then rDelete.Delete Shift:=xlUp '<- changed, added shift up
        'If Not rDelete Is Nothing Then rDelete.EntireRow.Delete Shift:=xlUp '<- use instead of above if entire row has to be deleted
        MsgBox "Month End successfully completed", vbInformation, "Done"
    End If
   
End Sub
Many thanks
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top