0% found this document useful (0 votes)
7 views8 pages

Progs

Excel and VBA codes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

Progs

Excel and VBA codes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

' to know cell type

Sub clear()
Worksheets(1).Cells.clear

End Sub

Function cellType(num As Variant)

If IsNumeric(num) Then
MsgBox "Number"
Else
MsgBox "Text"
End If

End Function

' to play animation

Sub animation()

Dim i As Integer
For i = 1 To 1000
Range("a1").Delete
Next i

End Sub

' set values in range


' objective: to learn assigning values to a data type called
"range"
Sub setRange()
Range("a1:a10").Value = WorksheetFunction.RandBetween(10, 100)
End Sub

' arithmetic calculations


' objective: to lear arithmetic operators together with assignment
operators

Function arithmetic(a As Variant, b As Variant, s As String)

' this is local variable


Dim c As Variant

If s = "add" Then
c = a + b
arithmetic = c

ElseIf s = "sub" Then


c = a - b
arithmetic = c

ElseIf s = "mul" Then


c = a * b
arithmetic = c

ElseIf s = "div" Then


c = a / b
arithmetic = c
End If

End Function
' Ex - 3: title: arithmetic mean
' objective: to write a program to compute arithmetic mean

' statements: range, worksheetfunction, average, round

'program

Sub arithMean()

Dim sum As Variant


Dim r As Range
Dim c As Variant

Dim avg As Variant

Set r = Range("a1:a10")

For Each c In r
sum = sum + c.Value
Next c

avg = sum / 10

MsgBox avg

End Sub

' Ex - 4: finding sum of the range using arrays and loops


' objective: to know as how to use loops to process arrays
' statements: For loop
' program

Sub arraySum()

Dim arr() As Variant


Dim i As Integer
Dim sum As Integer

ReDim arr(10) As Variant

sum = 0

For i = 1 To 10
arr(i) = i
sum = arr(i) + sum
Next i

MsgBox sum

End Sub

' Ex - 5: confidence interval


' objective: to compute confidence intervals using arithmetic mean
and z-scores
' Statements: worksheetfunctions[average, stdev, round], msgbox
' program

Sub confInt()

Dim myMean As Variant


Dim myStd As Variant
Dim UL As Variant
Dim LL As Variant

Set r = Range("a1:a10")

myMean = WorksheetFunction.Average(r)
myStd = WorksheetFunction.StDev(r)

UL = WorksheetFunction.Round((myMean + (1.96 * myStd)), 2)


LL = WorksheetFunction.Round((myMean - (1.96 * myStd)), 2)

MsgBox UL & " and " & LL

End Sub

' Ex: 6: title: calculate GM


' Objective: to know as how to use worksheet functions to
calculate means
' Statements: worksheetFunction[product, count], range, filldown

Function geomMean(rng As Range) As Double

Dim sp As Double
Dim n As Integer

sp = WorksheetFunction.Product(rng)
n = WorksheetFunction.Count(rng)

GeoMean = WorksheetFunction.Power(sp, 1 / n)

MsgBox sp & " " & n

End Function
' Ex: 6: title: calculate HM
' Objective: to know as how to use worksheet functions to
calculate means
' Statements: worksheetFunction[product, count], range, filldown

Sub harMean()

Dim lr As Integer

lr = Cells(Rows.Count, "A").End(xlUp).Row

Range("b2").Formula = "=1/a2"
Range("b2:b" & lr).FillDown

Range("b" & lr).Offset(1, 0).Formula = "=sum(b2:b10)"


Range("b" & lr).Offset(1, -1).Value = "Har-mean"
Range("b" & lr).Offset(1, 0).Formula = "=b10/10"

End Sub

' sum of the array elements

Sub arraySum()

Dim arr(10) As Variant


Dim i As Integer
Dim sum As Variant

sum = 0

For i = 1 To 10
arr(i) = i
sum = sum + arr(i)

Next i

MsgBox sum

End Sub

Sub harMean()

Dim lr As Integer
Dim sumres As Variant

lr = Cells(Rows.Count, "A").End(xlUp).Row

Range("b2").Formula = "=1/a2"
Range("b2:b" & lr).FillDown

Range("b" & lr).Formula = "=sum(b2:b9)"

sumres = Range("b" & lr).Value

MsgBox lr / sumres

End Sub

Sub harMean()

Dim lr As Integer
Dim sumres As Variant

lr = Cells(Rows.Count, "A").End(xlUp).Row

Range("b2").Formula = "=1/a2"
Range("b2:b" & lr).FillDown

lr = lr + 1

Range("b" & lr).Formula = "=sum(b2:b9)"

sumres = Range("b" & lr).Value

MsgBox lr / sumres

End Sub

Sub summaryCh()

Cells("a11").Value = WorksheetFunction.Average("a2:a10")
Cells("a12").Value = WorksheetFunction.Min("a2:a10")
Cells("a13").Value = WorksheetFunction.Max("a3:a10")

End Sub

You might also like