SlideShare une entreprise Scribd logo
1  sur  24
VBA Level 3

 By Ben Miu
What do we know?
•   Creating variables
•   Loops
•   Arrays
•   If-Then Else
•   Range Objects
•   Dictionary Objects
•   Subs/Functions
•   UserForm
ADO VBA
• ADO stands for ActiveX Data Objects. It allows
  VBA to communicate with back end databases
• A database back end is useful for storing
  information if there are more fields that Excel
  can handle
• It serves as a centralized location for all your
  tables
• Code is provided on next page
ADO VBA
Dim conn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rs As ADODB.Recordset

 conn.Open "Driver={Microsoft Access Driver (*.mdb, *.accdb)};};Dbq="DailyRiskFolder &
"Daily Risk.accdb;"
 Set cmd.ActiveConnection = conn

cmd.CommandText = "insert into Pricing (PriceDate, PriceIndex, PricePrice) values(#" &
strDate & "#,'" & a.Offset(0, 1).value & "','" & a.Offset(0, 2).value & "')"
cmd.CommandType = adCmdText
cmd.execute

cmd.CommandText = "select PricePrice from Pricing where PriceIndex = '" &
a.Offset(0, 1).value & "' and PriceDate = #" & finalDate & "#"
cmd.CommandType = adCmdText
Set rs = cmd.execute()
FreeFile
• Free file is a method which allows CSV files or
  TXT files to be read without having to use
  Excel to parse the data
• Commonly used with the Split function which
  creates a array for the data values within the
  CSV
• VB.NET equivalent is
  streamreader/streamwriter
• Useful for input files which are not XLS
FreeFile
intinFile = FreeFile
Open InputFile For Input As #intinFile

Do Until EOF(intinFile)
   Line Input #intinFile, sLine
Loop

Close #intinFile

intOutFile = FreeFile
Open InputFile For Output As intOutFile

For Each eline In strLine
  If eline <> "" Or eline <> vbCr Then Print #intOutFile, eline
Next eline

Close #intOutFile
Classes in VBA
• Class objects are a move towards true object
  orientation where you can re-use classes in
  other projects
• New era of programming whereby your code
  is portable
• Expert level VBA as understanding classes can
  be very tricky if you don’t get the basics down
• A sample class on next slide
Instances / Constructor / Destructor
• Classes allow you to create separate worlds
  based on base code where they don’t interact
  to each other
• Possibility of code conflicts is eliminated with
  classes and scope control is possible
• No real benefits of classes in simple VBA
  applications. Starts to make sense as you
  code more and more and don’t want to repeat
  yourself
Daily Risk Project
• Hired to bring automation to the risk group
• Daily Risk started as separate processes like
  partition creation, hierarchy generation and
  eventually was combined with reporting
• Daily Risk One Button solution is the risk
  group’s answer to all manual processes. It’s
  estimated that the Daily Risk process can do at
  least 3-4 people’s worth of work and cut down
  on hundreds of man hours per month
Evolution of Risk Needs
• With more object oriented coding, there will
  be more robust reporting capabilities faster
• For example, if someone wants a HEEN risk
  report or FSER risk report, and they want
  customization, object oriented programming
  base code will maintain the regular template
  and be able to handle new specific changes
• CONST (Constants allow for all change to take
  place on one module)
S&T
• S&T is responsible for the development of
  applications to be deployed enterprise wide or
  to a specific group
• Process involves requirements
  document, business analysis, project
  management, quality assurance, training and
  use case analysis
• Ted Tea does the VB.NET side whereas Trevor
  Tilley does the C# side
Two approaches to coding
• Business user understands his own
  processes, codes himself
• Turn around time is very fast as no requirements
  documentation is required
• Downside to business user’s coding is if the
  person ever leaves then someone else must
  maintain the code
• Could represent a operational risk.
  Therefore, Trevor Tilley is not a fan of VBA coders
  running around
When you should automate?
• The idea is that you should automate if the
  time it takes to code is faster than the time it
  is to do the process manually and/or the
  process is a daily process which requires many
  man hours
• Cutting and pasting one spreadsheet to
  another is not a justification to write code
  unless that cutting and pasting is part of a
  bigger picture
Breaking down to basic steps
• Much of coding is negotiating with other
  departments to get the data in a format that you
  can read with macros. Makes no sense to be
  reading a PDF file as a PDF file comes from a XLS
  or DOC originally
• All systems essentially have a database backend
  and SQL scripts can automate output from those
  databases
• Problem is, the other group may not have the
  resources to give you what you want
Moving from the small application
• A small cut and paste procedure can be much
  much more and start to become larger and
  eventually replace entire processes
• Bonnie part of VFA prepares the performance
  report which is ultimately driven by daily
  Sylvan output files
• Don’t just automate the cut and
  paste, automate the entire process to just one
  button
Bigger picture
• Small processes to larger processes
  encompassing more people in your group
  requires you to be the one that talks to others
  in your group and automate their work
• Many times, for example, Candy might
  prepare a file for Felix to deal with, then Felix
  passes the end result to Wenli. 3 people
  passing a document around modified 3
  different times
Process Map of Reporting

               Candy gets PDF from
                client and types it
                    into system




         Felix inserts
         benchmarks




                   Wenli creates
                     reports
How to think about a process
• When presented with a problem, the first
  thing you should think about is what types of
  VBA are you going to use. Are you going to
  use a dictionary, a array, a range? Are you
  going to use a back end database?
• Do you see this process being something that
  needs to be done on a regular basis?

Quick garbage code vs. Robust code
Garbage Code vs. Robust Code
• Using a For loop and you counting the number
  of rows is considered garbage code because it
  isn’t going to be robust
• Even using a While-Wend loop isn’t robust
  enough because the users can put spaces in
  between cells
• Let’s use Excel’s UsedRange approach to assist
How I first learnt VBA
• A D- student in VB class in high school
• Later when working at FK Imports, (Giftware
  industry) started to use VB code to assist in
  Crystal Reporting
• Started to become an automator at BMO
  Middle Office
• TD operations were simplified to one button
  clicks.
How to learn VBA
• Macro recorder is key to getting the syntax right.
  Don’t try to memorize the code
• Change the macro recorder’s output with what
  you have learned
• Don’t always choose the manual way over the
  automated way
• Books and video’s don’t help a lot because many
  times you will have to sift through it and it
  doesn’t help you
• Only way is to think ‘automation’ at every step of
  the way
Case Studies
Thank You

Contenu connexe

Tendances

E learning excel vba programming lesson 4
E learning excel vba programming  lesson 4E learning excel vba programming  lesson 4
E learning excel vba programming lesson 4Vijay Perepa
 
AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2Dan D'Urso
 
Excel VBA programming basics
Excel VBA programming basicsExcel VBA programming basics
Excel VBA programming basicsHang Dong
 
VBA Classes from Chandoo.org - Course Brochure
VBA Classes from Chandoo.org - Course BrochureVBA Classes from Chandoo.org - Course Brochure
VBA Classes from Chandoo.org - Course Brochurer1c1
 
AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1guest38bf
 
Online Advance Excel & VBA Training in India
 Online Advance Excel & VBA Training in India Online Advance Excel & VBA Training in India
Online Advance Excel & VBA Training in Indiaibinstitute0
 
Intro to Excel VBA Programming
Intro to Excel VBA ProgrammingIntro to Excel VBA Programming
Intro to Excel VBA Programmingiveytechnologyclub
 
How to apply a formula and macro in excel......by irfan afzal
How to apply a formula and macro in excel......by irfan afzalHow to apply a formula and macro in excel......by irfan afzal
How to apply a formula and macro in excel......by irfan afzal1995786
 
Getting started with Microsoft Excel Macros
Getting started with Microsoft Excel MacrosGetting started with Microsoft Excel Macros
Getting started with Microsoft Excel MacrosNick Weisenberger
 
2016 Excel/VBA Notes
2016 Excel/VBA Notes2016 Excel/VBA Notes
2016 Excel/VBA NotesYang Ye
 
Microsoft Office 2003 Creating Macros
Microsoft Office 2003 Creating MacrosMicrosoft Office 2003 Creating Macros
Microsoft Office 2003 Creating MacrosS Burks
 
Visual Basics for Application
Visual Basics for Application Visual Basics for Application
Visual Basics for Application Raghu nath
 
A Quick Simple MS Excel Macro
A Quick Simple MS Excel MacroA Quick Simple MS Excel Macro
A Quick Simple MS Excel MacroPranav Ghode
 
Introduction to AntiPatterns & CodeSmells
Introduction to AntiPatterns & CodeSmellsIntroduction to AntiPatterns & CodeSmells
Introduction to AntiPatterns & CodeSmellsClaudio Bernasconi
 

Tendances (20)

Vba introduction
Vba introductionVba introduction
Vba introduction
 
Vba Class Level 1
Vba Class Level 1Vba Class Level 1
Vba Class Level 1
 
Using Vba Excel
Using Vba ExcelUsing Vba Excel
Using Vba Excel
 
E learning excel vba programming lesson 4
E learning excel vba programming  lesson 4E learning excel vba programming  lesson 4
E learning excel vba programming lesson 4
 
AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2
 
Vba
Vba Vba
Vba
 
Excel VBA programming basics
Excel VBA programming basicsExcel VBA programming basics
Excel VBA programming basics
 
VBA Classes from Chandoo.org - Course Brochure
VBA Classes from Chandoo.org - Course BrochureVBA Classes from Chandoo.org - Course Brochure
VBA Classes from Chandoo.org - Course Brochure
 
AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1
 
Online Advance Excel & VBA Training in India
 Online Advance Excel & VBA Training in India Online Advance Excel & VBA Training in India
Online Advance Excel & VBA Training in India
 
Intro to Excel VBA Programming
Intro to Excel VBA ProgrammingIntro to Excel VBA Programming
Intro to Excel VBA Programming
 
How to apply a formula and macro in excel......by irfan afzal
How to apply a formula and macro in excel......by irfan afzalHow to apply a formula and macro in excel......by irfan afzal
How to apply a formula and macro in excel......by irfan afzal
 
Vba part 1
Vba part 1Vba part 1
Vba part 1
 
Getting started with Microsoft Excel Macros
Getting started with Microsoft Excel MacrosGetting started with Microsoft Excel Macros
Getting started with Microsoft Excel Macros
 
2016 Excel/VBA Notes
2016 Excel/VBA Notes2016 Excel/VBA Notes
2016 Excel/VBA Notes
 
Microsoft Office 2003 Creating Macros
Microsoft Office 2003 Creating MacrosMicrosoft Office 2003 Creating Macros
Microsoft Office 2003 Creating Macros
 
Visual Basics for Application
Visual Basics for Application Visual Basics for Application
Visual Basics for Application
 
A Quick Simple MS Excel Macro
A Quick Simple MS Excel MacroA Quick Simple MS Excel Macro
A Quick Simple MS Excel Macro
 
Learn Excel Macro
Learn Excel Macro  Learn Excel Macro
Learn Excel Macro
 
Introduction to AntiPatterns & CodeSmells
Introduction to AntiPatterns & CodeSmellsIntroduction to AntiPatterns & CodeSmells
Introduction to AntiPatterns & CodeSmells
 

En vedette

Financial statement analysis
Financial statement analysisFinancial statement analysis
Financial statement analysisNikhil Jaiswal
 
An AMIS Overview of Oracle database 12c (12.1)
An AMIS Overview of Oracle database 12c (12.1)An AMIS Overview of Oracle database 12c (12.1)
An AMIS Overview of Oracle database 12c (12.1)Marco Gralike
 
Austin Journal of Accounting, Audit and Finance Management
Austin Journal of Accounting, Audit and Finance ManagementAustin Journal of Accounting, Audit and Finance Management
Austin Journal of Accounting, Audit and Finance ManagementAustin Publishing Group
 
Project management
Project managementProject management
Project managementAvay Minni
 
Anglophilia
AnglophiliaAnglophilia
Anglophiliatloewen
 
IC12 - Leading Change in Rotary Clubs - Breakout Session
IC12 - Leading Change in Rotary Clubs - Breakout SessionIC12 - Leading Change in Rotary Clubs - Breakout Session
IC12 - Leading Change in Rotary Clubs - Breakout SessionRotary International
 
Rotary Club Orientation
Rotary Club OrientationRotary Club Orientation
Rotary Club OrientationKalpit Sanghvi
 
Basics of Accounting
Basics of Accounting Basics of Accounting
Basics of Accounting saiprasad D
 
Revitalize + Rethink Your Rotary Club: Crafting Your Member Experience
Revitalize + Rethink Your Rotary Club: Crafting Your Member ExperienceRevitalize + Rethink Your Rotary Club: Crafting Your Member Experience
Revitalize + Rethink Your Rotary Club: Crafting Your Member ExperienceRotary International
 
Henry VIII
Henry VIIIHenry VIII
Henry VIIIneni
 
Understanding financial statements
Understanding financial statementsUnderstanding financial statements
Understanding financial statementsGeni Whitehouse
 

En vedette (19)

Vba Excel Level 2
Vba Excel Level 2Vba Excel Level 2
Vba Excel Level 2
 
Financial statement analysis
Financial statement analysisFinancial statement analysis
Financial statement analysis
 
An AMIS Overview of Oracle database 12c (12.1)
An AMIS Overview of Oracle database 12c (12.1)An AMIS Overview of Oracle database 12c (12.1)
An AMIS Overview of Oracle database 12c (12.1)
 
Austin Journal of Accounting, Audit and Finance Management
Austin Journal of Accounting, Audit and Finance ManagementAustin Journal of Accounting, Audit and Finance Management
Austin Journal of Accounting, Audit and Finance Management
 
Project management
Project managementProject management
Project management
 
Understanding Financial Statement fraud- Forensic Accounting Perspective
Understanding Financial Statement fraud- Forensic Accounting PerspectiveUnderstanding Financial Statement fraud- Forensic Accounting Perspective
Understanding Financial Statement fraud- Forensic Accounting Perspective
 
Anglophilia
AnglophiliaAnglophilia
Anglophilia
 
Milwaukee Boot Camp Holiday Party
Milwaukee Boot Camp Holiday PartyMilwaukee Boot Camp Holiday Party
Milwaukee Boot Camp Holiday Party
 
Catalogo milwaukee 2016
Catalogo milwaukee 2016Catalogo milwaukee 2016
Catalogo milwaukee 2016
 
IC12 - Leading Change in Rotary Clubs - Breakout Session
IC12 - Leading Change in Rotary Clubs - Breakout SessionIC12 - Leading Change in Rotary Clubs - Breakout Session
IC12 - Leading Change in Rotary Clubs - Breakout Session
 
Rotary Club Orientation
Rotary Club OrientationRotary Club Orientation
Rotary Club Orientation
 
United Kingdom
United KingdomUnited Kingdom
United Kingdom
 
Austin Cell Science
Austin Cell ScienceAustin Cell Science
Austin Cell Science
 
Henry VIII
Henry VIIIHenry VIII
Henry VIII
 
Basics of Accounting
Basics of Accounting Basics of Accounting
Basics of Accounting
 
Excel vba macro programing
Excel vba macro programingExcel vba macro programing
Excel vba macro programing
 
Revitalize + Rethink Your Rotary Club: Crafting Your Member Experience
Revitalize + Rethink Your Rotary Club: Crafting Your Member ExperienceRevitalize + Rethink Your Rotary Club: Crafting Your Member Experience
Revitalize + Rethink Your Rotary Club: Crafting Your Member Experience
 
Henry VIII
Henry VIIIHenry VIII
Henry VIII
 
Understanding financial statements
Understanding financial statementsUnderstanding financial statements
Understanding financial statements
 

Similaire à Vba Class Level 3

Model-driven and low-code development for event-based systems | Bobby Calderw...
Model-driven and low-code development for event-based systems | Bobby Calderw...Model-driven and low-code development for event-based systems | Bobby Calderw...
Model-driven and low-code development for event-based systems | Bobby Calderw...HostedbyConfluent
 
Object Oriented Programming I
Object Oriented Programming IObject Oriented Programming I
Object Oriented Programming Iraven_rainagi
 
Architectural Decisions: Smoothly and Consistently
Architectural Decisions: Smoothly and ConsistentlyArchitectural Decisions: Smoothly and Consistently
Architectural Decisions: Smoothly and ConsistentlyComsysto Reply GmbH
 
Architectural Decisions: Smoothly and Consistently
Architectural Decisions: Smoothly and ConsistentlyArchitectural Decisions: Smoothly and Consistently
Architectural Decisions: Smoothly and ConsistentlyComsysto Reply GmbH
 
Dapper: the microORM that will change your life
Dapper: the microORM that will change your lifeDapper: the microORM that will change your life
Dapper: the microORM that will change your lifeDavide Mauri
 
Software Architecture and Architectors: useless VS valuable
Software Architecture and Architectors: useless VS valuableSoftware Architecture and Architectors: useless VS valuable
Software Architecture and Architectors: useless VS valuableComsysto Reply GmbH
 
Designing and Implementing Information Systems with Event Modeling, Bobby Cal...
Designing and Implementing Information Systems with Event Modeling, Bobby Cal...Designing and Implementing Information Systems with Event Modeling, Bobby Cal...
Designing and Implementing Information Systems with Event Modeling, Bobby Cal...confluent
 
Domain Driven Design Big Picture Strategic Patterns
Domain Driven Design Big Picture Strategic PatternsDomain Driven Design Big Picture Strategic Patterns
Domain Driven Design Big Picture Strategic PatternsMark Windholtz
 
Building Information Systems using Event Modeling (Bobby Calderwood, Evident ...
Building Information Systems using Event Modeling (Bobby Calderwood, Evident ...Building Information Systems using Event Modeling (Bobby Calderwood, Evident ...
Building Information Systems using Event Modeling (Bobby Calderwood, Evident ...confluent
 
Tech for the Non Technical - Anatomy of an Application Stack
Tech for the Non Technical - Anatomy of an Application StackTech for the Non Technical - Anatomy of an Application Stack
Tech for the Non Technical - Anatomy of an Application StackIntelligent_ly
 
Tasks Your Business Should Automate in 2022 That Just Make Sense
Tasks Your Business Should Automate in 2022 That Just Make SenseTasks Your Business Should Automate in 2022 That Just Make Sense
Tasks Your Business Should Automate in 2022 That Just Make SenseSafe Software
 
DITA Quick Start Webinar: Defining Your Style Sheet Requirements
DITA Quick Start Webinar: Defining Your Style Sheet RequirementsDITA Quick Start Webinar: Defining Your Style Sheet Requirements
DITA Quick Start Webinar: Defining Your Style Sheet RequirementsSuite Solutions
 
Introducing systems analysis, design & development Concepts
Introducing systems analysis, design & development ConceptsIntroducing systems analysis, design & development Concepts
Introducing systems analysis, design & development ConceptsShafiul Azam Chowdhury
 
Multi View Constructed Right
Multi View Constructed RightMulti View Constructed Right
Multi View Constructed RightOpenBossa
 
Building Big Data Streaming Architectures
Building Big Data Streaming ArchitecturesBuilding Big Data Streaming Architectures
Building Big Data Streaming ArchitecturesDavid Martínez Rego
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesInductive Automation
 
An Introduction To Model  View  Controller In XPages
An Introduction To Model  View  Controller In XPagesAn Introduction To Model  View  Controller In XPages
An Introduction To Model  View  Controller In XPagesUlrich Krause
 
VidyaBhooshanMishra_CV
VidyaBhooshanMishra_CVVidyaBhooshanMishra_CV
VidyaBhooshanMishra_CVLandis+Gyr
 
Lessons Learned from a major IBM Collaboration Solutions Deployment
Lessons Learned from a major IBM Collaboration Solutions DeploymentLessons Learned from a major IBM Collaboration Solutions Deployment
Lessons Learned from a major IBM Collaboration Solutions DeploymentMartijn de Jong
 

Similaire à Vba Class Level 3 (20)

Model-driven and low-code development for event-based systems | Bobby Calderw...
Model-driven and low-code development for event-based systems | Bobby Calderw...Model-driven and low-code development for event-based systems | Bobby Calderw...
Model-driven and low-code development for event-based systems | Bobby Calderw...
 
Object Oriented Programming I
Object Oriented Programming IObject Oriented Programming I
Object Oriented Programming I
 
Architectural Decisions: Smoothly and Consistently
Architectural Decisions: Smoothly and ConsistentlyArchitectural Decisions: Smoothly and Consistently
Architectural Decisions: Smoothly and Consistently
 
Architectural Decisions: Smoothly and Consistently
Architectural Decisions: Smoothly and ConsistentlyArchitectural Decisions: Smoothly and Consistently
Architectural Decisions: Smoothly and Consistently
 
Dapper: the microORM that will change your life
Dapper: the microORM that will change your lifeDapper: the microORM that will change your life
Dapper: the microORM that will change your life
 
Software Architecture and Architectors: useless VS valuable
Software Architecture and Architectors: useless VS valuableSoftware Architecture and Architectors: useless VS valuable
Software Architecture and Architectors: useless VS valuable
 
Designing and Implementing Information Systems with Event Modeling, Bobby Cal...
Designing and Implementing Information Systems with Event Modeling, Bobby Cal...Designing and Implementing Information Systems with Event Modeling, Bobby Cal...
Designing and Implementing Information Systems with Event Modeling, Bobby Cal...
 
Domain Driven Design Big Picture Strategic Patterns
Domain Driven Design Big Picture Strategic PatternsDomain Driven Design Big Picture Strategic Patterns
Domain Driven Design Big Picture Strategic Patterns
 
Building Information Systems using Event Modeling (Bobby Calderwood, Evident ...
Building Information Systems using Event Modeling (Bobby Calderwood, Evident ...Building Information Systems using Event Modeling (Bobby Calderwood, Evident ...
Building Information Systems using Event Modeling (Bobby Calderwood, Evident ...
 
Tech for the Non Technical - Anatomy of an Application Stack
Tech for the Non Technical - Anatomy of an Application StackTech for the Non Technical - Anatomy of an Application Stack
Tech for the Non Technical - Anatomy of an Application Stack
 
Tasks Your Business Should Automate in 2022 That Just Make Sense
Tasks Your Business Should Automate in 2022 That Just Make SenseTasks Your Business Should Automate in 2022 That Just Make Sense
Tasks Your Business Should Automate in 2022 That Just Make Sense
 
DITA Quick Start Webinar: Defining Your Style Sheet Requirements
DITA Quick Start Webinar: Defining Your Style Sheet RequirementsDITA Quick Start Webinar: Defining Your Style Sheet Requirements
DITA Quick Start Webinar: Defining Your Style Sheet Requirements
 
Introducing systems analysis, design & development Concepts
Introducing systems analysis, design & development ConceptsIntroducing systems analysis, design & development Concepts
Introducing systems analysis, design & development Concepts
 
Multi View Constructed Right
Multi View Constructed RightMulti View Constructed Right
Multi View Constructed Right
 
SDWest2005Goetsch
SDWest2005GoetschSDWest2005Goetsch
SDWest2005Goetsch
 
Building Big Data Streaming Architectures
Building Big Data Streaming ArchitecturesBuilding Big Data Streaming Architectures
Building Big Data Streaming Architectures
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
 
An Introduction To Model  View  Controller In XPages
An Introduction To Model  View  Controller In XPagesAn Introduction To Model  View  Controller In XPages
An Introduction To Model  View  Controller In XPages
 
VidyaBhooshanMishra_CV
VidyaBhooshanMishra_CVVidyaBhooshanMishra_CV
VidyaBhooshanMishra_CV
 
Lessons Learned from a major IBM Collaboration Solutions Deployment
Lessons Learned from a major IBM Collaboration Solutions DeploymentLessons Learned from a major IBM Collaboration Solutions Deployment
Lessons Learned from a major IBM Collaboration Solutions Deployment
 

Vba Class Level 3

  • 1. VBA Level 3 By Ben Miu
  • 2. What do we know? • Creating variables • Loops • Arrays • If-Then Else • Range Objects • Dictionary Objects • Subs/Functions • UserForm
  • 3. ADO VBA • ADO stands for ActiveX Data Objects. It allows VBA to communicate with back end databases • A database back end is useful for storing information if there are more fields that Excel can handle • It serves as a centralized location for all your tables • Code is provided on next page
  • 4. ADO VBA Dim conn As New ADODB.Connection Dim cmd As New ADODB.Command Dim rs As ADODB.Recordset conn.Open "Driver={Microsoft Access Driver (*.mdb, *.accdb)};};Dbq="DailyRiskFolder & "Daily Risk.accdb;" Set cmd.ActiveConnection = conn cmd.CommandText = "insert into Pricing (PriceDate, PriceIndex, PricePrice) values(#" & strDate & "#,'" & a.Offset(0, 1).value & "','" & a.Offset(0, 2).value & "')" cmd.CommandType = adCmdText cmd.execute cmd.CommandText = "select PricePrice from Pricing where PriceIndex = '" & a.Offset(0, 1).value & "' and PriceDate = #" & finalDate & "#" cmd.CommandType = adCmdText Set rs = cmd.execute()
  • 5. FreeFile • Free file is a method which allows CSV files or TXT files to be read without having to use Excel to parse the data • Commonly used with the Split function which creates a array for the data values within the CSV • VB.NET equivalent is streamreader/streamwriter • Useful for input files which are not XLS
  • 6. FreeFile intinFile = FreeFile Open InputFile For Input As #intinFile Do Until EOF(intinFile) Line Input #intinFile, sLine Loop Close #intinFile intOutFile = FreeFile Open InputFile For Output As intOutFile For Each eline In strLine If eline <> "" Or eline <> vbCr Then Print #intOutFile, eline Next eline Close #intOutFile
  • 7. Classes in VBA • Class objects are a move towards true object orientation where you can re-use classes in other projects • New era of programming whereby your code is portable • Expert level VBA as understanding classes can be very tricky if you don’t get the basics down • A sample class on next slide
  • 8.
  • 9. Instances / Constructor / Destructor • Classes allow you to create separate worlds based on base code where they don’t interact to each other • Possibility of code conflicts is eliminated with classes and scope control is possible • No real benefits of classes in simple VBA applications. Starts to make sense as you code more and more and don’t want to repeat yourself
  • 10. Daily Risk Project • Hired to bring automation to the risk group • Daily Risk started as separate processes like partition creation, hierarchy generation and eventually was combined with reporting • Daily Risk One Button solution is the risk group’s answer to all manual processes. It’s estimated that the Daily Risk process can do at least 3-4 people’s worth of work and cut down on hundreds of man hours per month
  • 11. Evolution of Risk Needs • With more object oriented coding, there will be more robust reporting capabilities faster • For example, if someone wants a HEEN risk report or FSER risk report, and they want customization, object oriented programming base code will maintain the regular template and be able to handle new specific changes • CONST (Constants allow for all change to take place on one module)
  • 12. S&T • S&T is responsible for the development of applications to be deployed enterprise wide or to a specific group • Process involves requirements document, business analysis, project management, quality assurance, training and use case analysis • Ted Tea does the VB.NET side whereas Trevor Tilley does the C# side
  • 13. Two approaches to coding • Business user understands his own processes, codes himself • Turn around time is very fast as no requirements documentation is required • Downside to business user’s coding is if the person ever leaves then someone else must maintain the code • Could represent a operational risk. Therefore, Trevor Tilley is not a fan of VBA coders running around
  • 14. When you should automate? • The idea is that you should automate if the time it takes to code is faster than the time it is to do the process manually and/or the process is a daily process which requires many man hours • Cutting and pasting one spreadsheet to another is not a justification to write code unless that cutting and pasting is part of a bigger picture
  • 15. Breaking down to basic steps • Much of coding is negotiating with other departments to get the data in a format that you can read with macros. Makes no sense to be reading a PDF file as a PDF file comes from a XLS or DOC originally • All systems essentially have a database backend and SQL scripts can automate output from those databases • Problem is, the other group may not have the resources to give you what you want
  • 16. Moving from the small application • A small cut and paste procedure can be much much more and start to become larger and eventually replace entire processes • Bonnie part of VFA prepares the performance report which is ultimately driven by daily Sylvan output files • Don’t just automate the cut and paste, automate the entire process to just one button
  • 17. Bigger picture • Small processes to larger processes encompassing more people in your group requires you to be the one that talks to others in your group and automate their work • Many times, for example, Candy might prepare a file for Felix to deal with, then Felix passes the end result to Wenli. 3 people passing a document around modified 3 different times
  • 18. Process Map of Reporting Candy gets PDF from client and types it into system Felix inserts benchmarks Wenli creates reports
  • 19. How to think about a process • When presented with a problem, the first thing you should think about is what types of VBA are you going to use. Are you going to use a dictionary, a array, a range? Are you going to use a back end database? • Do you see this process being something that needs to be done on a regular basis? Quick garbage code vs. Robust code
  • 20. Garbage Code vs. Robust Code • Using a For loop and you counting the number of rows is considered garbage code because it isn’t going to be robust • Even using a While-Wend loop isn’t robust enough because the users can put spaces in between cells • Let’s use Excel’s UsedRange approach to assist
  • 21. How I first learnt VBA • A D- student in VB class in high school • Later when working at FK Imports, (Giftware industry) started to use VB code to assist in Crystal Reporting • Started to become an automator at BMO Middle Office • TD operations were simplified to one button clicks.
  • 22. How to learn VBA • Macro recorder is key to getting the syntax right. Don’t try to memorize the code • Change the macro recorder’s output with what you have learned • Don’t always choose the manual way over the automated way • Books and video’s don’t help a lot because many times you will have to sift through it and it doesn’t help you • Only way is to think ‘automation’ at every step of the way