Tag: Examples

Drawing on a VBA Userform

I’m making a few post with old stuff from my main site (which is now closed), this is so I can point to the blogpost when I update my downlaods page, which I am planing on doing at some stage in the next few years!

This example uses an API to let you actually draw on the form free hand style!

drawonform

It  can be downloaded here: Drawing_v3

Beyond Excel Comments

Beyond Comments in Excel.

I’m not sure how it happen. I’m not sure when it happen. At first I was shocked, then I felt used, now just anger. [;-))))]
Somehow Mike Alexander has hacked into my laptop, and started to steal my blog post ideas. I use ATnotes to jott down any ideas I have for blog posts, here what’s on my desktop at the mo:

Comments in Excel

As you can see top of the list is “x things I hate about Comments in excel”. Well the old piglet has just posted this – nice work. So slightly after, slightly following here’s my list:

  • Can’t change default colour via UI
  • Cant change the default shape via the UI(?)
  • They look rubbish – mac, google anyone
  • Can’t set the start up position – robustly
  • Damage formatting of cells
  • Don’t display correctly with frozen panes etc
  • Can’t get rid of the line (?)
  • The line & arrow are poor quality
  • Are as technically advanced as a stone wheel.
  • Have never been significantly updated
  • Indicator triangles are wrong colour by default (green is a formula error?!)

And I bet there’s more. Unlike Mike though I’m not implementing them for tool tips. I’m interested in error feedback.

Modern programming technologies like .Net, have error providers, these are controls developers can use to track and respond to errors, proactive and reactively. Comments are dumb. They’re just there, and from a user experience P.O.V they don’t offer much. Sure there’re better than nothing, but only just.

Luckily we can work around this. Its really quite easy to put together a formula which can check for errors, then display a message accordingly. It won’t solve all of my issues but it’s another tool in the tool box. Here’s an example from a project I recently worked on. In this case data was entered into a “table” layout, errors where reported at the top of the document. I used a set of functions to the right hand side of the input table to check the important cells in the table.

Comments In Excel 2
Here’s another example but for a more orthodox form. It’s clear to see the benefits over the comments here. If the user enters the right data then no errors message is given, but when it’s wrong, bingo they can get some meaningful feedback, you can also add an additional checks at the end to see if there are any outstanding error on the sheet. As you can imagine you can make this as simple or as complex as your needs require.

Comments In Excel 3

So there you go, if you’re building a worksheet or form thats going to get used a lot, by a lot of different people, then maybe you should take the time to implement custom error messages, and get ride of those rubbish old comments!!!

P.S here’s the last example: MIE Tool Tips Comments Example

Excel 2007 Tables – the basics

Today I took my first look at Excel 2007 tables. I’ve hardly use Excel 2007, for obvious reasons, so I’ve not really played with many of the “new” features.

I did a bit of research and basically there are already a few good spots out there for table related stuff:

An incredibly cheesy Microsoft video, it does show all the main features (doesn’t that table look professional!)

Jan Karel Pieterses’ introduction and VBA methods

And Ron de Druin VBA page here

This chart shows my feeling towards tables in Excel 2007 over the last 3 hours:

Excel 2007 Data Tables

In summary, I think tables have been done really well, there are easy to use and makes sense. I think that the new reference system (table name[column])  is good, but just like named ranges, can cause as many issues as it might solved, especially when used by relatively low skilled Excel users.  Most of the stuff that I can see in tables was already there, and has “just” been made much easier to get at, I like the auto-update-y nature of tables, but that always worries me a bit too.
Just for completeness I have done a quick video to add to the tables database!

Setting the default value of a Class in VBA.

There is no keyword in VB6 that allows you to set the default value of a class. Code like this for example will error out.

VBA:
  1. Public Sub Test()
  2.  
  3. Dim x As New MyClass
  4.  
  5. x.Name = "Ross"
  6. x.Name2 = "Dave"
  7.  
  8. MsgBox x
  9.  
  10. End Sub

However it can be done, even if it's a bit of a pain! Export the class and add the following code in note pad, as the first line under the function heading:

Attribute Name.VB_UserMemId = 0

The value must be 0, and the Attribute must be the name of the function, get, etc. you want to use as the default. The notepad file might look some thing like this!

MyClassPic.PNG

Save the changes in notepad, in the IDE delete and re-import the class. The code will now work! Amazing! If you make any major changes to the class code you will have to redo the process, that's the PITA bit!

Example Workbook: DefaultTest.xls

Validating file names

"Validating" file names is one of those things that no matter how hard you try you'll never get it 100% right. There are a loads of reasons when a file might not save, even if the name is valid, acess rights, disk space, problems with the file it's self, network issues...
Basically you have to trust the user a bit and try to pick up any basic errors.
With that in mind I found this was the simplest, and most 'bang for buck' type of function to use. It's easy and gets it right most of the time. You still need to manage errors well when you save the actual file though!!!

VBA:
  1. Private Sub txtFileName_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  2.     If (Me.txtFileName.Text Like "*[/\:*?""<>]*") Then
  3.         a = MsgBox("Invalid filename format, please try another, " _
  4.             & "Stuff like /, @, ~, ?, ! etc does not help.", , _
  5.             gsAppName & gsAppVersion)
  6.  
  7.         Me.txtFileName.SetFocus
  8.     End If
  9. End Sub

Here I've added it to the Exit event of a text box, so it runs each time the user leaves the text box. Another option would be to run the check at the end of a form, i.e when OK is pressed. You'll have to think about which method will work best for your application.