Automatically add columns and formulae then fill-down in Excel
Some simple VBA code to add columns in Excel, insert specific formulae at the top then fill-down to the bottom of the sheet.
For those who work with log files, it may be necessary to routinely insert columns at a specific position, insert a formula and then fill-down to derive certain values. This can be automated in Excel using Visual Basic using four simple scripts.
To insert columns at a specific position in a worksheet, use the following VBA script.
Sub AddColumns() 'Inserts Two Columns at B and C Worksheets(1).Range("B:C").EntireColumn.Insert End Sub
In this example, two columns are inserted where Column B is located. Adjust the range as required.
If the new columns require headers in Row 1, then the following script can do that.
For instance, let’s say that we want the header in B1 to be called “Value 1” and the header in C1 to be called “Value 2”. The following script will do this:
Sub AddHeader() Worksheets(1).Range("B1").Formula = "Value 1" Worksheets(1).Range("C1").Formula = "Value 2" End Sub
After adding columns, you may want to insert a formula to perform a calculation based on data elsewhere in the sheet. Since cells B1 and C1 contain headers, we’ll add them to cells B2 and C2:
Sub AddFormula() 'Inserts specific formulae to cells B2 and C2 Dim Formulas(1 To 2) As Variant With ThisWorkbook.Worksheets("Transposed Data") Formulas(1) = "=SUM(E2+G2)" Formulas(2) = "=SUM($E$2+2)" .Range("B2:C2").Formula = Formulas 'Changes number format in Columns B and C to general .Range("B:C").NumberFormat = "General" End With End Sub
This code adds the formula
=SUM(E2+G2) to B2 and
=SUM($E$2+2) to C2. These should be changed to suit your circumstances. As many formulae can be added to the code as desired, so long as
Dim Formulas(1 To 2) As Variant,
Formulas( ) and
.Range is expanded to accommodate them.
In addition, this code will change the
NumberFormat to “General”. Again, this can be changed to whatever type is required. The number type really only becomes an issue of the contents of the cells on the left of the new columns have a different number format. For instance, if Column A had a series of dates and was therefore in Date format, Columns B and C would have been created in Date format too.
Once the automated formulae have been added to the top of the column, you may want them to automatically fill down.
The following code will identify how many rows contain data in the worksheet and then fill-down from B2 to the last data-containing row in Column B.
Sub FillColumn() 'Fills column to last row of data from Cell B2 Dim LastRow As Long LastRow = Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row Range("B2:B" & LastRow).FillDown End Sub
The range can be adjusted to accommodate more columns. In our previous example, we added formulae to the first rows in Columns B and C, so we need to change the code from
Range("B2:B" & LastRow).FillDown to
Range("B2:C" & LastRow).FillDown.
By using this series of scripts, your worksheet should now automatically:
- Insert columns
- Add column headers
- Add formulae to the first row
- Fill down to the last row
Don’t forget that the easiest way to manage this is to link all of the scripts together:
Sub DoEverything() AddColumns AddHeader AddFormula FillColumn End Sub