Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

VBA Macro Workshop III: Excel Data Manipulation with VBA in Bruin Health Insurance, Study notes of Personal Health

An introduction to Visual Basic for Applications (VBA) in Excel, focusing on updating the company's data storage format for Bruin Health Insurance. The objective is to write a VBA macro to create new rows for renewed memberships and format the data accordingly. enabling the Developer tab, getting started with VBA, VBA in Microsoft Excel, subroutines, functions, macro recorder, variables, arrays, IF statements, WHILE and FOR loops, FOR EACH loops, message boxes, and debugging tools.

Typology: Study notes

2021/2022

Uploaded on 09/12/2022

charlene
charlene 🇺🇸

4.8

(5)

266 documents

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
EXCEL WORKSHOP III:
INTRODUCTION TO VBA IN
EXCEL
Bruin Actuarial Society
Note: Slides will be posted on our website after this meeting
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download VBA Macro Workshop III: Excel Data Manipulation with VBA in Bruin Health Insurance and more Study notes Personal Health in PDF only on Docsity!

EXCEL WORKSHOP III:

INTRODUCTION TO VBA IN

EXCEL

Bruin Actuarial Society

Note: Slides will be posted on our website after this meeting

BACKGROUND

  • You are an actuary for Bruin Health Insurance, LCC, a Los Angeles based company insuring personal health claims in Southern California
  • Prior to your team’s analysis of coverage and reserves, you notice that the policy data is not formatted in the desired manner
  • Each membership is only listed once, regardless of how many times it has been renewed
  • Problem: You need to be able to identify the specific 1-year term you’re looking at when conducting the analyses

OBJECTIVE

  1. Write a VBA macro to accomplish this task with the short excerpt of data
  2. Then, once you’ve ensured the macro works correctly, run it on the full set of membership data, splitting the 13,041 membership plans into 39,215 rows

OUR GOAL

ENABLING THE DEVELOPER TAB File Option Customize Ribbon Check “Developer”

THE VBA ENVIRONMENT Multiple ways to open the VBA Editor:

  1. Go to Developer tab → Select Visual Basic
  2. Keyboard shortcut:
  • Windows: Alt + F
  • Mac: Opt + F
  1. Right-click on a worksheet → Select View Code

GETTING STARTED A module is where you can write VBA code Type your code here

GETTING STARTED

  • It is always a good idea to put Option Explicit in the declarations at the top of your module
  • This forces you to declare all your variables
  • You can either:
    1. Manually type this, or
    2. Go to Tools → Options → Check “Require Variable Declaration”

VBA IN MICROSOFT EXCEL

  • 2 commonly used methods in VBA: Select and Activate
  • Select allows you to select one or more objects Worksheets(“Sheet1”).Select Range(“A1:A3,C1:C3,E1:E3”).Select
  • Activate allows you to select one object. If you already have multiple objects selected, this allows you to select one object within them. Range(“A1”).Activate
  • Ex: You can select a worksheet where you want your code to run and then activate the first cell in that worksheet that you want to apply changes to.

SUBROUTINES

  • Most of your VBA code will be in subroutines (also called macros )
  • Subroutines can be used to perform calculations, change formatting, and copy and paste among other repetitive tasks
  • We enclose our code in the following: Sub Myroutine() End Sub
  • In this case, we have named our subroutine “Myroutine()” (but we can name this whatever we want)

FUNCTIONS

  • Syntax Function MyFunction(param1 As dtype, param2 As dtype,…) As dtype statements MyFunction = value End Function
  • Ultimately, you assign a return value by setting the name of the function to some value
  • These functions can be called from workbook cells, like how you use COUNTIF(), VLOOKUP(), and other functions

MACRO RECORDER

  • This is the easiest way to “write” a macro
  • Under the Developer tab, click “Record Macro”
  • All your actions will be translated into code, which you can find under Modules in the VBA Editor
  • You can look at and modify this code to suit your purposes
  • Often slow and clunky, but can be very powerful tool if used right (e.g. no one remembers how to filter/sort data in VBA, but this tool helps!)

VARIABLES

  • If the data type is not specified, the variable will be declared as a variant
  • A variant can contain any kind of data, and the data type can change at any point
  • Try to avoid these if possible (they require a lot of memory)

ARRAYS

  • Arrays are created with parentheses to indicate size: Dim myarray(5) as Integer
  • The size can be changed: ReDim myarray(10)
  • But this will delete the data. To preserve the data inside, use: ReDim Preserve myarray(15)
  • Individual elements can be accessed and modified with parentheses: myarray(0) = 5
  • There’s a lot more to be learned with arrays, but you can look online for more details. We’ll work primarily with Workbook objects instead.