Monday, August 16, 2010

Playing with Excel Worksheet Change event

Workbook sheet change is a very good place to put your code if you want to track user actions. Depending on user action, you may want to write validations or database round trips. It can also be a substitute for "formulas" if written correctly. But it is easy to overlook scenarios and user interaction patterns, and make mistakes in coding.

A good way to begin is by creating sub routine to manage your operations on the Excel sheet. This is a global sub that can be called to set when some "controlled" system operations are going on. (System = your code).

The following code can be placed in a public module.

Option Explicit

Public bSystemUpdating As Boolean, oLastCalulation As XlCalculation
'Flag to indicate start of system update

Public Sub ManageSystemUpdate(bStartUpdate As Boolean)
If bStartUpdate Then
    
    If bSystemUpdating Then Exit Sub
    'Exit if update by system already started
    
    bSystemUpdating = True
    oLastCalulation = Application.Calculation
    ' Store Calculation mode for reseting when done
    
    Application.Calculation = xlCalculationManual
    
Else

    Application.Calculation = oLastCalulation
    bSystemUpdating = False
    
End If

Application.EnableEvents = Not bStartUpdate
'To disable Excel events like Worksheet change

Application.DisplayAlerts = Not bStartUpdate
'To disable Excel alerts like "Delete Sheet confirmation" etc

Application.ScreenUpdating = Not bStartUpdate
'To disable Excel screen updatation... Excel will update the screen on completion of your routine

End Sub

What next? Now we take the "Workbook_SheetChange" event and add our custom code into it. Remember, the "Workbook_SheetChange" event gets fired everytime any change happens to the cells, i.e., including your code. So it is important to manage the re-firing of these events in case you make updates. This can be done by setting a global variable to flag the start of a system update.

Option Explicit

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Not Application.Intersect(Application.Range("Sheet1!A:A"), Target) Is Nothing Then
    'Only if any cell in the column "A" changes
    
    FetchFooValues Target
    
End If
End Sub

Public Sub FetchFooValues(oRange As Range)
Dim lCtr As Long

    ManageSystemUpdate True
    'Disable all events as well as all updates and calculation by Excel application
    
    For lCtr = 1 To oRange.Rows.Count
        Application.Range("Sheet1!B1").Offset(lCtr - 1).Value = FetchSingleFooValue(Trim(Application.Range("Sheet1!B1").Offset(lCtr - 1).Value))
    Next
    
    ManageSystemUpdate False
    'Enable all events
    
End Sub

Private Function FetchSingleFooValue(sInput As String)

'Do some operation here
FetchSingleFooValue = sInput

End Function

All done!

Thursday, August 12, 2010

Excel Template

Time and again, we create new Excel files with VBA, and end up rewriting our prized VBA codes (or copying pasting at best) that could be reused. Some of us have our own sleeveful of Excel files containing VBA codes.

There are some good ways of organizing (and growing) them. The most obvious way would be something like in Word... "normal.dot". In Excel unfortunately, such a thing does not exist. But we could do something close. Make a template file that could be used as a standard from the default "New Workbook" dialog.

Write the VBA codes in a new Excel file and save it as a Template, say "My_VBA_Template". The VBA code could be written anywhere as usual - as new modules or written as workbook/worksheet events.
Remember to save the file as a "Macro-enabled Template (.xltm)". You can now place the file in any of the following locations depending on the usage:
  1. C:\Program Files\Microsoft Office\Office12\XLSTART\  -or-
  2. C:\Documents and Settings\<user>\Application Data\Microsoft\Templates\
Now start Excel normally and open a new workbook. Choose the "My Templates..." from the access panel.

From here, choose the your template "My_VBA_Template".

A new file with the embedded macros would be created "My_VBA_Template1" (just like "Book1" normally).

For all future uses, a good thing is that, your templete would appear in the Recent Templates section and hence much easier to access.

Happy Excelling!

Wednesday, August 4, 2010

Unlock a VBA password protected Excel file

Ever felt the need to open a VBA protected excel file... maybe one of your old files that contained an excellent routine! How do you come out of that pain?

Important: This article is for educational purposes. Try this method for opening ONLY your own files, as I did too!

So how does Excel store the file contents - cell data including formulas and formats, conditional formatting, VBA code, etc. etc. Lets investigate. Create a new Excel file MyTest.xlsm and enter some dummy test data in the first sheet. Add some formulas and conditional formatting (if you want to really understand the details).
Let us now see how excel stores this data in the file. Open the file in notepad or a hex editor. Did you notice the first 2 characters? "PK". So Excel compresses its file contents. Now we know why there is not much difference if you compress an Office 2007 file.
Lets look into the compressed contents. Rename the file extension from .xlsm to .zip
Open the MyFile.zip file. Wow! its an extensive structure with xml files to store the workbook, worksheets, calculations, sharedstrings, etc.
This is how the XML of the Sheet1 looks
Lets explore more. Lets go back to our original file and add some VBA code to it.
Add a password and protect the VBA code.
Save the file and redo the same steps as earlier to open the xml file structure. We now have another XML file called vbaProject.bin. This is the file that I need to recover. Lets investigate further. Open this file in a Hex Editor (there are lots of free ones out there... the one I use is Hex Editor Neo at http://www.hhdsoftware.com/Products/home/hex-editor-free.html).
Search for the keyword "DPB" in the content. Remember to find it just above "[Host Extender Info]".
Once found, replace the string "DPB" to "DBx" and save the file. Now replace the edited "vbaProject.bin" file and place it back into the compressed file collection (replace with the old one). Rename the compressed file back to ".xlsm".

Try to open the file in Excel. It gives an error saying that the file contains an invalid key DBx. Hit Yes and proceed. The file opens. So far good. Now go to the VBA editor.
Next it gives another error "Unexpected Error". Proceed again by hittng OK
VBA Editor opens. Now try to open the module code. Again the "Unexpected Error". Dont lose heart... we are just there. Save the file and close it. We are all set. Your file is as good as new without the VBA password. Go check for yourself!

So Excel may NOT be encrypting the VBA file after all, rather it is only setting a flag to lock the VBA content. This is evident because we can still see the code in the vbaProject.bin file (that we had written in the VBA earlier).
Some more interesting bits. Check how Excel stores its file contents. Look into the file sharedStrings.xml. It stores the strings at a global level, not in the sheet itself. Also look into how it stores the formulas and formatting. Try out things like like dragging a cell content (like Area 1) into 5 cells (producing Area 1, Area 2, Area 3...) and see how it stores the values... using ranges.

That also says that, it may be better to access Excel data directly from these XML files rather than through the Excel application model. We could have much more control and simplified logic for storing (for instance in database), transporting (eg client machine to server), opening and displaying simple content. Many possibilities here...

Well, I was pretty pleased that today I learned something new... and an unlocked VBA code.

Saturday, March 27, 2010

Business of Software Conference 2008 (Part 2)

Excerpts from lecture by Dharmesh Shah, OnStartups.com
  • At startup, your idea may "suck"... get started!
  • Say "NO" to venture capital
  • Target liquidity
  • You don’t have to go out there and market (broadcast) to get people interested!
  • Real EV of raising venture capital is high (add difficulty in getting one)
  • Nobody reads Business plans... rather write a blog!
  • All startups should be embarrassed about their product when it goes out into the market... If you are not embarrassed, you have waited too long!!
  • Don't depend on advertising to sell your product
  • You are not gonna be able to get the "pricing" right anyway... so just pick one
  • Starting as early as possible and "charge" as often as possible
  • SAAS - You are now the customer
  • COCA - Cost of customer acquisition
    • = Money spent to get a new customer onboard
    • = (Total cost of sales + marketing/Number of new customers) - lifetime value of the customer (LTV)
    • Therefore, one of the biggest success factors - lower attrition rate
  • CHI - Customer Happiness Index - Probability of customer that he will stay next month
    • Above can be correlated with (and hence should be monitored closely)
      • Did the customer use the software, usage patterns (e.g. customers who use "a" feature are 4 times more likely to stay than who don’t
      • Track usage data - by feature... who recommend it + which customers used it and how many left + who approved it
  • Don’t do "partnerships"
  • Go to the market even before you have written the first line of code
  • Manage Risks - technology risks, market risks
  • Try and reduce market risk as early as possible
  • "Pay per click" is a morphine drip

Business of Software Conference 2008

Excerpts from lecture by Jason Fried, 37 Signals (the Back Pack guy)
  • Maintain "momentum"... do small projects
  • Build the "real" thing (UI)... avoid abstraction (unwanted documentation)
  • Do what works now! Optimize for now... No decision is permanent
  • Avoid words like - Need, Can't, Easy, Everyone, Nobody
  • Have Uninterrupted stretch of time for productive work
  • Focus on what is NOT going to change - Speed, Simplicity, Clarity, Ease of use, Reliability, Uptime
  • Keep questioning... always and all sorts
  • Focus on "easy" ones "first"

Wednesday, March 3, 2010

Sharing Agile experiences of a Scrummer

Jean Tabaka has recently compiled a list of tips and tricks to scrum better. I thought it is worthwhile to share it. We may know already know most of them, but penning them down into a list makes it interesting! And, alomg the way, it has some great links too.

Here is the article.

Tuesday, February 16, 2010

Karaoke - Bade Achche Lagte Hai

For long I have been searching the karaoke version of this song. Finally, here's the link to
Bade Achche Lagte Hain – Amit Kumar – SD Burman – BALIKA BADHU.

Friday, February 5, 2010

Flash as a UI: Integration with HDP Applications

As posted earlier, here is an application of the flash Dial component used from within a C# app. During this exercise, I have learnt a few good things. Flash can easily be used as a front end for a traditional application written in .NET or Java.

Flash, over the years have mastered the art of screen displaying content. It has excellent layout placement, UI precision as well as rendering capabilities. This, along with the great filters makes it a ideal for UI. On the other hand, languages like C#, VB, VB6 and Java are very strong from programming point of view. Memory handling, disk and shell interaction, interaction with the OS and now more recently web services are better handled by heavy duty developer platforms (HDP). But the HDP have always lacked (though we have probably not acknowledged) pretty graphical UI. A complex user interface does not have to, NOT look "chick".

The midway, Flash UI for a HDP App, has a lot of potential in store. A probable contender is AIR, but would lose out owing to the fact that legacy applications are still on the HDP (the developers lobby!). Also, AIR may be (at least initially) viewed as, not sturdy enough for HD applications. Lets look at the "Flash UI for a HDP App" approach.

The problem has always been the interaction of the HDP with flash. The common (unwilling crime) has been taking the "SetVariable" route. Here we update a variable from outside Flash and wait for it to detect the change asynchronously. The "ChangeVariable event is fired and all the processing is done within Flash without an active feedback back to the HDP app. This seems more of a HACK rather than a solution.



Long awaited, there is now a mechanism to run an operation synchronously from a HDP app to flash and back. It is done through "ExternalInterface" and can be smoothly called from HDP as well as javascript.


- or -


Here is an example created to demonstrate the above in .NET using an earlier created Flash component. Google out "ExternalInterface Flash Samples" for understanding the concept.


Another big advantage of using a Flash UI is, that it is usable both in desktop applications as well as web app. You develop a single UI... and it is deployable both on desktop and web; and looks and interacts exactly the same way. Sometime back, I came across a paper (pdf report) done around this area (citation invited)(* updated below) that talks about Flash as a front end. Go ahead, indulge... You can even afford to keep f-in-box aside and start on your own!

To sum up, Flash needs to be seriously tried as a front-end for complex, enterprise "user interaction intensive" applications like ERP, industrial automation, simulation, etc. I foresee frameworks, interface layers and design templates in the Open Source being developed to support and enhance such initiatives. A Flash UI could give a product company that small winning oomph over competition.

*Updated on 10th Feb 2010: Here is the pdf mentioned in the post above. It was a March 2003 paper taking about "Macromedia Flash and VB: A Potent Combination".

Sunday, January 31, 2010

Embracing scrum

Introducing scrum to a first-timer team may not necessarily be difficult. Even if the team is completely ignorant of the scrum-way-of-working, the following steps could ensure a smooth process sustenance.
  1. As a scrum master, focus only on the scrum process for the first couple of sprints. Ensure that none of the steps are bypassed and every step (that has been decided by the process team) is adhered to as a religion.
  2. Hand hold the team about what is to be done at each step - what questions are we trying to answer here? Develop your own "team jargon" that the team can connect to, for each activity.
  3. Punctuality is a MUST. All meetings start absolutely at the decided second, and ends as decided or earlier. All meetings have to be conducted and recorded even if there is nothing to discuss.
  4. Conduct the Daily scrum meetings with passion. New teams members start respecting and appreciating scrum from the Daily scrum meetings. Keep the sessions clean and crisp. (If you are not confident, practice your part thoroughly or attend other scrum meetings). These sessions will help you drive across the core spirit of a scrum team.
  5. Celebrate each sprint closure.

Saturday, January 30, 2010

Flash Dial (Gauge) Component

For an application, I needed a Dial (Gauge) component made in flash (preferably in AS3), and open source!. After searching for sometime, I got some great, and some not-so-great components. A few are listed below:
  1. One of them, a classic, is from Bright Point, but unfortunately in flex.
  2. A good collection of components from AnyChart lie here, but not free.
  3. Another one was from XML/SWF, mediocre, but again paid.
Hence, decided to write my own. I am sure this could benefit few of you too.

Objective:
  1. Create a flash Dial component,
  2. that had capability to define regions (at least 3),
  3. as well as support percentage display,
  4. customizable background colors,
  5. real time value update without refresh would be bonus.

Here is the outcome written in flash (<30kb). It has the following methods:
  1. InitiliazeComponent: takes the following parameters: Minimum Value, Maximum Value, Percent Display Needed?, Region Definition
  2. DisplayNewValue: takes "New Value" as parameter. 
Rather than using the traditional SetVariable route, I used the ExternalInterface (which is by the way, newer and much better). Some very good posts are available in this regard, here. The ExternalInterface is also pretty smooth when calling from .NET applications. Will be covering this area later.

Here is the application being supported by a javascript test page. The loading of display parameters as well as dial value is done through javascript. The body onload event in the browser did not wait for the Flash SWF to load completely, firing much before the movie actually got loaded. Thanks to the new way of communication between flash and External apps, the parameter loading javascript waits for the flash to complete loading the movie first.

Do write in the comments if you like it and/or need the AS3 source code.

Thursday, January 28, 2010

Understanding finer issues of Scrumming

Out of experiences, we gather that using small control points we can move the earth. In scrum discussions, we come across such control areas listings that need to be monitored and attended to. These control points, if adhered to, help manage the project flow with ease. If not religiously followed, can be the starting points of damage.

One such area is the "Defend Meeting" that surprisingly does not figure high in these lists. Why so important? Because when it comes to planning, 50% of us are below average.

The Defend Meeting is a team session that is conducted after the initial planning is done by the inidividual members. Here the members "defend" their plans towards a team concensus. This session gives one more chance to rethink the plan and helps each member to see the COMPLETE picture, hence reach better team estimates. It is often seen that it brings out a lot of missing links that have not been thought during the planning. The Defend Meeting helps the team fight out their insecurities (especially the younger ones) and iron out the "coordination critical paths".

Another mantra that I learnt from an associate team, Mike Marcus and Don A Hampton, is "Expose inefficiency". The team starts to unearth flaws early in the game. In a truely fluid agile environment, rather than promoting wrong competition (as we would think), this team behaviour actually binds them together and works out better. If the team members are in seperate locations or multiple teams are coordinating or team size is big, this princicle is a must. (Ideal size is still 6 though). This principle could be promoted as one of the team policies and reminded as often needed. Do not shy away from marking the lapses or failures in the Sprint Review document.