Monday, September 9, 2013

Eight Bug Reduction Tips - VBA

You cannot completely eliminate bugs in your programs, but there are a few tips that will help you keep them to a minimum
Use an Option Explicit at the beginning of your module - Doing so will require that you define the data type for every variable that you use. It's a bit more work, but you'll avoid the common error of misspelling a variable name.. And there's a nice side benefit: Your routines will often run faster.
Format your code with indentation - Using indentation to delineate code segments is quite helpful. If you have several nested For...Next loops, for example, consistent indentation will make it much easier to keep track of them all.
Be careful with On Error Resume Next - This statement causes Excel to ignore any errors and continue. In some cases, using this statement will cause Excel to ignore errors that shouldn't be ignored. Your may have bugs and not even realize it.
Use lots of comments - Nothing is more frustrating than revisiting code that you wrote six months ago - and not having a clue as to how it works. Adding a few comments to describe your logic can save you lots of time down the road.
Keep your subroutines and functions simple - Writing your code in smaller modules, each of which has a single, well-defined purpose, makes it much easier to debug them.
Use the macro recorder to help you identify properties and methods - If I can't remember the name or syntax of a property or method, it's often quicker to simply record a macro and look at the recorded code.
Consider a different approach - If you're having trouble getting a particular routine to work correctly, you might want to scrap the idea and try something completely different. In most cases, Excel offers several alternative methods of accomplishing the same thing.

Understand Excel's debugger - Although it can be a bit daunting at first, you'll find that Excel's debugger is an excellent tool. Invest some time and get to know it.

Finding cells by format

We often search cells by values or contents in the cells by using find functionality in Excel. But do you know we can search a cell by format.
Let’s see how?  Assume I have a big client database in Excel and I have highlighted a customer number with different cell color and with different font format (Bold and Italic). now from this database if I have to find this different customer, I will use Excel find functionality with finding format instead of the content of the cell.

To find the customer number, by format press Ctrl+F to get Find and Replace pop-up. click on the options button, to get advanced find options.

When you click on the Options buttons, the Find and Replace pop-up gets expanded and shows all advanced options. 







Click on Format button, and give the kind of format you want to search in the range. 


  

Excel shows the preview of the format you are going to search in the range. Press Find Next or Find All and Excel finds all the cells with the same format you have given in the condition.





Similarly you can give colors in the filter criteria and filter the range by color.

Important: Clear the format once you are done with Find operation as Excel may retain the format and search the same format every time you are using Find functionality. 


Sunday, September 1, 2013

Wildcard Characters in Excel

We will discuss wildcard characters in this blog. People who have worked on any SQL, must be knowing about the wildcard characters and their uses. People who have not worked on SQL here is a brief about the wildcard characters we have in Excel.
Wildcard characters * (asterisk), ? (question mark), ~(tilde) followed by a * or ? or ~.

Usage of asterisk (*) in Excel:

 Asterisk is used when we want to search a string which starts/ends with a specific word or pattern.
Asterisk is also used to search a pattern if the pattern is appearing in middle of the word.
Example and usage of  Asterisk:
<!--[if !vml]--><!--[endif]-->









We have a few string values in a range and we are passing few conditions with the use of wildcard characters:
"*ex" in the filter condition
"*ex*" in the filter condition
"ex*"  in the filter condition
          













Question mark (?) can be used when you want to search a string with one variable character. For example: you have strings ABC and ACC, if you give “A?C” in filter condition, excel will show both the strings.
So where do we use wildcard character tilde (~)?
The answer is when you want to search a wildcard character in the sheet, you have to use tilde before the character to search it. Let’s take the same example we have used above. I have appended an asterisk to at the end of the list.

Now if I want to filter only the character * so that filter returns the last row from the list. If I write * in the filter condition, the filter will display the entire list instead of just the last row. So what do I do to get just the last row?
I need to use the tilde wildcard character. To search a wildcard character from the list/sheet, write tilde just before character in the filter condition:

<!--[if !vml]--><!--[endif]-->
You will find the required result.

<!--[if !vml]--><!--[endif]-->
Important
Wildcard characters can be used in Find functionality and formulas in Excel.
To know more about usage of wildcard characters in the formula and Find functionality contact me on : support@excelvba.org

Saturday, August 24, 2013

Absolute vs Relative cell refrencing

Did you ever drag a formula and it behaved dramatically. Did you ever faced a problem as mention below:

In the above picture I have tried calculating the Price of the products in the year 2014 by using Price of Product 1 in 2013 (cell G8) and Price change factor (cell H4). The formula in the first cell is :

G8+(G8*H4)

When I drag the formula for Product 2 to Product 5, it shows a very abnormal result. The reason why formula is showing a wrong result is explained in the below picture:

 The formula is taking cell H7 instead of H4, which is Price Change Factor to calculate the Prices in 2014.
Why did the reference change? Answer to this question is we have not fixed the cell H4 in the formula or in other words we did not make absolute reference to the cell H4. The referencing used in this formula is Relative, which changes with every increment/decrements of row and column.What should I do to make it absolute/fixed? You just need to append $ (dollar sign) to the range and make it $H$4 instead of H4. With these dollar signs, your formula looks something like this.
Shortcut to add these dollar signs is F4. Write the formula in the first cell :G8+(G8*H4). Go in formula editing mode by pressing F2 on the formula cell, select the range you want to make absolute and press the button F4, drag the formula in below rows, for all products. Here in this example you really don't need to fix column since your dragging the formula in the rows. To fix the only the row you need to append dollar sign only before row number 4 and not before column index H. The formula looks something like this: G8+(G8*H$4). If in case you want to drag the formula in columns in the same row, you need to fix column by appending $ before H and not before 4 (your formula will be G8+(G8*$H4) ). Press F4 key repeatedly to add/remove dollar sings before row/column index.

Hope this is useful. Visit us on excelvba.org 

Excel keyboard shortcuts

Our day to day work involves excel and to complete our work fast we should have adequate knowledge of keyboard shortcuts. I am going to give you simple but useful tip which will helps you to navigate excel and perform tasks much quicker without touching the mouse.
Use arrow keys in combination of Ctrl/Shift key to traverse your Excel sheet.
·        Ctrl + Down arrow – Data Region Dow
·        Ctrl + Up arrow – Data Region Up
·        Ctrl + Right arrow – Data Region Right
·        Ctrl + Left arrow – Data Region Right

Use Shift key with the above combination to select the data e.g. Ctrl+Shift+Down arrow to select the data range from current cell to last filled cell of the data region.
Some more useful shortcuts:
  • Ctrl+ Space Bar – Selects the entire column
  • Shift+ Space Bar – Selects the entire row
  • Ctrl + PageUp – Previous sheet
  • Ctrl + PageDown – Next sheet
  • Ctrl + Home – Selects cell A1 of the active sheet
  • Ctrl + 1 – Opens Format Cell window
  • Ctrl+Shift+1 – Changes the cell format to Number
  • Ctrl+Shift+2 – Changes the cell format to Time
  • Ctrl+Shift+3 – Changes the cell format to Date
  • Ctrl+Shift+4 – Changes the cell format to Dollar
  • Ctrl+Shift+5 – Changes the cell format to Percentage
  • Ctrl+Shift+6 – Changes the cell format to Scientific
And finally Ctrl + Z to undo what ever mess you have done the excel sheet while practicing these shortcuts.

Follow me on excelvba.org