Using Visual Basic with EXCEL
Visual Basic ( VB ) is a programming language which allows one to write procedures ( programs ) that perform operations on the spreadsheet or perform calculations that return a value.
VB is an Object Oriented language, meaning that it is designed around objects, their properties, and what they do. VBE refers to Visual Basic for Excel and it has a specific library of objects that relate to EXCEL.
For example ActiveSheet is the object by which EXCEL refers to the sheet you are currently working on. ActiveSheet.Name is a property of the active sheet (ie it's name). ActiveSheet.ActiveCell.Font is the property of the font of the active cell in the active sheet
ActiveSheet.Range(c3).Calculate is an example of a method. It specifies that the contents of cell C3 on the active sheet be calculated.
In EXCEL you can see more of these examples by going to "tools" then to "macro" then to "visual basic". In Visual basic go to "view" and then "Object Browser".
VB Procedures that perform operations on the spreadsheet are called SUBs
VB Procedures that perform calculations and return values are called FUNCTIONs
Below is an example of a SUB;
This sub changes the sign of a cell if the adjacent cell is a number and is also negative.
Sub changesign()
' Declare variables
Dim RowNum As Integer, colnum As Integer, currcell As Range, nextcol As Integer, temp As Single
RowNum = ActiveCell.Row ' intialize row number
colnum = ActiveCell.Column ' intitialize col number
nextcol = colnum + 1
Set currcell = ActiveSheet.Cells(RowNum, colnum) ' go to first cell
Set nextcell = ActiveSheet.Cells(RowNum, nextcol) ' get adjacent cell
Do While currcell.Value <> "" ' do while cell not empty
If IsNumeric(currcell.Value) Then ' is cell a number
temp = nextcell.Value
If currcell.Value < 0 Then
nextcell.Value = -1 * temp
End If
End If
RowNum = RowNum + 1
Set currcell = ActiveSheet.Cells(RowNum, colnum)
Set nextcell = ActiveSheet.Cells(RowNum, nextcol) ' get adjacent cell
Loop
End Sub
Below is an example of a FUNCTION;
This function and its related functions calculates the distance between two points on the earth's surface.
Function deltnm(lat1, long1, lat2, long2)
' This function calculates distance between two points on the earth's surface
' Point 1 is defined by lat1 long1
' Point 2 is defined by lat2, long2
' convert degrees to radians
the = 0.01745329252 * lat1
ale = 0.01745329252 * long1
ths = 0.01745329252 * lat2
als = 0.01745329252 * long2
c = Sin(the)
ak = -Cos(the)
D = Sin(ale)
e = -Cos(ale)
a = ak * e
b = -ak * D
g = -c * e
h = c * D
cp = Sin(ths)
akp = -Cos(ths)
dp = Sin(als)
ep = -Cos(als)
ap = akp * ep
bp = -akp * dp
gp = -cp * ep
hp = cp * dp
C1 = a * ap + b * bp + c * cp
29 delt = acos(C1)
33 deltkm = 6371 * delt
deltnm = deltkm * 3281 / 6076
Exit Function
End Function
Function asin(x)
asin = Atn(x / Sqr(-x * x + 1))
End Function
Function acos(x)
acos = Atn(-x / Sqr(-x * x + 1)) + 1.5708
End Function
Function atan2(x, y)
atan2 = Atn(x / y)
End Function