Excel New Year Resolutions

January 2, 2012 8 comments »

Well folks, it’s officially the year 2012. And even though the Mayans have put a stigma on 2012 with all their “end of the world” mumbo jumbo, I’m very excited about the upcoming year.

.

This is the time for new-year’s resolutions. I was going to put down “stop procrastinating” on my list, but I never got around to creating it.

.

If you too have yet to create your list of new-year’s resolution’s, here are a few Excel resolutions you can adopt for 2012.

  1. Stop emailing workbooks called Book1.xls
  2. Stop using Merge and Center
  3. Stop hiding cell content using White font
  4. Start making your workbooks Print-ready, so we don’t print 25 blank pages when we print it
  5. Stop printing Excel eye charts with 8 pitch font

Feel free to add your own. Happy New Year!

Running a SQL Stored Procedure from Excel with Dynamic Parameters

December 12, 2011 8 comments »

In my life-changing post, Running a SQL-Stored Procedure from Excel, I showed you how to make Excel interact with a SQL server Stored Procedure.

.

Since that post, I've had several requests to cover the topic of passing dynamic parameters to a Stored Procedure. So today, I will FINALLY show you.

.

Step 1: Data tab – > From Other Sources -> From SQL Server

.

Step 2: Enter Credentials.

.

Step 3: Choose any small table or view. It doesn't matter which one because we'll be changing the connection anyway.

.

Step 4: In the Data Connection Wizard dialog, give your connection a friendly name that you can remember. This is how you'll point to this connection in VBA. In this case, I'm calling this connection MYSERVER.


.

Step 5: Excel will pop up the Import Data dialog box - Click Properties here (NOT THE OK BUTTON).

.

Step 6: Click on the Definition tab. Here, you'll want to change Command Type to SQL, and then enter your Stored Procedure name in the Command Text input. As you can see, the SQL statement is simply a call to the Stored Procedure along with the Parameter name (in this case, the procedure is expecting one parameter that accepts a market name).

.

Step 7: Excel warns you about something unimportant - Click Yes (which in this case, means go away).

.

Step 8: Excel will activate the Import Data dialog box again. This time click OK to fire the Stored Procedure and return the results.

.

Step 9: Note the results you get from your original hard-coded command you entered in Step 6. At this point, you know your connection is working.

.

Step 10: Create a mechanism to dynamically select and enter your parameters.

In my case, I need to pass a market name to my Stored Procedure. So I added a simple dropdown where my selection goes to Cell B2. The mechanism you choose to select your parameters is not the important thing here. The important thing is to note where the final selection will be housed. In this example, my market selection will end up in Cell B2.

.

Step 11: The final step is to copy and paste this Macro into a standard module. Notice that all we are dong here is changing the definition fo the CommandText property of the Connection. This basically means that as long as you can point to the correct connection (MYSERVER in this case), you can rebuild the CommandText on the fly. Here, I'm rebuilding the CommandText to point to my chosen cell range (cell B2 - where my market selection is housed).

Visual Basic:
  1. Sub RefreshQuery()
  2.  
  3.     With ActiveWorkbook.Connections("MYSERVER").OLEDBConnection
  4.         .CommandText = "EXECUTE dbo.Tng_Market_Feed '" & Range("B2").Value & "'"
  5.     End With
  6.     ActiveWorkbook.Connections("MYSERVER").Refresh
  7.    
  8. End Sub

.

At this point, it's just a question of firing the Macro each time your parameter selection changes.

Now you're cooking with gas!

Buying Apps on Your Smart Phone

December 2, 2011 2 comments »

On this Friday, I'd like to share another life lesson from The Oatmeal:
» Read more: Buying Apps on Your Smart Phone

Open CD Rom – The Most Useless API Call

November 28, 2011 1 comment »

Over the Thanksgiving holiday, I was ignoring the family and puttering around on the internet. There, I found a list of all the cool API calls you can do through VBA. An API call is essentially a piece of code you can use to make the Windows operating system do things – like open a directory, freeze the system, open a dialog box, etc.

.

Well, there is an API Call that essentially opens your CD Rom drive. That's right – you can implement code so you don't have to go through the painstaking task of pressing the Open button for your CD Rom drive. Wow …. AUTOMATION IS THE BEST!

.

OK…this gets my vote for the most useless API call. Second place is the API call to close the CD Rom Drive.

.

That being said, you could use this useless API to prank your coworkers.
» Read more: Open CD Rom – The Most Useless API Call

Happy Thanksgiving 2011

November 22, 2011 2 comments »

Happy Thanksgiving to all you Excel Nerds out there.

I hope you all enjoy your time with family and friends.

.

I'll return next week with more stuff.

Take Your Kids to Work Day

November 21, 2011 2 comments »

Can we all admit that Takes Your Kids to Work Day is a joke?

Who is getting any serious work done with little a-holes hanging around?

.

On my last "take your kids to work day", my kids asked if they could make paper airplanes and fly them around the office.

Does that sound like I'm getting any real work done?

.

It should be called let your spouse sleep in day, because that's the only real benefit of this charade.

 

I need a workforce reduction.

Easily Navigate Many Worksheets

November 14, 2011 6 comments »

Last week, Debra over at Contextures posted an article that showed us lots of nifty ways to navigate a workbook that has many worksheets. Today, I'd like to add my two cents by sharing a small macro that will give you a fairly easy way to jump from sheet to sheet.

.

We all have them – those workbooks that have 10, 15, 20 or more tabs.

I personally have lots of training workbooks that have 15+ tabs.

When you've got a workbook that has this many sheets, it can be a pain to find the sheet you need.
» Read more: Easily Navigate Many Worksheets

Use Fill Handle to Fill in the 1st and 15th of Each Month

November 7, 2011 No comments »

Here's an easy tip from Excel MVP Bob Umlas – author of the new book, Excel Outside the Box.

.

Let's say you want to fill in some rows with the 1st and the 15th of every month.
» Read more: Use Fill Handle to Fill in the 1st and 15th of Each Month

Merging Two Staggered Columns

October 31, 2011 5 comments »

Last week, I was working with some data when I encountered the need to merge two staggered columns.

That is to say, I needed to merge two columns where the data was blank in either one column or the other.

.

Here is an example.
» Read more: Merging Two Staggered Columns

Formatting Access Queries to the Thousands Place

October 24, 2011 No comments »

In an Email last week, Christina asks:

"Do you know of an easy way to show Access query results in thousands?
Thanks DataPig.You are the like that black bird on Angry Birds – the bomb."

Ok….so I added that last sentence. But Christina did ask a pretty good question.

.

Here's how you can format Access query fields so that they show as thousands.

.

You first start with a working query. As you can see, this query shows revenue by market with an indication of how much of that revenue comes from Internet Sales. You'll notice that the revenue columns inundate you with lots of numbers. That is to say, the revenue columns are difficult to read. Showing these columns in thousands would help readability.
» Read more: Formatting Access Queries to the Thousands Place