A week ago had a personal task to convert 40 or so word documents to pdf in standard and minimum size. Doing them by one at a time was felt such a waste, so recorded and edited a vba macro to do the same task in a jiffy.
Here’s the simple vba subroutine to do the conversion.
Excel’s inbuilt functions are powerful. They can achieve a lot. Almost anything can be conquered with these functions.
Recently needed a function to know which row is being modified. As far as i know, there’s no way this can be achieved by using excel’s function. This is where little bit of vba helps.
Here’s the code that helped.
Private Sub Worksheet_Change(ByVal Target As Range)
If (Target.Column = 3 Or Target.Column = 4) Then
If Target.Row > 1 Then Cells(1, 6) = Target.Row
In the above sheet change method, the code checks if any row has changed in the columns 3 and 4 and if something has changed beyond row 1 in these columns, the F1 cell is updated with the row number.
Simple and small code but very useful when one wants to know which row is getting modified in excel.
The information about the row being modified can be used to do lot of things using excel in built functions.
Now think where can one use this. Stay tuned I have a follow up post on the use.
A post on Charles Darwin by a friend reminded me an approach I am experimenting in terms of cleaning my stock portfolio.
As I have discussed here, I am a Gardener now when it comes to investing. I find selecting and buying lot easier than selling.
For buying I have a system or at least a framework but selling lacks any systematic approach. So while reading the book Seeking Wisdom from Darwin to Charlie Munger by Peter Bevelin,I thought of taking Darwin’s help.
Now every quarter and during annual results, I assess the stocks in my portfolio using 4 variables, ROE, Cash by Net Profit, Debt to Equity and growth of each additional rupee invested.
Gather this data into this Excel sheet and then rank them. Consistent lower ranks are the ones that need to go. So Darwin’s survival of the fittest in action. I also use this sheet to evaluate a potential new stock for the portfolio.
Getting this sheet populated is simple, hard part is acting on the information it reveals. .
Here’s the excel sheet if anyone is interested.
I am handing few big excel data files lately and these files are loaded with vlookups, sumifs,sumproduct and truckload of loopy VBA code.
Running the VBA code was pain, excel drags as if a dinosaur is sitting on it. The files were so muddled that even the excel in-built functions to convert a range of cells to number takes 5 mins.
Trying to optimise the vba application in the excel file, I did the usual Application.ScreenUpdating = False and Application.Calculation = xlManual in the VBA to speed up all macros. This helped but not entirely.
The key to increasing speed of calculation and improving performance of your excel worksheet and VBA code lies in understanding how Excel calculates and re-calculates each cell and resolves dependencies. Excel recalculate’s whenever an event occurs (for example a cell is changed, a VBA code calls for re calculation or anything else). For each change the entire calculation process is carried out, so the trick to gain major benefit came from looking at all the formulas and optimising them.
It’s not easy but there are many fantastic resources that can help. Here are some of my favourite ones.
75 Excel Speeding Tips
Its a long List, many repeated but worth a visit.
Excellent tips to optimize vba code. You should do this before you move onto anything.
How to speed up calculation and Improve performance of Excel and VBA
These are excellent tips and must visit to know more about the calculations steps followed by Excel.
Creating pareto chart in excel requires multiple steps as there is no direct method to accomplish this in excel. Here’s a vba a script that creates a pareto chart in excel.
How to use the script?
Select the data and run the ParetoChart sub. That’s it. The scripts always assumes the first row is headers. Don’t forget that.
And if you need Boxplot in excel, this is where you should go.
Here’s the code….
Working on a side project in VBA, a few weeks ago, I needed a function to get the userid of the person currently logged in to the system. Here’s the function that came handy! Posting it here for the general audience!
Option Compare Text
Private Declare Function GetUserName Lib "advapi32.dll" _
Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long</code>
Public Function NTDomainUserName() As String
' This function gets the user id of the person currently logged in
Dim strBuffer As String * 255
Dim lngBufferLength As Long
Dim lngRet As Long
Dim strTemp As String
lngBufferLength = 255
strBuffer = String(255, 0)
lngRet = GetUserName(strBuffer, lngBufferLength)
strTemp = UCase(Left(strBuffer, lngBufferLength - 1))
If strTemp = "" Then
NTDomainUserName = "Not Known"
NTDomainUserName = strTemp
' Test function
UserId = NTDomainUserName
Sheets(1).Cells(1, 1) = UserId
While browsing through google play, this android app got my attention. Having discarded my android phone, I asked my friend N to download the app on his mobile.
Liked the game and then we two went for a walk to stretch our legs and give our eyes break from the computer screen.
It is in this walk that we challenged each other if we can create this simple game in our programming language of choice.
We figured by an hour we can complete the game. I wanted to do it in java but didn’t have the sdk installed on my system, so decided to use my old pal vba. Launched excel and began with my phone timer set to shout in an hour.
By stretching 5 mins more to an hour, I had the basic functionality ready in excel.
Then it took, another one hour to get this game little more polished. Though far from complete.
So here’s the result. Click to download the excel file and play
What I learnt from the exercise?
The application.ontime function in excel 2007 is broken. Was not able to stop it. So had to introduce a new Boolean variable to stop the play.
Working with shapes was a new thing for me. I have always used cells or a user form for working in excel VBA, but for this I wanted to see how to use shapes.
Not bad for a total of 2+ hours of effort and it refreshed my dormant vba skills!!
Lot can be improved in the app, but hey I have real work to do. If you want, you are free to refine the game further. or look at the code here.