Word to PDF

word to pdf
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.

Continue reading

Simple Code to Find Which Excel Row is Being Modified

Excel’s inbuilt functions are powerful. They can achieve a lot. Almost anything can be conquered with these functions.

Well almost……everything!!

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
    End If
End Sub

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.

Is There a Dinosaur Sitting on Your Excel File?

Dinosaur Sitting On your excel App
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.

Optimize VBA
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.

Pareto Chart in Excel

Pareto Chart in 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….

Continue reading

VBA Script to Add Auto Captions to Images In a Word document

Has this ever happened to you? You are writing a big image heavy test report or user document in Microsoft word. And as you get to the end of your job, you suddenly realise you forgot to caption your images in the document.

You get angry on yourself at the prospect of spending an extra hour to correct this oversight.

Well a similar thing happened recently. And when I thought about it, it had happened more than I want to reveal here. Since writing report isn’t a daily activity of my job, this thing reoccurs. After a hundred and odd page, I go back and add caption to the pictures that I forgot in the first place.

So this time, instead of fixing the captions, I spent the next hour experimenting, how to fix the mess and came up with the following VBA script to add auto captions to images in a word document.

Simple script. Posting it here in case if someone is in similar situation.

Sub CaptionPictures()
Dim objPic As InlineShape

For Each objPic In ActiveDocument.InlineShapes
objPic.Select
Selection.InsertCaption Label:="Figure", TitleAutoText:="", Title:="", _
Position:=wdCaptionPositionBelow, ExcludeLabel:=0
Next objPic
End Sub

Getting User Id of The Person Currently LoggedIn [vba]

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"
Else
NTDomainUserName = strTemp
End If

End Function


' Test function

Sub test_Open()
UserId = NTDomainUserName
Sheets(1).Cells(1, 1) = UserId
End Sub

Plants Become Weeds when they…..

FortranDLL in visual studio

How to Create Fortran DLL in Visual Studio? – The simple way


Plants become weeds when they obstruct our plans or our tidy maps of the world. Fortran is also perceived in the same way. Although still a formidable programming language, many now treat it as weeds.

They encounter it and they go back to fortran 66 mindset. Fortran’s new capabilities are discounted and people look past the new features of the language to talk about the horrible “go to” mess!! They might want to look at this Myths Of Fortran post!

There’s so much legacy code in fortran, but no one wants to touch it. The easiest solution commonly used is to convert it into a dll or some library and use with new languages. This explains the popularity of the post how to create fortran dll in visual studio on this blog.

This brings me to today’s post. Here’s an even more convenient animated gif to explain the process of converting a fortran program to a dll in visual studio with intel fortran compiler!

An Excel App for Your Brain!

Screen Shot 2013-04-29 at 10.24.23 PM
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.

Fortran DLL and Excel -a 4 step video tutorial

Ever since I had used the screen-o-matic screen-cast, I was itching to use it again to create something.

Today morning finding some time, I decided to go ahead, but then stumbled on these awesome four step video tutorials on mixing fortran and excel.

This is what I was going to screen-cast, but this videos do an excellent job.

So please enjoy and learn.

Step 1

Step 2

Step 3

Step 4

A habit and International Standard Atmosphere

Now it’s a habit.
isa in vba international standard atmosphere
If you are into programming then you might be all too familiar with the first hello world program that we all start our learning with.

This was true for me till I got into aerospace, now my first program is always the program to calculate temperature at an altitude for international standard atmosphere.

This has become a habit. I do this for all the new programming languages that I learnt. Perl, python, VBA, vb, java, etc you name it and my first program in all this language was ISA.

The other day I was browsing my old desktop and found this long forgotten visual basic excel program that I wrote 6+ years back. It’s a small program that calculates and gives temperature and pressure at different altitude according to international standard atmosphere.

Download the excel file here. ISA is coded as UDF in excel.

Evolution of BoxPlots

Famous statistician John Tukey (http://en.wikipedia.org/wiki/John_Tukey) created the boxplot in 1970. Boxplot shows a distribution summary in a small amount of space. In this paper [PDF], Hadley Wickham and Lisa Stryjewski look back on boxplot and its evolution up to present.

Boxplots are particularly useful for comparing distributions across groups. I was introduced to them two years back and have been using them ever since.

I think Boxplots are perfect example of what John Tukey believed

Its far better to have an approximate answer to the right question, than the exact answer to the wrong question…

Oh by the way, excel 2003 donot have boxplot by default so I have created a excel VBA code to just produce boxplot in excel. Do check it out!

BoxPlot in Excel

Two years back around this time, while working with matlab, I came across the plot called boxplot.

Liked the idea of. Ever since I wanted to replicate that in one my fav language of choice, fortran.

Well that became part of my work.

While reasearhing boxplot on google, I came across this page. Loved the idea of creating boxplots in excel.

So coded this excel VBA macro that takes in some numbers and creates a boxplot for it.

The video below shows how this works.

Download the excel file and don’t forget to look under the hood.
or for those who don’t want to download, here’s the complete code.

Count 1 to 10 game for Kids in Excel

anmolCount1to10

In the April of last year, my then 2 years old daughter learnt  counting from 1 to 10. Just after a week when she first began saying one, two, three…. I recorded her voice.

Then I taught her to count her fingers, she enjoyed it so much that she always wanted me to play with her by counting her fingers… 

This gave an idea. And this download attached to this post is the result of it.

Its excel version of the game that I used to play with my daughter. The excel game has my daughter’s voice and her little fingers.

She loved this game and we had  ton of fun with it. But now she has moved on to other stuffs, so I thought I will post these files here so that anyone with kid around that age can use the excel file.

The code is in VBA. You need to enable macros and have your speaker on to enjoy the game.

Hope you all enjoy this as much as myself and Anmol did.

Instructions:

  1. Download and unzip the zip file.
  2. Click on the excel file
  3. Enable macro
  4. Use your mouse and start clicking on Anmol’s little fingers.

 

Smile

And yes, its unlocked so you can learn some cool sound playing in vba too

Cheers