Using VBA & OnKey to delete rows with blank cells in a certain column

gamaron

New Member
Joined
Mar 4, 2010
Messages
2
Disclaimer: I'm a complete VBA newbie.

I can get Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete to work in the "immediate" mode (with Cntrl-G in the macro editor) as per this post.

However, I'd like to map this behavior to a key combo. I can't get this to work:

Code:
Application.OnKey "+^{D}", "Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete"

Nor can I get this to work:

Code:
Application.OnKey "+^{D}", "DelRowsWithSelectedBlankCells"

Public Sub DelRowsWithSelectedBlankCells()
Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub

Suggestions? Do I have to declare the sub somehow? Do I have to map the macro to the sheet/workbook or something?

Thanks in advance!

(I suspect I'm missing some fundamental VBA step...)
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
The Public Sub part needs to be in a standard module, not in the same place as the Onkey event. Usually you put the OnKey event in the workbook_open event (or a worksheet_activate event if it's just for one sheet).

For example, put this code in the ThisWorkbook module:
Code:
Private Sub Workbook_Open()
Application.OnKey "+^{D}", "DelRowsWithSelectedBlankCells"
End Sub

Put this in a standard module (VB Editor -> Insert -> Module):
Code:
Public Sub DelRowsWithSelectedBlankCells()
Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub

Put this in the ThisWorkbook module as well:
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnKey "+^{D}"
End Sub

Hope that helps!
 
Upvote 0

Forum statistics

Threads
1,214,806
Messages
6,121,667
Members
449,045
Latest member
Marcus05

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