You are on page 1of 13

Visual Basic: Functions

on ranges
Examples
Application volatile
'Volatile functions'
Function MyFunc()
Application.Volatile
Dim cell As Range: Sheet1.Activate
Set cell = Range("A1")
MyFunc = cell.Value + cell.Offset(1, 0).Value
End Function
Range of cells with the same value
'create a range of cells with the same elements
Sub range1()
Range("H1:H4,I2:J5") = 10
Dim sum As Integer
sum = WorksheetFunction.sum(100, 200, 300): MsgBox sum
Dim sum2 As Integer, cell As Range
Set cell = Range("H1:H4,I2:I5") 'you must set before cell,otherwise
cell=Range("H1:H4,I2:I5")
sum2 = WorksheetFunction.sum(cell)
MsgBox sum2
End Sub
Copy/Pastea)
'Copy/paste exercises:
'a)copy first 3 elements from column A to column C rows
4,5,6
Sub range2()
Range("A1:A3").Select
Selection.Copy
Range("C4").Select
ActiveSheet.Paste
End Sub
Copy/Pasteb)
'b)copy first 2 elements from column A to column B ,row
3.
Sub range3()
Range("A1:A2").Select
Selection.Copy
Range("B3").Select
ActiveSheet.Paste
End Sub
Copy/Paste c)
'c)copy first 2 elements from column A to column B,row 3,sheet2
Sub range4()
Worksheets(1).Activate
Range("A1:A2").Select
Selection.Copy
Worksheets(2).Activate
Range("B3").Select
ActiveSheet.Paste
End Sub
Copy/Paste d)
'd)
Sub range5()
Worksheets(1).Activate
Range("A1:A2").Select
Selection.Copy
Worksheets(3).Activate 'if sheet3 is not added yet it shows subscript out of range
Range("B3").Select
ActiveSheet.Paste
Range("A1:M100").ClearContents 'clears sheet 3
Sheet1.Range("A1:M100").ClearContents 'clears sheet1
End Sub
Current regions
'Current regions -- Suppose in sheet2 we have some
values in A1:A2,B3:B4
Sub range6()
Set Rng = Range("A1").CurrentRegion
Maximum = WorksheetFunction.Max(Rng)
MsgBox Maximum
Rng.Select
End Sub
Ranges
Sub range7()
Range(ActiveCell, ActiveCell.End(xlDown)).Select
'starting from active cell to the end are selected
sum = WorksheetFunction.sum(Range(ActiveCell,
ActiveCell.End(xlDown)))
MsgBox sum
End Sub
Rangescontd
Sub sum_range()
MsgBox WorksheetFunction.sum(Range(ActiveCell, ActiveCell.End(xlDown)))
End Sub

Sub sum_range2()
MsgBox WorksheetFunction.sum(Range(ActiveCell, ActiveCell.End(xlToRight)))
End Sub

Sub sum_range3()
MsgBox WorksheetFunction.sum(Range(ActiveCell, ActiveCell.End(xlToLeft)))
End Sub

Sub sum_range4()
MsgBox WorksheetFunction.sum(Range(ActiveCell, ActiveCell.End(xlUp)))
End Sub
Intersections
Sub intersection1()
MsgBox Intersect(Range("B2:C7"), Range("C6:F8")).Count
Intersect(Range("B2:C7"), Range("C6:F8")).Select
End Sub
Sub intersection2()
Intersect(Range("B2:D7"), Range("C4:F8")).Select
sum = WorksheetFunction.sum(Intersect(Range("C4:E7"),
Range("D4:F6"))) 'we use the sum function from excel to sum the elements
MsgBox sum
End Sub
Exercise: count intersection range
Private Sub intersection2_Click()
Sheet2.Activate
Set cell = Selection
If Not IsEmpty(cell) Then MsgBox "You win" Else MsgBox "You lose": End If
End Sub
'Exercise'
'If the intersection between a selection and a specific range of cells has more than 4 cells display hello'
Private Sub intersection_Click()
Set cell = Selection
If Intersect(Selection, Range("A1:E5")).Count >= 4 Then
MsgBox "hello"
Else
MsgBox "Good bye"
End If
End Sub
Empty range,intersection empty
'Display Hi! if the intersection between 2 ranges is not disjoint and good
bye if it is disjoint'
Private Sub intersection3_Click()
Set cell = Range("A1:B5"): Set cell2 = Selection
If Intersect(cell, cell2) Is Nothing Then
MsgBox "good bye"
Else
MsgBox "Hi!"
End If
End Sub

You might also like