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...)
 

mvptomlinson

Well-known Member
Joined
Mar 10, 2008
Messages
2,638
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!
 

Forum statistics

Threads
1,081,693
Messages
5,360,664
Members
400,592
Latest member
katekoz

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top