Looping is used to repeatedly running a set of codes till it completes given condition. The loops are the fundamental component of any programming Language. In Excel VBA too there are many loop constructs. Let us have a close look into basic loop constructs.
Basic Loop constructs of Excel VBA:
For…..next
Do……While
Do……Until
While……….Loop
For Each……Next
For……Next Loop:
This loop is used when there is a need to execute a statement or a block of statements for certain number of times.
Example for For…..Next Loop:
For i =1 to 10
Total=Total+i
Next i
This code will calculate the sum of numbers from 1 to 10. In this example statement ‘Total=Total+i’ repeats 10 times. ‘i’ and ‘Total’ are the variable used in the code.
In this example for For….Next loop following steps took place:
This loop will work as long as the value of ‘i’ is more than 1 and less than 10.
The For loop initializes the value of ‘i’ as 1. As 1 is less than, 10 the next statement executed.
The code ‘Total=Total+i’ is executed for first time.
The code ‘Next’ increments the value of ‘i’ by 1
The control shifts to the begining of the loop, where the value of the variable ‘i’ value is checked
Till value of ‘i’ is less than 10 step 2 and 3 are executed.
As soon as value of ‘i’ becomes more than 10 the loop terminates.
We sometime look for easy way to remove duplicate entries from a large table without any filters or complicated formulas. Starting Office 2007, Microsoft added a direct method to remove duplicates from a range. There is one shortfall in this method which I will describe later.
Use mouse or keyboard arrow keys with “Shift” button pressed to select the area which contains duplicate content.
Click on “Data” tab of excel and click “Remove duplicate” button in the multi function tool bar.
A dialog box pops up. In the dialog box define the columns of the area to be included in the comparison of individual rows. All cells of the two rows should thus not display the same content due to which the rows become duplicates of each other.
In the column field of dialog box, remove the check marks in front of the columns which needs to be ignored during the comparison and click ‘OK’.
The duplicates in the columns of excel has been removed.
Note: If all the columns are not included in the comparison(when there are obvious differences between individual row), Excel always first top most row out of the rows that have been identified as duplicate. This is needs attention when the previously excluded cells are required later.
This method short fall is that, you will not be able to see which cells has duplicates. If you want to know which cells having the duplicate values, go to “Conditional formating” and select “Highlight Cell Rules”. In that, choose “Duplicate Values”. In the pop up select color which you want to see for the duplicate values. Now, use a filter by color and select the color you have opted for duplicats. It will show all the cells with duplicate values. Once found, you can decide on which data need to be removed and which needs to be retained.
After we create a tables in Excel, we will realize that the columns could be better as rows and vice versa. So in this post we will have a look into excel tip which describes how to rotate tables or swapping columns and rows in Excel. This tip will work on Excel XP, Excel 2003, Excel 2007, Excel 2010. Swapping of columns and rows in excel using this method is very simple and it does not changes the data.
How to Swap Row and Column in Excel Sheet
Follow the steps given below, to perform swapping of rows and columns in a range.
Select the area whose column and rows needs to be swapped.
Right click the selected area and select “Copy” if you want to create a new table keeping the original or Select “Cut” if you want to entirely replace the Table.
Select the cell where the table should be placed.Ensure that area where the we want to insert the Table should be the same as original.
Right click on your mouse or keyboard shortcut, select “Paste Special”.
A pop up dialogue box appears. Select the option “Transpose” which is places bottom right corner.
Select “OK”. Done. Your Table rows and columns swapped.
If you are facing any issue for transposing the rows and columns then, please leave your issue in the comment section below.
In excel there are situations where we need to find last row of an sheet. This can achieved by defining a variable and assigning the last row value to the variable and using offset property to find the last row.
Method 1: Declaring the variable and assigning the last row to the variable. Eg: Dim FinRow ‘Finrow is variable which hold the value of last row. Finrow=Range(“A65536”).End(x1Up).Row Now Finrow variable contains the last row number in Column “A”
Now if you want to put total in the last row of Column “A” then use Range(“A”&Finrow+1).Value=”Total”
In excel adding a range with one condition can be done with conditional formula called sumif. The conditional sum worksheet function sumif adds the specified range based on the criteria. The sumif conditional sum accepts only one criteria. If you want to perform sum based on multiple criteria, then you need to use multi conditional sum formula sumifs.
Syntax of conditional sum sumif Worksheet Function
Syntax: =SUMIF(range, criteria, [sum_range]).
Range: The range of cells that you want evaluated by criteria. Cells in each range must be numbers or names, arrays, or references that contain numbers. Blank and text values are ignored. Criteria: The criteria in the form of a number, expression, a cell reference, text, or a function that defines which cells will be added. For example, criteria can be expressed as 32, “>32”, B5, 32, “32”, “apples”, or TODAY(). Important Note: Any text criteria or any criteria that includes logical or mathematical symbols must be enclosed in double quotation marks (“). If the criteria is numeric, double quotation marks are not required. sum_range:The actual cells to add, if you want to add cells other than those specified in the range argument. If the sum_range argument is omitted, Excel adds the cells that are specified in the range argument (the same cells to which the criteria is applied).
Note: If you dont define sumrange then excel adds the range based on criteria. sumrange is needed if you want add other than criteria range.
Examples for sumif worksheet function
Below are the example for conditional sum in excel.
So sumif excel formula to add only Apple is
=SUMIF(A2:A6,”Apple”,B2:B6)
If you are having multiple criteria for sum then you should use multi conditional sum refer to my previous post on multiconditional sum.
You can also use Wild card charectors with SUMIF function. In that way you can even extend the usage of SUMIF functions.