You are on page 1of 4

UNIONS,INTERSECTIONS, FONT,BACKGROUND COLOUR IN VBA

Private Sub range_union_Click()


MsgBox "union of 2 ranges:"
Union(range("A1:B7"), range("E1:F7")).Select
MsgBox "union of 3 ranges:"
Union(range("A1:B7"), range("E1:F2"), range("A12:B14")).Select
MsgBox "now interestion of two ranges"
Intersect(range("A1:B5"), range("B1:C5")).Select
MsgBox "good bye"
End Sub

Private Sub range_selection_click()


Dim rng As range, cell As range
Set rng = range("C4:D10")
Set cell = Selection
If cell.count = 1 And IsNumeric(cell) And Not IsEmpty(cell) And Not
Intersect(rng, cell) Is Nothing Then
MsgBox "You win"
Else
MsgBox "You lose"
End If
End Sub

'Lets modify the code a bit,easing the condition of having count=1,and


instead we put count<=4'
Private Sub range_selection2_click()
Dim rng As range, cell As range
Set rng = range("C4:D10")
Set cell = Selection
If cell.count <= 4 And IsNumeric(cell) And Not IsEmpty(cell) And Not
Intersect(rng, cell) Is Nothing Then
MsgBox "You win"
Else
MsgBox "You lose"
End If
MsgBox cell.count ' A range cannot be ever numeric'
MsgBox IsNumeric(cell)
End Sub

'Football matches'
Private Sub football_match_click()
Dim rng As range, matchname As String, counter As Integer, i As Integer, j As
Integer
Set rng = range("A1").CurrentRegion
counter = 0
Worksheets(1).Columns(2) = "" 'We empty column B(or2)
For i = 1 To rng.count
For j = i + 1 To rng.count
matchname = rng.Cells(i).Value & " vs " & rng.Cells(j).Value
Cells(counter + 1, 2).Value = matchname
counter = counter + 1
Next j
Next i
ActiveSheet.Columns(2).PrintPreview
End Sub
'For example if i Write Little House in the woods on the 16th line we will
set up now a font color for each cell
Private Sub fontsetup_click()
range("A16").Font.Color = vbRed
range("B16").Font.Color = RGB(0, 255, 0)
range("C16").Font.Bold = True
range("D16").Font.Italic = True
range("E16").Font.Underline = True
End Sub

'Background_color and fill sub routine now'


Private Sub bkg_click()
range("A1").Interior.Color = RGB(255, 0, 0)
range("A2").Interior.Color = vbRed
range("B1:B2").Interior.Color = vbGreen
range("B3").Interior.ColorIndex = 0
'range(Cells(1, 3)).Interior.ColorIndex = 37 is not working,range(cells(i,j))
is not defined
End Sub

Private Sub range2()


Dim rangeToUse As range
Set rangeToUse = range("B2:C3,C4:E5")
MsgBox rangeToUse.Areas.count & vbNewLine & rangeToUse.Areas(1).count &
vbNewLine & rangeToUse.Areas(2).count 'if is typed only vb the results are
displayed consecutive
'if vbNewLine is displayed the results are displayed each on every line
End Sub
Private Sub range3()
Dim rangeToUse As range
Set rangeToUse = range("B2:C3,C4:E5,F5:G7,H8:J10")
For Each singleArea In rangeToUse.Areas
MsgBox singleArea.count & vbNewLine 'vbnewline is unnecessary
Next singleArea
End Sub

Private Sub selection_of_ranges_click()


Dim rangeToUse As range, singleArea As range, cell1 As range, cell2 As range,
i As Integer, j As Integer
Set rangeToUse = Selection
Cells.Interior.ColorIndex = 0
Cells.Borders.LineStyle = xlNone
If Selection.Areas.count <= 1 Then
MsgBox "Please select more than one area. "
Else
rangeToUse.Interior.ColorIndex = 38
For Each singleArea In rangeToUse.Areas
singleArea.BorderAround ColorIndex:=1, Weight:=xlThin
Next singleArea
For i = 1 To rangeToUse.Areas.count
For j = i + 1 To rangeToUse.Areas.count
For Each cell1 In rangeToUse.Areas(i)
For Each cell2 In rangeToUse.Areas(j)
If cell1.Value = cell2.Value Then
cell1.Interior.ColorIndex = 0
cell2.Interior.ColorIndex = 0
End If
Next cell2
Next cell1
Next j
Next i
End If

End Sub

Private Sub sum1_click()


Static x As Integer
x = x + 1
MsgBox x
End Sub

'Create a command button that reads the elements of a column, preferably


numbers,which register the scores in a toefl test and then
'on the next one it writes passed/failed if the score is >=80 or below
respectively.

Private Sub pass_or_fail_click()


Dim rng As range, cell As range, result As String
Set rng = range("O1").CurrentRegion
For Each cell In rng
If cell.Value >= 80 Then
cell.Offset(0, 1).Value = "passed"
Else
cell.Offset(0, 1).Value = "failed"
End If
Next cell
End Sub

'Remove duplicates inside a column'


Private Sub remove_duplicates()
Dim toAdd As Boolean, unique As Integer, i As Integer, j As Integer
Cells(1, 2).Value = Cells(1, 1).Value
unique = 1
toAdd = True
For i = 2 To 10
For j = 1 To uniqueNumbers
If Cells(i, 1).Value = Cells(j, 2).Value Then
toAdd = False
End If
Next j
If toAdd = True Then
Cells(uniqueNumbers + 1, 2).Value = Cells(i, 1).Value
uniqueNumbers = uniqueNumbers + 1
End If
toAdd = True
Next i
End Sub

'Test-low difficulty level'


'1.Activate a worksheet,a column,a specific set of rows, each one at the
time,and then select each part.
Private Sub activated_click()
MsgBox "this is the first step"
Worksheets(2).Activate
Worksheets(2).Cells.Select
MsgBox "this is the second step"
Columns(4).Select
MsgBox "this is the third step"
Rows("1:3").Select
MsgBox "good bye"
End Sub

'2.Activate a cell of your choice, give a value, make the same to each cell
underneath,select the region and the numbers from the
'region

Private Sub activated2_click()

Dim i As Integer, j As Integer, cell As range, rng As range, total As Integer


i = InputBox("enter the row you want")
j = InputBox("enter the column you want")
Worksheets(2).Activate
Cells(i, j).Value = 3
For k = 1 To 5
Worksheets(2).Cells(i + k, j).Select
Cells(i + k, j).Value = 10 - k
Next k
rng = range(Selection.Offset(-k, 0)).CurrentRegion
For Each cell In rng
total = total + cell.Value
Next cell
MsgBox "good bye"
End Sub
'3.Make bold and italic a selection,an active cell
Private Sub button1_click()
Worksheets(2).Activate
With ActiveCell.Font
.Bold = True
.Italic = True
End With
End Sub

You might also like