0258 Excel Analytics and Programming PDF
0258 Excel Analytics and Programming PDF
George Zhao
[email protected]
Goals of the Workshop
Learn Excel tools by utilizing them in various cases
Tools and materials covered here are merely a sample of
Excel functionality
Understand the logic and syntax behind Visual Basic
programming, which interacts with the Excel interface
No programming background required
Create dynamic algorithms to approach cases
When data is changed, but retains its original format,
the algorithm should be able to automatically handle the
transition appropriately
Lesson materials:
Learning Slides[.pdf]
Exercises - Blank [.xlsx]
Exercises - Filled [.xlsm]
1 2 3 4 5
1
2
3
4
5
x y
-3 9.9
-2
-1
0
1
2
3
Created by George Zhao 12
Show Formulas
Formulas > Show Formulas
Toggle on and off between showing / not showing
x y
-3 9.9
-2 4
-1 #VALUE!
0 0
1 5
2 #VALUE!
3 9
Created by George Zhao 14
1-Dimensional Fixed Reference
Fix cell reference in cells A1, B2, BB32: $A$1, $B$2,
$BB$32
r= 0.1
x y
-3 9.9
-2 4.4
-1 1.1
0 0
1 1.1
2 4.4
3 9.9
Created by George Zhao 15
Deeper Look into Fixing Reference
A1: not fixing column A nor row 1
$A$1: fixing column A and row 1
$A1: fixing ONLY column A, not row 1
A$1: fixing ONLY row 1, not column A
1 2 3 4 5
1
2
3 12
4
5
Created by George Zhao 17
Example: Multiplication Table
Focus again on cell E4: (12 = 3 x 4)
All entries on row 4: product of 3 (A4) and ___
All entries on column E: product of 4 (E1) and ___
1 2 3 4 5
1
2
3 12
4
5
Created by George Zhao 28
Actual Code Juxtaposed
Multiply the row index by the column index
Do this for each cell in the row, and then for each row
=PERCENTRANK([array], x)
size = 30
location = "Hamilton Hall"
passFail = False
avgGrade = 94.4
ltrGrade = A
size = 30
location = "Hamilton Hall"
passFail = False
avgGrade = 94.4
ltrGrade = A
Dim dat(2, 1)
dat(0, 0) = "Criterion"
dat(0, 1) = "Value"
dat(1, 0) = "Budget"
dat(1, 1) = 5123.21
dat(2, 0) = "Enough?"
dat(2, 1) = True
ReDim Preserve dat(2,1)
0 1
0 Criterion Value
1 Budget 5123.21
2 Enough? TRUE
ALT + F11
Created by George Zhao 69
ALT + F11
Cells(1,1) = 1
Cells(2,1) = 2
Cells(3,1) = 3
…
Cells(1000,1) = 1000
Row = 1
Do Until Row=1000
Cells(Row, 1) = Row
Row = Row + 1
Loop
Range("A1").Select
Do Until IsEmpty(ActiveCell.Value)
[Can do something here for each row]
ActiveCell.Offset(1,0).Select
Loop
ActiveCell.FormulaR1C1 = "3"
Range("C3").Select
ActiveCell.FormulaR1C1 = "=R[-2]C[-2]^2"
Range("C4").Select
Created by George Zhao 154
Defining Function in VBA
Look at this line of code:
ActiveCell.FormulaR1C1 = "=R[-2]C[-2]^2"
ActiveCell can be substituted for other equivalents:
Cells(2,3)
ActiveCell.Offset(1,2)
Range("F5:G9")
Range("F5:G9").Offset(10, 10)
=R[-2]C[- ]^
Offset from current cell, 2 rows above and 2 columns left
2500
2000
1500 Close
Open
1000
500
0
7/20/2012 7/18/2012 6/25/2012 6/24/2012 6/18/2012
1000
3000
0
2500
1500
500
6/18/2012
6/19/2012
6/20/2012
6/21/2012
6/22/2012
7/19/2012
7/20/2012
Close
Open
178
The Problem
Axis is arranged numerically, treating the dates on a
continuous spectrum
We want discrete spectrum, treating the dates not as
numerical, but as text
Luckily, there’s feature for that
Right click axis > Format Axis > Axis Type: Text Axis
Range("B3").Select
Do Until IsEmpty(ActiveCell.Value)
fullText = ActiveCell.Value
startPosition = 1
bracketedText = "["
Range("G3").Select
currentRow = 1
Created by George Zhao 235
Traversing Data
Columns("X:Y").Clear
Do Until IsEmpty(ActiveCell)
If ActiveCell.Value >= Min And ActiveCell.Value <= Max
Then
Cells(currentRow, 24) = ActiveCell.Offset(0, -6)
Cells(currentRow, 25) = ActiveCell.Offset(0, -5)
currentRow = currentRow + 1
End If
ActiveCell.Offset(1, 0).Select
Loop
Created by George Zhao 236
Auto Fit Column & Hide Form
Columns X:Y" .EntireColumn.AutoFit
UserForm1.Hide
End Sub
Sub Show()
UserForm1.Show
End Sub
16 10
The Bronx
Brooklyn
Manhattan
Queens
50