0% found this document useful (0 votes)
116 views4 pages

Buttons, Ranges and Arrays - VBA

In this section you will see how you can make selections,activate cells and create buttons and clicks All the commands are organized in subroutines ENJOY!

Uploaded by

theodor_munteanu
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)
116 views4 pages

Buttons, Ranges and Arrays - VBA

In this section you will see how you can make selections,activate cells and create buttons and clicks All the commands are organized in subroutines ENJOY!

Uploaded by

theodor_munteanu
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/ 4

Buttons,ranges,arrays in VBA

Function ColorIndex(rcell As Range)


ColorIndex = Range(1, 1).Interior.ColorIndex
End Function

Private Sub Formula_Click()


Range("D4").Formula = "=B3*10"
Range("D4").Formula = "R3C2*10"
Range("D4").Formula = "=R[-1]C[-2]*10"
End Sub

'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

'Size of a 2-Dimensional Array'


Sub array2D()
Dim films(1 To 5, 1 To 2) As String, x As Integer, y As Integer
x = UBound(films, 1) - LBound(films, 1) + 1
y = UBound(films, 2) - LBound(films, 2) + 1
MsgBox "This array consists of" & x * y & "elements"
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

'select a specific column'


Sub CommandButton6_Click()
Dim example As Range
Set example = Range("A1:C4")
example.Columns(2).Select
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

'Exercise for HW'


'Make another command button that from a region selects its minimum and
emphasizes it
Sub region_select_color_min_click()
Dim minimum As Double, rng As Range, cell As Range
Cells.Interior.ColorIndex = 0
Set rng = Range("D1").CurrentRegion
minimum = WorksheetFunction.Min(rng)
For Each cell In rng
If cell.Value = minimum 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

'select only a column let's see how'


Sub select_column_click()
For i = 1 To 3
MsgBox "next column to be selected is " & i
Range("A1:C4").Resize(, i).Select
Next i 'You will notice that after the last step all columns are selected'
End Sub

'Activation of worksheets,ranges and objects'


Sub activation_click()
Worksheets(2).Activate
Worksheets(2).Cells.Select
MsgBox "this was the first step"
Columns(2).Select
MsgBox "this was the second step"
Rows("5:7").Select
MsgBox "this was the third step"
Columns("B:E").Select
MsgBox "that was another selection"
MsgBox Cells(5, 2).Row 'shows the row number'
MsgBox Cells(5, 2).Column 'shows the column number'
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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy