Kill NULLs – a Simple macro to save time when importing data from SQL Server

As part of a my ongoing consulting gig, I often run painfully long queries on SQL Server to fetch data. This data obviously ends up in Excel for further analysis. Now, some of these queries return NULL values in several columns (did I tell you that the queries have a gazillion left joins on them, oh yeah, they do). Although technically NULL is nothing, when you import this data to Excel, we get the text value NULL in the cells. And I don’t need these NULL values messing up all the calculations and pivots.

SNAG-0077

Of course, we can go ahead and use the isnull() SQL function to deal with them at the query level. But since the queries have 100s of columns and used by various teams for different purposes, changing them causes a lot of pain. So I did what any sensible Excel user would do. Just kill those NULLs mercilessly once they are in Excel.

How to get rid of all NULLs?

Simple. Find replace. Just press CTRL+H and enter NULL as find value, replace with nothing, check “Match entire cell contents” option and viola. NULLs are gone.

Of course, doing this NULL Kill find replace can quickly get tiring and dull. So I went ahead and wrote a one line macro that does this and stuck this macro on the quick access toolbar. Now, whenever there is some new query data, I just press this button, play swoooosh sound in my mind and smile.

Here is the macro, incase you deal with the same problem everyday.


Sub killNull()
    Dim rng As Range
    
    Set rng = ActiveCell.CurrentRegion
    
    rng.Replace "NULL", "", LookAt:=xlWhole
End Sub

Here is instruction on how to add this macro to your personal macros workbook and how to add it to QAT or Ribbon.

Check out more short & sweet macros to save time.

Dirty data distressing daily?

If you deal with dirty data, please share examples of your problems in the comments. I am always looking for new material / ideas to discuss on the blog. Alternatively, if you have a smart way to deal with dirty data, post it in the comments. I am always looking for things to learn.