Lesson 9 Using Macros For Analytics
Lesson 9 Using Macros For Analytics
You are required to do few tasks in Microsoft Excel which are to be done repeatedly.
Also, you need to create and then run a macro that quickly applies these formatting changes
to the cells that needs to be selected.
To achieve these tasks, you will be learning a few concepts, such as macros for analytics,
means of data using macros, correlation coefficient and removing duplicates using macros
Using Macros for Analytics
Using Macros for Analytics
We use functions within Excel to perform data analysis, charting, and predictive analytics.
Using Macros for Analytics
Macros is an important feature in Excel which permits to do VBA programming within Excel workbook.
Source: https://www.google.com/url?sa=i&url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fwww.k2e.com%2Fseminars%2Fexcel-
macros%2F&psig=AOvVaw2h6kc_fd2sSQnjL8L12diF&ust=1635577130758000&source=images&cd=vfe&ved=0CAsQjRxqFwoTCMj4qeqF7_MCFQAAAAAdAAAAABAD
VBA
Source: https://www.google.com/url?sa=i&url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fwww.reddit.com%2Fr%2Fvba%2F&psig=AOvVaw0chti-
XNoCvPvHQYrG3jjy&ust=1635577238890000&source=images&cd=vfe&ved=0CAsQjRxqFwoTCJj-w5qG7_MCFQAAAAAdAAAAABAK
Create Macros and Functions
• Event based
• Subroutine/Sub Procedure
• Functions
Event Based
This is based on a macro event. For instance, whenever the worksheet is activated, a message box is printed
by the below macro. Example:
Subroutine
This is a set of commands that does some processing in the worksheet and does not return any value.
Example:
Functions
They are similar to sub procedures, but they return some value to the calling sub procedure.
Example:
The white area on the right side can be used to create all the macro functions on the data.
Create Macros and Functions
Sub function_name()
…
…
End Sub
Create Macros and Functions
To choose an event to work on, choose Worksheet on the first drop down and then activate
on the second drop down
Create Macros and Functions
This function will create a set of commands whenever the worksheet is activated.
Create Macros and Functions
A cell value within the sheet can be accessed using the cells.
Mean is defined as the sum of values in a data set divided by the number of values
in the data set.
Mean
We can the find mean of a column of values using macros for Excel.
Steps to Find Mean
The five point summary in statistics specifies five values to describe a set of numeric values.
Values of Five Point Summary
The five point summary can be visualized using a box and whisker chart.
Values of Five Point Summary
The lowest point is minimum, and the topmost value is the maximum value.
Values of Five Point Summary
Press Alt+F11 on the Excel sheet where we have the data set of Boston_housing
Boston_housing
Calculate Five Point Summary
Macro values are the same as in the box and whisker plot.
Correlation Coefficient Using Macros
Correlation Coefficient Using Macros
Let us consider an example: For the Boston housing data, we can implement a macro to calculate the
correlation coefficient between ‘INDUS’ and ‘MEDV’ using a macro.
Correlation Coefficient Using Macros
When we verify the results with the CORREL function, both are the same.
Steps to Find Correlation Coefficient
Step 3: The code calculates the correlation coefficient using the mathematical formula for
‘INDUS’ and ‘MEDV’ columns
Steps to Find Correlation Coefficient
Step 6: The results are stored in the same Excel in columns P and Q
We can see that the calculated correlation coefficient and CORREL functions are the same.
Steps to Find Correlation Coefficient
Step 7: Any formula can be assigned to a cell value using macros. This is done by the following
command:
To remove duplicates using macros in a data set within a range, we can use the
RemoveDuplicates function.
Removing Duplicates
Columns:=1 specifies that the first column must be used for checking duplicates.
We will try to remove duplicates from a range of rows and columns for our entire data set of
Boston housing.
Range("A2:N451").RemoveDuplicates Header:=xlNo
Steps to Remove Duplicates
We can find the mean of a column of values using Macros for Excel.
A. Programming
B. Formula writing
C. Charting
A. Programming
B. Formula writing
C. Charting
A. Visual Basic
B. VC++
C. VBA
D.
Knowledge
Check
Macros are based on which programming language?
2
A. Visual Basic
B. VC++
C. VBA
D.
D.
Knowledge
Check
VBA in Excel macros stands for?
3
D.
A. Cells(11,3).values="=MEAN(A1:A24)"
B. Cells(11,3)="=MEAN(A1:A24)"
C. Cells(11,3)="=AVERAGE(A1:A24)"
D. Cells(11,3).values="=AVERAGE(A1:A24)"
Knowledge
Check
What is the macro way to set the value of C11 to mean of cells A1:A24?
5
A. Cells(11,3).values="=MEAN(A1:A24)"
B. Cells(11,3)="=MEAN(A1:A24)"
C. Cells(11,3)="=AVERAGE(A1:A24)"
D. Cells(11,3).values="=AVERAGE(A1:A24)"
A. True
B. False
C.
D.
Knowledge
Check For finding median of a dataset using macros, the data is ordered in ascending and
6 middle value is found programmatically? True or False.
A. True
B. False
C.
D.
True. By definition of median, the data is ordered in ascending order and the middle value(s) is/are the median(s).
Knowledge
Check
Which of the following defines Interquartile range?
7
A. Q1-Q2
B. Q2-Q3
C. Q3-Q1
D.
Knowledge
Check
Which of the following defines Interquartile range?
7
A. Q1-Q2
B. Q2-Q3
C. Q3-Q1
D.
A. Mean
B. Median
C. Maximum
D. Minimum
Knowledge
Check
Which of the following is NOT a part of the 5-point summary?
8
A. Mean
B. Median
C. Maximum
D. Minimum
A. Bar chart
B. Box-and-whisker
C. Line graph
D. Histogram
Knowledge
Check
Which plot is based on the 5-point summary?
9
A. Bar chart
B. Box-and-whisker
C. Line graph
D. Histogram
A. 0
B. 1
C. 2
D.
Knowledge
Check
What is the maximum value of the correlation coefficient?
10
A. 0
B. 1
C. 2
D.
A. -1
B. 0
C. 1
D.
Knowledge
Check If two variables are non-correlated, the value of the correlation coefficient is around
11 which value?
A. -1
B. 0
C. 1
D.
C.
D.
Knowledge
Check
What is typically done with highly correlated variables for data analytics?
12
C.
D.
Highly correlated variables are removed and only one of them is retained for the model.
Knowledge
Check
What is the range of Correlation values?
13
A. 0 and 1
B. 1 and 2
C. -1 and +1
D. -1 and 0
Knowledge
Check
What is the range of Correlation values?
13
A. 0 and 1
B. 1 and 2
C. -1 and +1
D. -1 and 0
A. True
B. False
C.
D.
Knowledge
Check
Duplicates cannot be removed using macros. True or False.
14
A. True
B. False
C.
D.
A. Header:=xlNo
B. Header:=No
C. Header:=FALSE
D. Header:=None
Knowledge
Check How is the function for RemoveDuplicates used to specify that there is no header in
15 the data?
A. Header:=xlNo
B. Header:=No
C. Header:=FALSE
D. Header:=None
A. True
B. False
C.
D.
Knowledge
Check Columns keyword is used to specify the column number to check for duplicates in
16 RemoveDuplicates macro function. True or False.
A. True
B. False
C.
D.