Welcome to MSDN Blogs Sign in | Join | Help

Improving Sheet Selection

Today's author: Sam Radakovitz, a Program Manager on the Excel team that enjoys creating VBA applications in Excel.

A bit ago I got a question through email asking if we could change the 'grid selection color' in Excel. Or at least I called it 'grid selection color', it's basically what the cells look like when you select a block or multiple blocks of them:

As you can see, the color is a bit light and hard for some folks to see, especially on a laptop if you're looking at it from the wrong angle. Unfortunately, in Excel 2007 you can't really change the color of the selection. You used to be able to change the 'Selected Items' color in the Windows appearance settings but that doesn't work with Excel 2007. This is something we're looking to address in the Excel … but until then I wanted to try and provide an add-in solution. I tried a couple of solutions and I wasn't 100% successful with either attempt, but maybe a code savvy blog reader can pick up where I left off and finish the task.

Approach #1, Shapes

My first approach was to use OfficeArt shapes to create the selection. It's pretty easy to do code wise: you get a selection change event, you loop through each area in the selection and drop a shape on it:

I take the shape and inset it a little bit in the selection so the drag fill is still available, then pick a darker color and make it a bit transparent. I converted this into an add-in and it was good to go … but feature creep sets in and I start thinking about the different things I can do. In short, I added some options and created a ribbon entry point to control them:

I added an option on the menu to turn on/off the selection add-in, a color chooser, transparency control, and some additional information you can display in the selection like the count of cells (different that the count in the status bar btw) and address. So now the selection looks like:

As I was playing around, I found one of my favorite things to do is to make the selection color a random color … not sure why it's so entertaining, but I couldn't stop selecting things after enabling the option:

The final interesting 'feature creep' item is the ability to set a formula and have it evaluated on each selection, then return the results to the text in the selection. It's the "custom formula…" menu item in the picture above. For an example: In the picture below, I create a 'countif' formula counting the number of times 'Sam' is in the selection, the formula I type in is:

="Number of Sams: " & countif([selection],"sam")

The [selection] part is the part replaced with the address of the active selection. The final result:

Overall using a shape is an interesting idea … but it isn't a 100% win for two main reasons … and some other smaller ones, but I'll speak to the ones I think are deal-breakers:

No Undo

Because the selection event fires often, and we are creating a shape in the active sheet, this kills the Undo stack … and people need their Undo stacks! Maybe one day, we'll be able to write code and not blow away the Undo stack, but until then, this is bad bad bad.

No right click

Because it's a shape, I can catch the left click no problem, but the right click isn't catchable, and puts the shape into edit mode. I've altered the left click on the shape to show the right click menu by sending a mouse click to Excel, but that isn't enough. I *thought* I had a work around for this … my plan was to create an ActiveX label control over the shape and make the label transparent, but catch the clicks since the label has a left and right mouse button events … but I couldn't get the label created w/o blowing away my global variables … and then it didn't surface the events as it should. Maybe something I was doing wrong, but if someone gets that working, please let me know!

Approach #2, Userform

My second approach didn't get too far, but I think its superior in that it addresses the two deal-breakers above. My plan was to have an add-in create a modeless and unclick-able form, which would then move with your selection in Excel. Since you can't click it, your mouse commands work great … and we aren't adjust things that cause the UNDO stack to get blow away. I got the form to be unclick-able and transparent, but the big problem was positioning and sizing the form:

The position and size of the shape solution (the first approach) was easy since we're using the grid coordinates to position the shape, and the top/left of a range is the same for the shape. For the form, we're using the screen positioning, so some translation has to happen between the grid position and the form position. There are some functions that are supposed to help with this, but they don't work as I would expect. An Internet search turns up some folks who have figured it out, but it's not trivial at all and at the moment I don't have the time to research it further.

My final words

Both solutions are available for download; see the links at the end of this article. If you're one of the code savvy readers who wants to alter the add-ins, by all means download it and shoot us a mail and tell us about what you did! Also, if you're reading this and have an idea or any comments on the add-ins or Excel grid selection in general, don't hesitate to shoot us a email or post a comment.

Published Tuesday, April 22, 2008 3:37 PM by Joseph Chirilov
Filed under:

Attachment(s): Selection-Shapes-and-Userform.zip

Comments

# re: Improving Sheet Selection

Thursday, April 24, 2008 5:05 AM by Tom Page

Some nice tricks given the fundamental nature of making selections. I have to say I like the appearance of selections in Excel 2007 - I think it's just right, both on my laptop and desktop at work. On a more general note I should say Excel 2007 is such an improvement over earlier versions - I hope the next version is similarly impressive.

# re: Improving Sheet Selection

Thursday, April 24, 2008 5:54 AM by Val Lomax

The best way I have found to enable the selection to be seen, easily, in Excel 2007, is by changing the Display Properties to 'Windows Classic Style' with colour scheme 'Marine (High color). This works for me

# re: Improving Sheet Selection

Thursday, April 24, 2008 6:37 AM by Jan Karel Pieterse

Nice job.

The userform idea is challenging, but I went that route once and it took me too much work to justify trying to do this with your code, sorry!

# re: Improving Sheet Selection

Thursday, April 24, 2008 7:25 AM by Ian Hamilton

This typifies the colours in Office 2007 and one of the main reasons why I dislike it. Outlook especially is so wishy washy that I find it very  hard to use. Along with the fact that one can no longer drag and drop on to the ribbon as you could with office 2003

# re: Improving Sheet Selection

Thursday, April 24, 2008 8:08 AM by Jon Peltier

The lightness of the selection in an Excel 2007 worksheet has been a problem since the earliest betas. The highlighting of the row and column headers does help a lot, but in the early mornings before my eyes wake up <g> I often have more trouble than usual. Most of my work is still in Classic Excel, so it's not a problem all the time, but the contrast between versions makes me wonder why it happened.

# Replies

Thursday, April 24, 2008 2:18 PM by SamRad

Tom - thanks for the kind words!

Val - thanks for the tip, some folks might find it helpful!

Jan Karel - yeah, at first glance the userform seems simple: just position it over the selection ... but actually doing that is rough, especially if you want to account for panes and zoom and a variety of other things.

Ian & Jon - thanks for the feedback, I'll pass it onto our user experience team.

# re: Improving Sheet Selection

Thursday, April 24, 2008 2:34 PM by Tim Mayes

This was a serious problem for me on my Vista PC, but not on XP. On the Vista machine I finally decided to adjust the video card driver settings to turn up the contrast a few notches. That works for me, but probably not somebody who does professional graphics/desktop publishing and needs absolute color accuracy.

BTW, this was a real hassle on Vista because NVIDIA took forever to come out with a driver for my card that could actually save the new settings.

The selection color should definitely be an option in the next version of Excel. If we can change the colors of gridlines, why not the selection color as well?

# re: Improving Sheet Selection

Thursday, April 24, 2008 2:49 PM by Andy Pope

The shapes approach is quite cool.

As well as position issues the userform approach is limited when dealing with non contiguous selections.

# More Replies

Friday, April 25, 2008 1:53 PM by SamRad

Tim - thanks for the contrast tip.  I'll forward the selection change color comment to our user experience team.

Andy - shapes were fun to do.  with non-contiguous selections, i'm guessing i'd need to create a collection of the userform and position them all around, similar to creating mutiple shapes.

# re: Improving Sheet Selection

Wednesday, April 30, 2008 11:02 AM by Chris

They both looked like pretty slick solutions until we hit the gotchas.

Good point about non-contiguous selections Andy - for me these are a staple of day-to-day Excel usage.

# re: Improving Sheet Selection

Wednesday, April 30, 2008 11:15 AM by Stephen Bullen

Anything in the Accessibility API to tell where the selection is in screen coordinates?

# Even More Replies

Wednesday, April 30, 2008 4:46 PM by SamRad

Chris - Agreed, all good until you hit those road blocks.  I think the discontinuous selections and the userform method can be overcome, just getting the positioning of the form is tough.

Stephen - I don't think there is.  If memory serves me right, off the activewindow there is a method to convert the selection coords to the screen coords, but it doesn't appear to work for me ... or work as I thought it would. If you search the web for form positioning in Excel you'll find some folks who were able to do it, but it wasn't easy and required more than just one method.

I'd love to hear if someone has an easy way to do it.

# re: Improving Sheet Selection

Wednesday, April 30, 2008 11:20 PM by Ian Huitson

The color must be stored as a variable somewhere inside the excel executable, so why can't the excel team issue a small patch to add a selection color selector to choose the color in the Excel Options menu ?

The fact that it isn't part of or accesible to the the object model doesn't matter

# re: Improving Sheet Selection

Monday, May 05, 2008 12:37 AM by Bernie

Hi - well done! An ambitious piece of work! I wouldnt have had the guts to even attempt that!

I just tried turning the problem on its head...

I created a workbook and then create and ran the following code.

It changes the interior colour of each column and inserts the colour index value as the column heading....

Sub ColourTheColumns()

Dim i As Integer

Dim c As Range

Dim r As Range

Dim ws As Worksheet

On Error GoTo Err

Set ws = Worksheets(1)

With ws

 Set r = .[A:A:IV:IV]

 For i = 1 To r.Columns.Count

 Set c = .Columns(i)

   c.Cells(1).Value = i

   c.Interior.ColorIndex = i

 Next i

Err:

 .Rows("15:30").Select

End With

End Sub

Then I selected about 10 rows.

I left them selected while I scrolled across until the end of the coloured columns taking note of which colours provided the best contrast between the selected and unselected rows.

In my opinion it was 17 (a light purple) and funnily enought 48 a medium grey.

If you want to be able to distinguish between selected and unselected ranges more easily, try changing the background colour to a colour that produces the greatest contrast when highlighted.

You can do this by creating an excel template that has the background set to your chosen colour already.

just a thought

New Comments to this post are disabled
 
Page view tracker