Writing an Excel Macro

excel_marco

Writing an Excel Macro

Recently I got another project , where I had to create some cool Excel Macros for doing some data churning stored in excel. This was the first time I was writing a Macro so I decided to go after it with full valour. Excel Macros are written using Visual Basic which has been one of my favourite languages from school days(I did my first commercial project using VB 6.0 and I was paid handsomely for it 🙂 ).I have posted a tutorial on whatever I learnt.

Introduction –

Visual Basic for Applications (or VBA) is a variation on the Visual Basic language which extends the basic syntax to include specific object trees used to describe Microsoft Office application components. These include documents, worksheets, and other containers, as well as some useful built-in functions for manipulating them.

Like VB, VBA is based on the BASIC language, and so should be familiar. Unlike VB, it is interpreted-only, when used in the context of Microsoft Office, and as such might be significantly slower than it’s compiled cousin. It is generally only used to enhance an application document, or connect across Microsoft Office applications.

Task Accomplished

I had some data in an Excel Sheet , I had to do three tasks.

  • Write a module to Load some data(User Data) into a new row in excel at intervals of 30 rows.
  • Delete all those rows where the column-“userdata” was empty.
  • Copy data in intervals from one excel sheet to another sheet.

I will post the code which I had written, and explain what all I did, hope this you helps in writing your own excel macros.

–If you want to programatically add values to some rows in a sheet use this code.

Sub UserLoad_Calc()
''declare userLoad
Dim UserLoad As Long
Dim StepLoad As Long
Dim counter As Long

StepLoad = 50
UserLoad = 50
”The for Loop to loop through all the rows with steps of 30
For counter = 2 To Worksheets(“UserLoadData”).rows.Count Step 30

If UserLoad < 4000 Then
Worksheets(“UserLoadData”).Cells(counter, 9) = UserLoad
UserLoad = UserLoad + StepLoad
Else
Worksheets(“UserLoadData”).Cells(counter, 9) = UserLoad
End If
Next counter
End Sub

–If You want to delete those rows if a particular cell is empty use this code. In my case column 9 of each cell is empty.


Sub deleteRows()

Dim rows As Long
Dim rng As Range
Set rng = ActiveSheet.UsedRange.rows
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False ” To increase the performance since this is a high CPU activity.

Debug.Print rng.rows.Count
For i = rng.rows.Count To 2 Step -1
”find the row whose cell number 9 is empty.
If Application.WorksheetFunction.CountA(rng.Cells(i, 9)) = 0 Then
rng.rows(i).EntireRow.Delete
ActiveSheet.UsedRange
End If
Next i
”setting back to normal
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub

–If you want to programatically fill up rows/columns of one sheet from filtered data from some other sheet, use this code.

Sub LoadNewData()
Dim i As Long
i = 2

”The for Loop to loop through all the rows with steps of 30
For counter = 2 To Worksheets(“Data”).rows.Count Step 30

”fill request execution time
Worksheets(“UserLoadData”).Cells(i, 10) = Worksheets(“Data”).Cells(counter, 17)

”fill request wait time
Worksheets(“UserLoadData”).Cells(i, 13) = Worksheets(“Data”).Cells(counter, 18)

”fill requests queued
Worksheets(“UserLoadData”).Cells(i, 14) = Worksheets(“Data”).Cells(counter, 20)
i = i + 1
Next counter
End Sub

Do drop me a line if you have any questions regarding automating your excel tasks or creating macros using VBA.