Stop delete range deleting "total" cells

AnnieS2Elise

New Member
Joined
Mar 17, 2018
Messages
5
Hi all

Can anyone tell me what's wrong with my code?

I'm trying to get a range to start with a set number of rows when I click a button, but not delete the "Ad_Total" row.

It needs to work whether I'm starting with 6 or 20 rows and always end up with 5 rows in a range, which the "Ad_Date" cell above and "Ad_Total" cell below.

Currently if I click my button enough times it deleted the "Ad_Total" row.

Thanks in advance
Annie

Code:
[TABLE="width: 72"]
<tbody>[TR]
[TD="width: 72, bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]Private Sub  New_Button_Click()[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]Dim Answer As String[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]Dim MyNote As String[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]Dim iStartRow1 As Integer[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]Dim iEndRow1 As Integer[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]iStartRow1 =  Range("Ad_Date").Row + 6[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]iEndRow1 =  Range("Ad_Total").Row - 2[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]MyNote = "Changing to a new month  will delete all current data. Have you saved a copy of your file?"[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]Answer = MsgBox(MyNote, vbQuestion +  vbYesNo, "STOP!")[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]If Answer = vbNo Then[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]Unload Me[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]Else[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]Dim List As Variant[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]Dim LR As String[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]Dim r As Long[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]List = Array("Ad_Total")[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]LR = Range(iStartRow1 &  ":" & iEndRow2)[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]If  IsError(Application.Match(Range("Ad_Total").Text, List, False))  Then[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]Unload Me[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]Else[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]Rows(iStartRow1 & ":"  & iEndRow1).Delete[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]Range("Ad_Contents").ClearContents[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]End If[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]End If[/COLOR][/SIZE][/FONT][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][FONT=Calibri][SIZE=3][COLOR=#000000]End Sub[/COLOR][/SIZE][/FONT][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Re: Stop delete range deleting "total" cells - URGENT

Can anyone tell me what's wrong with my code?
I seem you already answered this question: "It doesn't work"

To come with a good suggestion, however, you should explain which is the need.

So try explaining how your data are organized and what you wish to achieve.

Bye
 
Upvote 0
Re: Stop delete range deleting "total" cells - URGENT

Ciao Anthony47

Esempio e spiegazione di seguito.

Grazie

(Example and explanation below for the non Italians here)

Starting point:
2dqskjl.jpg
[/IMG]

I then run a macro to set the rows in that range back to 5visible rows (and one hidden)
63y3ba.jpg
[/IMG]

If I ‘accidentally’ run the macro again, this happens. I wantsomething in the code that will say if the range includes “Ad_Total” cell (whichis the total cell), don’t run the macro

28rma35.jpg
[/IMG]<strike></strike>

<strike></strike>
 
Upvote 0
Re: Stop delete range deleting "total" cells - URGENT

You use named ranges to point the crucial area of your worksheet; we (Italians, Germans, Americans) don't know their position, but I guess that you can play with iStartRow1 and iEndRow1 to check if the macro should be run or not. Something like:
Code:
iStartRow1 = Range("Ad_Date").Row + 6
iEndRow1 = Range("Ad_Total").Row - 2
'Check if enough space:
If (iEndRow1 - iStartRow1) < 5 Then       '<<< The "right" limit
    MsgBox ("Nothing to remove")
    Exit Sub
End If
MyNote = "Changing t... etc etc"
Bye
 
Upvote 0
Re: Stop delete range deleting "total" cells - URGENT

You use named ranges to point the crucial area of your worksheet; we (Italians, Germans, Americans) don't know their position, but I guess that you can play with iStartRow1 and iEndRow1 to check if the macro should be run or not. Something like:
Code:
iStartRow1 = Range("Ad_Date").Row + 6
iEndRow1 = Range("Ad_Total").Row - 2
'Check if enough space:
If (iEndRow1 - iStartRow1) < 5 Then       '<<< The "right" limit
    MsgBox ("Nothing to remove")
    Exit Sub
End If
MyNote = "Changing t... etc etc"
Bye

Works perfectly - thank you!!!!
 
Upvote 0
Re: Stop delete range deleting "total" cells - URGENT

Grazie per il feedback
 
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,976
Members
448,934
Latest member
audette89

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