Buttons, Ranges and Arrays - VBA
Buttons, Ranges and Arrays - VBA
'Arrays'
Sub array_string()
Dim films(1 To 5) As String
films(1) = "Lord of the Rings"
films(2) = "Speed"
films(3) = "Star Wars"
films(4) = "The Godfather"
films(5) = "Pulp Fiction"
MsgBox films(4)
End Sub
'Matrix 5x2'
Sub array_string2()
Dim films(1 To 5, 1 To 2) As String
Dim i As Integer, j As Integer
For i = 1 To 5
For j = 1 To 2
films(i, j) = Cells(i, j).Value
Next j
Next i
MsgBox films(4, 2)
End Sub
'Dynamic array'
Sub CommandButton3_click()
Dim numbers() As Integer, size As Integer, i As Integer 'here numbers is a
variable length array'
size = WorksheetFunction.CountA(Worksheets(2).Columns(4))
ReDim numbers(size)
For i = 1 To size
numbers(i) = Cells(i, 4).Value
Next i
MsgBox numbers(size)
Dim total As Integer
For i = 1 To size
total = total + Cells(i, 4).Value
Next i
MsgBox total
End Sub
'Ranges''--select a range'
Sub CommandButton4_click()
Dim example As Range
Set example = Range("A1:C4")
End Sub
'assign to a range a specific number'
Sub CommandButton5_Click()
n = InputBox("enter a number")
Dim example As Range
Set example = Range("A5:C8")
example.Value = n
End Sub
'copy/paste'
Sub Selections_Click() 'I rename the subroutine as in the caption,outside in
Excel'
Range("A1:A2").Select
Selection.Copy
MsgBox "we make a small pause to see the second step"
Range("H1:H2").Select
ActiveSheet.Paste
MsgBox "we make another small pause to see the third step"
Range("K1:K2").Select
ActiveSheet.Paste
End Sub
'count'
Sub count_click()
Dim example As Range
Set example = Range("A1:C4")
MsgBox example.count
MsgBox "Good bye"
End Sub
Sub count2_click()
Dim example As Range
Set example = Range("A1:C9")
MsgBox example.count
End Sub
'Remark: If you insert a new command button in Developer->Insert-
>ActiveXControls->Command Button after you rename by
'right-click->Properties->set name as count2 you activate this sub
'Enjoy!
'Current region:Selections'
Sub region_click()
Range("A1").CurrentRegion.Select
Range("H1").CurrentRegion.Select
End Sub
'Color_maximum_value_of_dynamic_range'
Sub region_select_color_click()
Dim maximum As Double, rng As Range, cell As Range
Cells.Interior.ColorIndex = 0
Set rng = Range("D1").CurrentRegion
maximum = WorksheetFunction.Max(rng)
For Each cell In rng
If cell.Value = maximum Then
cell.Interior.ColorIndex = 22
End If
Next cell
End Sub
Sub Resize_click()
Range("A1:C4").Resize(3, 2).Select
MsgBox ("Hello,next step now")
Range("A1:C4").Resize(3, 5).Select
End Sub
Sub active_cell_click()
ActiveCell.EntireRow.Select
MsgBox "this is the next step"
ActiveCell.EntireColumn.Select
MsgBox "after that"
ActiveCell.EntireColumn.Value = 2
MsgBox "last step finally"
ActiveCell.EntireRow.Value = 4
End Sub
Sub active_cell2_click()
ActiveCell.EntireColumn.Value = 2
End Sub
'Here we can play in infinite ways
'We just have to be creative
'Next step will be offsetting positions--We will see how our buttons behave
in the near future
Sub active_cell3_click()
ActiveCell.EntireRow.Offset(1, 0).Cells(1).Value = 3
MsgBox "next step"
ActiveCell.EntireColumn.Offset(0, 1).Cells(1).Value = 5
End Sub