VBA - delete rows only in specific range

Worf99

New Member
Joined
Jan 13, 2018
Messages
44
Hi to everyone and Happy New Year.

I wrote a very simple VBA code (associated to a hot key CTRL+D) to delete the current row in a sheet:

Sub del_row()


Set foglioattivo = ActiveWorkbook.ActiveSheet


Rows(ActiveCell.Row).Delete


End Sub


I would like this to work only from row 9 to row 40, so that if the users mistakenly hit CTRL+D on the first rows it doesn't delete anything.

How can I do that?


Thank you very much.
Matt
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
this should work

Code:
Sub del_row()
    
    If ActiveCell.Row >= 9 And ActiveCell.Row <= 40 Then
        Rows(ActiveCell.Row).Delete
    End If

End Sub
 
Upvote 0
Hi KKaren,
that works perfectly!!!! Thank you so much!!!!

In the rows the users can delete with the hot key there are input data and formulas, so to prevent accidental deletes by the users, I pass-protected the sheet.

My code is actually this below, the sub has to remove the password and activate it again after the delete invoked by the hot key.
It takes about half a second or even slighy longer.
I'm wondering if there is a faster way to achieve this...

Sub cancella_riga()




Set foglioattivo = ActiveWorkbook.ActiveSheet


'sproteggo il foglio
ActiveSheet.Unprotect Password:=" "
Call AutoFiltroOff


'cancella la riga
If ActiveCell.Row >= 9 And ActiveCell.Row <= 100 Then
Rows(ActiveCell.Row).Delete
End If


'riproteggo il foglio
ActiveSheet.Protect Password:=" ", AllowFiltering:=True
End Sub

---------------------------------------------------------------------

Public Sub AutoFiltroOff()


Dim wb As Workbook
Dim ws As Worksheet


Set wb = ThisWorkbook
Set ws = wb.ActiveSheet


'azzera le condizioni del filtro automatico
'e mostra tutti i dati
If ws.AutoFilterMode Then
If ws.FilterMode Then
ws.ShowAllData
End If
End If


Set ws = Nothing
Set wb = Nothing


End Sub
 
Upvote 0
How much difference does just turning screenupdating off make?

Code:
Sub cancella_riga()


    Set foglioattivo = ActiveWorkbook.ActiveSheet

    Application.ScreenUpdating = False
    'sproteggo il foglio
    ActiveSheet.Unprotect Password:=" "
    Call AutoFiltroOff


    'cancella la riga
    If ActiveCell.Row >= 9 And ActiveCell.Row <= 100 Then
        Rows(ActiveCell.Row).Delete
    End If


    'riproteggo il foglio
    ActiveSheet.Protect Password:=" ", AllowFiltering:=True
    Application.ScreenUpdating = True
End Sub


Code:
Public Sub AutoFiltroOff()


    Dim wb As Workbook
    Dim ws As Worksheet


    Set wb = ThisWorkbook
    Set ws = wb.ActiveSheet


    'azzera le condizioni del filtro automatico
    'e mostra tutti i dati
    If ws.AutoFilterMode Then
        If ws.FilterMode Then
            ws.ShowAllData
        End If
    End If


    Set ws = Nothing
    Set wb = Nothing


End Sub
 
Upvote 0
Hi and thank you.
I didn't know that command and it helps in other part of my file.
The part I'm working on is slow because it depends on a web query.
I think I need to halt the calculation and resume it but I have to be careful.
Thanks again!
 
Upvote 0

Forum statistics

Threads
1,214,422
Messages
6,119,395
Members
448,891
Latest member
tpierce

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