Primer For Visual Basic in Excel: Numbers 3 2 5 6 3 Sum
Primer For Visual Basic in Excel: Numbers 3 2 5 6 3 Sum
Introduction
Visual Basic (VB) is the controlling language that enables you to customize Microsoft Excel, Word, and
PowerPoint. VB is a cumbersome language, and one way to learn it is to have someone else write the
computer program for you. The way to do this is to create a MACRO. Any time you create a macro, Excel
writes a program (a Module) for the macro in VB.
Getting To the Visual Basic Editor
ALT_F11
Travels between the Visual Basic Editor and the Excel Worksheet. You can also click on the bottom panel or
on the top left Blue Excel icon or Click on Tools Macro VB Editor
Writing Visual Basic Programs Automatically by Creating Macros
Macros are automated procedures. If, for example you want to create a MACRO that adds the numbers in
row 1 and colors the sum box yellow take the following steps
Numbers
1.
2.
3.
4.
5.
6.
7.
3 Sum =
1.
2.
3.
4.
5.
7. Get back to the worksheet by clicking on the blue X of the command panel (or hit Alt_F11).
8. Click on Exit Design Mode of the Control Toolbox.
9. Click on the Command Button and it will perform the sum.
10. Click on the Command Button every time you enter new numbers to get a revised sum.
You can also Copy and Paste a VB program from a Macro to the VB Sheet for the Command Button
Sub Macro3()
' Using the Worksheet above, get the data in A1, double it in A2
Then copy the formula in Row 2 for all data points in Row 1 (A1:E1)
Range("A2").Select
ActiveCell.FormulaR1C1 = "=2*R[-1]C"
Range("A2").Select
Selection.AutoFill Destination:=Range("A2:E2"), Type:=xlFillDefault
Range("A2:E2").Select
End Sub
3. Do the Work
1
1
2
3
3
2
3
1
2
1
2
2
Number
Frequency
1
2
3
Sub Macro3D()
' Determine the number of cells with specified values for the Worksheet above
Range("G2:G4").Select
Selection.FormulaArray = "=FREQUENCY(R[-1]C[-6]:R[2]C[-3],RC[-1]:R[2]C[-1])"
End Sub
Sub Workbook_Open()
' This Program prints the color index in Column A and paints the color in Column B
' To find color index key hit F1 on the word, ColorIndex
Dim i As Integer
For i = 1 To 52
Cells(i, 1) = i
Cells(i, 2).Interior.ColorIndex = i
Next i
End Sub
Sub Macro4()
' FIRST, In Excel Worksheet, Enter # of Tens in Row 1 (B1 to K1) and # of Units in Column A
' Sum 10* number in Row 1 plus number in Column A
' This is code if we write formula in Cell B2 then copy and drag down, then copy and drag across
Range("B2").Select
ActiveCell.FormulaR1C1 = "=RC1+10*R1C"
Selection.AutoFill Destination:=Range("B2:B11")
Range("B2:B11").Select
Selection.AutoFill Destination:=Range("B2:K11"), Type:=xlFillDefault
End Sub
Sub Macro4B()
' FIRST, In Excel Worksheet, Enter # of Tens in Row 1 (B1 to K1) and # of Units in Column A
' Sum 10* number in Row 1 plus number in Column A
' The code below was created by highlighting B2:K11 and entering the formula with CRTL_SHFT_ENTER
Range("B2:K11").Select
Selection.FormulaArray = "=RC[-1]:R[5]C[-1]+10*R[-1]C:R[-1]C[5]"
End Sub
Sub Workbook_Open()
' This Program uses Nested Loops and Cell Reference to create the numbers from 0 to 99 in a 2-D Array
' This writes numbers equal to the col - 1 plus 10* the (row - 1)
Dim row As Integer, col As Integer
For row = 1 To 10
For col = 1 To 10
Cells(row, col) = col - 1 + 10 * (row - 1)
Next col
Next row
End Sub
Name
John
Mary
Sue
Grade
Test
65
95
35
Sub Macro(5)
'Assign Grade using If Statement and Color using Conditional Formatting
Range("B2:B4").Select
Selection.FormulaArray = "=IF(RC[1]:R[2]C[1]<60,""F"",IF(RC[1]:R[2]C[1]<90,""P"",""A""))"
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""F"""
Selection.FormatConditions(1).Interior.ColorIndex = 3
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""P"""
Selection.FormatConditions(2).Interior.ColorIndex = 6
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""A"""
Selection.FormatConditions(3).Interior.ColorIndex = 4
End Sub
Sub Workbook_Open()
' This Program assigns grades and colors using Cell Reference the If Statement in a Loop
Dim row As Integer, score As Integer, hue As Integer
Dim Grade As String
For row = 2 To 4
score = Cells(row, 3)
If score < 60 Then
Grade = "F"
hue = 3
ElseIf score < 90 Then
Grade = "P"
hue = 6
Else
Grade = "A"
hue = 4
End If
Cells(row, 2) = Grade
Cells(row, 2).Interior.ColorIndex = hue
Next row
End Sub
Sub Workbook_Open()
' This Program uses nested FOR-NEXT loops and SELECT CASE to color cells
'FIRST IN EXCEL Enter numbers from 1 to 64 in cells A1 to H8
' Spectrum colors are #11, 5, 8, 4, 6, 45, 3
Dim row As Integer, col As Integer
Dim x As Integer, hue As Integer
For row = 1 To 8
For col = 1 To 8
x = Cells(row, col).Value
Select Case x
Case 0 To 9
hue = 11
Case 10 To 19
hue = 5
Case 20 To 29
hue = 8
Case 30 To 39
hue = 4
Case 40 To 49
hue = 6
Case 50 To 59
hue = 45
Case 60 To 69
hue = 3
End Select
Dim B As String
row = 1
A = Cells(row, 1)
Sheets.Add
Sheets("Sheet4").Select
Sheets("Sheet4").Name = "Data"
Open A For Input As #1
Do Until EOF(1)
Line Input #1, B
Cells(row, 1) = B
row = row + 1
Loop
End Sub
The Input Box: An Automated Procedure to Put Data Into VBX Programs From the Excel Worksheet
The Input Box tells the user what data to enter in a program. The user friendly program below to find the
cube of a number is run with a command button. After clicking on the command button, an Input Box pops
up asking the user for the number he wants to cube. The