Thursday, February 10, 2011

Valentines Day Excel Workbook VBA

Admittedly, this could be made into so much more of an elaborate greeting but then perhaps flowers or chocolates would be more suitable.  

This short script, when placed within the 'This Workbook' section in the project section in your VB browser, will activate any time a change is made to that workbook (i.e. selecting a cell).

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    x = MsgBox("Will you be my valentine?", vbYesNo, "Valentine Greeting")
    If x = 6 Then
        MsgBox "I am yours"
        MsgBox "Happy Valentines Day!"
    End If
End Sub

As you can see, a pop-up window will appear with the question "Will you be my valentine?"  If the user selects yes you see a new pop-up window with "I am yours".  If no is selected they will only see "Happy Valentines Day!".  Surprise the Excel geek in your life with a workbook today and a custom message of your choosing and have a happy valentine's day!

Monday, February 7, 2011

Highlight fields in vba rather than conditional formatting

Ever wanted to quickly highlight cells where the values are not found in another list?  Here is an example of how VBA can accomplish just that!

Notice that there is an error handling used 'On Error Resume Next'?  If you run this macro without it you will have problems but this statement allows the script to continue on even though the error exists!