SlideShare ist ein Scribd-Unternehmen logo
1 von 19
VBA Programming for Excel 
 Review Excel Objects 
 Excel Methods 
 Identifying Specific Cells 
 Review Functions for Excel 
 Custom Menus
Range Objects 
 Range(Name) 
 Name: text string 
 “B3”,”Input” 
 Offset 
 Range(“B3”).Offset(2,1) = Range(“C5”) 
 Offset numbers can be called 
MyNumber = 3 
Range(“D4”).Offset(myNumber, -1).Select 
<Results: Cursor ends up in cell C7>
Default Objects 
 ActiveCell 
 ActiveCell.Offset(0,1).Select 
 RowNum = ActiveCell.Row 
 ActiveSheet 
 ActiveSheet.Name = “Data” 
 ActiveSheet.Visible = VeryHidden 
 Selection 
 Selection.Clear
What does this code do? 
ActiveCell.Offset(Range(“B2”),-2) = [b4]/4 
4
Controlling Objects 
 Use assignment statements to change 
objects or properties 
 Different effects, similar results 
 Range(“F3”).Value = Range(“D3”).Value*15 
 Range (“F3”).Formula = “=D3*15” 
 First form enter a number – no updates! 
 Second form enters a formula
Collections 
 Worksheets 
 Worksheets(1) 
 Worksheets(“Sheet2”) 
 Columns 
Columns(“C:D”).HorizontalAlignment = xlCenter 
 Rows 
Rows(5).RowHeight = 19.5 
 Note difference between Row and 
Rows
Excel Methods 
 Record macros to define 
 Copy, Paste 
Range(“B3:D6”).Select 
Selection.Copy 
 Sort
Find a Given Cell 
 GoTo 
 Edit, Goto, Special 
 Last Cell 
 Current Region 
 {End}{Down} 
 Find first column in the last row
Look-ups 
 VLookUp(value, table, col_num, close) 
 Value: item to find 
 Table: range of data to search 
 Must be sorted by 1st column 
 Col_num: which column has data? 
 Close: true or false 
 True: select nearest match – always finds 
 False: find exact, or return #N/A
VLookUp 
Value to LookUp 
Search range 
Return column 
True: find 
closest match
Spreadsheet Functions in VBA 
 Application.WorkSheetFunction.Name(Arguments) 
Application.WorksheetFunction.Today() 
 Cell addresses must appear as ranges 
Application.WorkSheetFunction.IsNumber(Range(“B3”)) 
 Most worksheet functions have a VBA equivalent 
 Functions must be used in assignment statements 
vAns = Application.WorkSheetFunction. _ 
vLookup(Range(“A10”), Range(“A2:C8”), 3, True) 
vOut = Range(“A10”).formula & “ lives in “ vAns 
MsgBox vOut
Find() 
 VBA Function – not available on sheet 
 Expression.Find(What) 
 Expression must define a range on the 
spreadsheet 
 Returns Range location of first match 
 Expression range need not be sorted 
 If no match is found, it returns Nothing
Find( ) Function 
Range(“C10”).Value = _ 
Range(“A2:A8”).Find(“Gene”).Offset(0,2).Value 
Looks in cells A2:A8 for 
“Gene”, 
returns [A5] 
Offsets 2 cells right from [A5] 
returns [C5] 
Finds the value in [C5] = 58 
Puts the value 58 in [C10]
User Defined Functions 
 You can write your own custom functions 
 Decide what information will be passed in 
(Arguments) 
 Decide what value will be returned 
 Decide how VBA will use the arguments to 
calculate the returned value 
 Example: Determine employee bunuses 
 Argument: Amount of sales 
 Return value: Bonus amount 
 Bonus = 2% if Sales > $50,000
User-defined Functions 
 Form: Function Name(Arguments) 
 Unlike Sub – the name of the function 
must be repeated in the code 
Function Bonus(Sales) 
If Sales > 50000 Then 
Bonus = Sales * 0.02 
Else 
Bonus = 0 
End If 
End Function
Using Custom Functions 
 Functions can be called from another sub 
vSales = Range(“B3”).Value 
vBonus = Bonus(vSales) 
Range(“C3”).Value = vBonus 
 Functions can be used in the spreadsheet 
 Use Function Generator [fx] 
 Look under “User-defined” 
 Place cursor in [C3], write: 
=Bonus(B3) 
 Note how the results differ! 
 See VBAFunctions.xls in the handouts
Custom Menus 
 Define Variables 
 Use Set to define contents 
Dim myButton As CommandBarButton 
Set myButton = CommandBars("Worksheet Menu Bar")_ 
.Controls("Tools").Controls.Add 
With myButton 
.Caption = "Say Hi" 
.MoveBefore:=4 
.OnAction = "SayHi" 
.FaceId = 2174 
End With 
Caption: Words in menu list 
MoveBefore: Position in list 
OnAction: Macro to call 
FaceID: Icon to display
Removing Menu Items 
 Search the existing buttons 
 Remove identified items 
For Each Item In CommandBars("Worksheet Menu Bar")_ 
.Controls("Tools").Controls 
If Item.Caption = "Say Hi" Then 
Item.Delete 
Exit For 
End If 
Next Item
Activating Custom Menus 
 Menu code goes in a macro 
 Macro must be run to make menu 
appear or disappear 
 Use WorkBook_Open to add menues 
 Use WorkBook_BeforeClose to remove 
 See SayHi.xls in the handouts

Weitere ähnliche Inhalte

Was ist angesagt?

Functions oracle (pl/sql)
Functions oracle (pl/sql)Functions oracle (pl/sql)
Functions oracle (pl/sql)harman kaur
 
Implementation of EAV pattern for ActiveRecord models
Implementation of EAV pattern for ActiveRecord modelsImplementation of EAV pattern for ActiveRecord models
Implementation of EAV pattern for ActiveRecord modelsKostyantyn Stepanyuk
 
Writing Group Functions - DBMS
Writing Group Functions - DBMSWriting Group Functions - DBMS
Writing Group Functions - DBMSSolaiman Hridoy
 
EAV Sytem- Magento EAV Model
EAV Sytem- Magento EAV ModelEAV Sytem- Magento EAV Model
EAV Sytem- Magento EAV ModelKhoa Truong Dinh
 
Introduction To Oracle Sql
Introduction To Oracle SqlIntroduction To Oracle Sql
Introduction To Oracle SqlAhmed Yaseen
 
Nokia Asha App Development - Part 2
Nokia Asha App Development - Part 2Nokia Asha App Development - Part 2
Nokia Asha App Development - Part 2Marlon Luz
 
Using single row functions to customize output
Using single row functions to customize outputUsing single row functions to customize output
Using single row functions to customize outputSyed Zaid Irshad
 
MSc COMPUTER APPLICATION
MSc COMPUTER APPLICATIONMSc COMPUTER APPLICATION
MSc COMPUTER APPLICATIONMugdhaSharma11
 
Excel formulas-manual
Excel formulas-manualExcel formulas-manual
Excel formulas-manualsrv1972
 
Entity Attribute Value (Eav)
Entity   Attribute   Value (Eav)Entity   Attribute   Value (Eav)
Entity Attribute Value (Eav)Tâm
 
Geometria analitica con geogebra secme 22589
Geometria analitica con geogebra secme 22589Geometria analitica con geogebra secme 22589
Geometria analitica con geogebra secme 22589EmilioEscalanteHerna
 
Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Achmad Solichin
 

Was ist angesagt? (20)

Functions oracle (pl/sql)
Functions oracle (pl/sql)Functions oracle (pl/sql)
Functions oracle (pl/sql)
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
 
Implementation of EAV pattern for ActiveRecord models
Implementation of EAV pattern for ActiveRecord modelsImplementation of EAV pattern for ActiveRecord models
Implementation of EAV pattern for ActiveRecord models
 
Writing Group Functions - DBMS
Writing Group Functions - DBMSWriting Group Functions - DBMS
Writing Group Functions - DBMS
 
EAV Sytem- Magento EAV Model
EAV Sytem- Magento EAV ModelEAV Sytem- Magento EAV Model
EAV Sytem- Magento EAV Model
 
Les06
Les06Les06
Les06
 
Introduction To Oracle Sql
Introduction To Oracle SqlIntroduction To Oracle Sql
Introduction To Oracle Sql
 
Python Programming
Python Programming Python Programming
Python Programming
 
Nokia Asha App Development - Part 2
Nokia Asha App Development - Part 2Nokia Asha App Development - Part 2
Nokia Asha App Development - Part 2
 
Using single row functions to customize output
Using single row functions to customize outputUsing single row functions to customize output
Using single row functions to customize output
 
Get more from excel
Get more from excelGet more from excel
Get more from excel
 
MSc COMPUTER APPLICATION
MSc COMPUTER APPLICATIONMSc COMPUTER APPLICATION
MSc COMPUTER APPLICATION
 
Sql functions
Sql functionsSql functions
Sql functions
 
Excel formulas-manual
Excel formulas-manualExcel formulas-manual
Excel formulas-manual
 
Les07
Les07Les07
Les07
 
Entity Attribute Value (Eav)
Entity   Attribute   Value (Eav)Entity   Attribute   Value (Eav)
Entity Attribute Value (Eav)
 
Geometria analitica con geogebra secme 22589
Geometria analitica con geogebra secme 22589Geometria analitica con geogebra secme 22589
Geometria analitica con geogebra secme 22589
 
Practica dos
Practica dosPractica dos
Practica dos
 
Dbms lab questions
Dbms lab questionsDbms lab questions
Dbms lab questions
 
Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Les02 (restricting and sorting data)
Les02 (restricting and sorting data)
 

Andere mochten auch

Materi Pemfaktoran Persamaan Kuadrat
Materi Pemfaktoran Persamaan KuadratMateri Pemfaktoran Persamaan Kuadrat
Materi Pemfaktoran Persamaan KuadratIndah Lestari
 
Matematik Tambahan: Persamaan kuadratik
Matematik Tambahan: Persamaan kuadratikMatematik Tambahan: Persamaan kuadratik
Matematik Tambahan: Persamaan kuadratikCikgu Marzuqi
 
PPT Puzzle Milus 1
PPT Puzzle Milus 1PPT Puzzle Milus 1
PPT Puzzle Milus 1Indah Sari
 
Ppt kpk dan fpb.pptx vvvv
Ppt kpk dan fpb.pptx vvvvPpt kpk dan fpb.pptx vvvv
Ppt kpk dan fpb.pptx vvvvRela Tusriyanto
 
Dokumen.tips kelas 7-matematika-bab-1-bilangan-bulat (2)
Dokumen.tips kelas 7-matematika-bab-1-bilangan-bulat (2)Dokumen.tips kelas 7-matematika-bab-1-bilangan-bulat (2)
Dokumen.tips kelas 7-matematika-bab-1-bilangan-bulat (2)Antoni Antoni
 
Bahan Ajar Bilangan Bulat
Bahan Ajar Bilangan BulatBahan Ajar Bilangan Bulat
Bahan Ajar Bilangan BulatDara-71
 
Soal UTS TIK kelas 7 semester 2 hardware
Soal UTS TIK kelas 7 semester 2 hardwareSoal UTS TIK kelas 7 semester 2 hardware
Soal UTS TIK kelas 7 semester 2 hardwareCitra Aero
 
SOAL UAS GANJIL MATEMATIKA SMP KELAS VII 2015/2016
SOAL UAS GANJIL MATEMATIKA SMP KELAS VII 2015/2016SOAL UAS GANJIL MATEMATIKA SMP KELAS VII 2015/2016
SOAL UAS GANJIL MATEMATIKA SMP KELAS VII 2015/2016Budi Haryono
 
Soal unas &amp; pembahasan
Soal unas  &amp; pembahasanSoal unas  &amp; pembahasan
Soal unas &amp; pembahasanAnnisa Rahmaw
 

Andere mochten auch (14)

Materi Pemfaktoran Persamaan Kuadrat
Materi Pemfaktoran Persamaan KuadratMateri Pemfaktoran Persamaan Kuadrat
Materi Pemfaktoran Persamaan Kuadrat
 
SOAL BILANGAN BULAT
SOAL BILANGAN BULATSOAL BILANGAN BULAT
SOAL BILANGAN BULAT
 
1. bab bilangan bulat
1. bab bilangan bulat1. bab bilangan bulat
1. bab bilangan bulat
 
Matematik Tambahan: Persamaan kuadratik
Matematik Tambahan: Persamaan kuadratikMatematik Tambahan: Persamaan kuadratik
Matematik Tambahan: Persamaan kuadratik
 
BAB I : Operasi Hitung Bilangan
BAB I : Operasi Hitung BilanganBAB I : Operasi Hitung Bilangan
BAB I : Operasi Hitung Bilangan
 
PPT Puzzle Milus 1
PPT Puzzle Milus 1PPT Puzzle Milus 1
PPT Puzzle Milus 1
 
Ppt kpk dan fpb.pptx vvvv
Ppt kpk dan fpb.pptx vvvvPpt kpk dan fpb.pptx vvvv
Ppt kpk dan fpb.pptx vvvv
 
Dokumen.tips kelas 7-matematika-bab-1-bilangan-bulat (2)
Dokumen.tips kelas 7-matematika-bab-1-bilangan-bulat (2)Dokumen.tips kelas 7-matematika-bab-1-bilangan-bulat (2)
Dokumen.tips kelas 7-matematika-bab-1-bilangan-bulat (2)
 
Bahan Ajar Bilangan Bulat
Bahan Ajar Bilangan BulatBahan Ajar Bilangan Bulat
Bahan Ajar Bilangan Bulat
 
Operasi hitung bilangan bulat
Operasi hitung bilangan bulatOperasi hitung bilangan bulat
Operasi hitung bilangan bulat
 
Soal mtk
Soal mtkSoal mtk
Soal mtk
 
Soal UTS TIK kelas 7 semester 2 hardware
Soal UTS TIK kelas 7 semester 2 hardwareSoal UTS TIK kelas 7 semester 2 hardware
Soal UTS TIK kelas 7 semester 2 hardware
 
SOAL UAS GANJIL MATEMATIKA SMP KELAS VII 2015/2016
SOAL UAS GANJIL MATEMATIKA SMP KELAS VII 2015/2016SOAL UAS GANJIL MATEMATIKA SMP KELAS VII 2015/2016
SOAL UAS GANJIL MATEMATIKA SMP KELAS VII 2015/2016
 
Soal unas &amp; pembahasan
Soal unas  &amp; pembahasanSoal unas  &amp; pembahasan
Soal unas &amp; pembahasan
 

Ähnlich wie Excell vba

Ähnlich wie Excell vba (20)

belajar VBA
belajar VBAbelajar VBA
belajar VBA
 
Working with shapes
Working with shapesWorking with shapes
Working with shapes
 
Introduction to DAX Language
Introduction to DAX LanguageIntroduction to DAX Language
Introduction to DAX Language
 
MA3696 Lecture 9
MA3696 Lecture 9MA3696 Lecture 9
MA3696 Lecture 9
 
Excel 2007 Unit H
Excel 2007 Unit HExcel 2007 Unit H
Excel 2007 Unit H
 
OBIEE 12c Advanced Analytic Functions
OBIEE 12c Advanced Analytic FunctionsOBIEE 12c Advanced Analytic Functions
OBIEE 12c Advanced Analytic Functions
 
Ms excel
Ms excelMs excel
Ms excel
 
1.2 Zep Excel.pptx
1.2 Zep Excel.pptx1.2 Zep Excel.pptx
1.2 Zep Excel.pptx
 
Excel.useful fns
Excel.useful fnsExcel.useful fns
Excel.useful fns
 
Excel Macros and VBA Demo.docx
Excel Macros and VBA Demo.docxExcel Macros and VBA Demo.docx
Excel Macros and VBA Demo.docx
 
Excel Macros and VBA Demo.docx
Excel Macros and VBA Demo.docxExcel Macros and VBA Demo.docx
Excel Macros and VBA Demo.docx
 
Python Lecture 4
Python Lecture 4Python Lecture 4
Python Lecture 4
 
Tutorials on Macro
Tutorials on MacroTutorials on Macro
Tutorials on Macro
 
9 - Advanced Functions in MS Excel.pptx
9 - Advanced Functions in MS Excel.pptx9 - Advanced Functions in MS Excel.pptx
9 - Advanced Functions in MS Excel.pptx
 
Mdx Basics
Mdx BasicsMdx Basics
Mdx Basics
 
What are Tableau Functions? Edureka
What are Tableau Functions? EdurekaWhat are Tableau Functions? Edureka
What are Tableau Functions? Edureka
 
6 Function Built In (Excel 2019 dn 365).pptx
6 Function Built In (Excel 2019 dn 365).pptx6 Function Built In (Excel 2019 dn 365).pptx
6 Function Built In (Excel 2019 dn 365).pptx
 
Excel functions, formulas, Prepared by singh institute RK Puram
Excel functions, formulas, Prepared by singh institute RK PuramExcel functions, formulas, Prepared by singh institute RK Puram
Excel functions, formulas, Prepared by singh institute RK Puram
 
C# excel bar chart
C# excel bar chartC# excel bar chart
C# excel bar chart
 
Project advance itt
Project advance ittProject advance itt
Project advance itt
 

Kürzlich hochgeladen

Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 

Kürzlich hochgeladen (20)

Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 

Excell vba

  • 1. VBA Programming for Excel  Review Excel Objects  Excel Methods  Identifying Specific Cells  Review Functions for Excel  Custom Menus
  • 2. Range Objects  Range(Name)  Name: text string  “B3”,”Input”  Offset  Range(“B3”).Offset(2,1) = Range(“C5”)  Offset numbers can be called MyNumber = 3 Range(“D4”).Offset(myNumber, -1).Select <Results: Cursor ends up in cell C7>
  • 3. Default Objects  ActiveCell  ActiveCell.Offset(0,1).Select  RowNum = ActiveCell.Row  ActiveSheet  ActiveSheet.Name = “Data”  ActiveSheet.Visible = VeryHidden  Selection  Selection.Clear
  • 4. What does this code do? ActiveCell.Offset(Range(“B2”),-2) = [b4]/4 4
  • 5. Controlling Objects  Use assignment statements to change objects or properties  Different effects, similar results  Range(“F3”).Value = Range(“D3”).Value*15  Range (“F3”).Formula = “=D3*15”  First form enter a number – no updates!  Second form enters a formula
  • 6. Collections  Worksheets  Worksheets(1)  Worksheets(“Sheet2”)  Columns Columns(“C:D”).HorizontalAlignment = xlCenter  Rows Rows(5).RowHeight = 19.5  Note difference between Row and Rows
  • 7. Excel Methods  Record macros to define  Copy, Paste Range(“B3:D6”).Select Selection.Copy  Sort
  • 8. Find a Given Cell  GoTo  Edit, Goto, Special  Last Cell  Current Region  {End}{Down}  Find first column in the last row
  • 9. Look-ups  VLookUp(value, table, col_num, close)  Value: item to find  Table: range of data to search  Must be sorted by 1st column  Col_num: which column has data?  Close: true or false  True: select nearest match – always finds  False: find exact, or return #N/A
  • 10. VLookUp Value to LookUp Search range Return column True: find closest match
  • 11. Spreadsheet Functions in VBA  Application.WorkSheetFunction.Name(Arguments) Application.WorksheetFunction.Today()  Cell addresses must appear as ranges Application.WorkSheetFunction.IsNumber(Range(“B3”))  Most worksheet functions have a VBA equivalent  Functions must be used in assignment statements vAns = Application.WorkSheetFunction. _ vLookup(Range(“A10”), Range(“A2:C8”), 3, True) vOut = Range(“A10”).formula & “ lives in “ vAns MsgBox vOut
  • 12. Find()  VBA Function – not available on sheet  Expression.Find(What)  Expression must define a range on the spreadsheet  Returns Range location of first match  Expression range need not be sorted  If no match is found, it returns Nothing
  • 13. Find( ) Function Range(“C10”).Value = _ Range(“A2:A8”).Find(“Gene”).Offset(0,2).Value Looks in cells A2:A8 for “Gene”, returns [A5] Offsets 2 cells right from [A5] returns [C5] Finds the value in [C5] = 58 Puts the value 58 in [C10]
  • 14. User Defined Functions  You can write your own custom functions  Decide what information will be passed in (Arguments)  Decide what value will be returned  Decide how VBA will use the arguments to calculate the returned value  Example: Determine employee bunuses  Argument: Amount of sales  Return value: Bonus amount  Bonus = 2% if Sales > $50,000
  • 15. User-defined Functions  Form: Function Name(Arguments)  Unlike Sub – the name of the function must be repeated in the code Function Bonus(Sales) If Sales > 50000 Then Bonus = Sales * 0.02 Else Bonus = 0 End If End Function
  • 16. Using Custom Functions  Functions can be called from another sub vSales = Range(“B3”).Value vBonus = Bonus(vSales) Range(“C3”).Value = vBonus  Functions can be used in the spreadsheet  Use Function Generator [fx]  Look under “User-defined”  Place cursor in [C3], write: =Bonus(B3)  Note how the results differ!  See VBAFunctions.xls in the handouts
  • 17. Custom Menus  Define Variables  Use Set to define contents Dim myButton As CommandBarButton Set myButton = CommandBars("Worksheet Menu Bar")_ .Controls("Tools").Controls.Add With myButton .Caption = "Say Hi" .MoveBefore:=4 .OnAction = "SayHi" .FaceId = 2174 End With Caption: Words in menu list MoveBefore: Position in list OnAction: Macro to call FaceID: Icon to display
  • 18. Removing Menu Items  Search the existing buttons  Remove identified items For Each Item In CommandBars("Worksheet Menu Bar")_ .Controls("Tools").Controls If Item.Caption = "Say Hi" Then Item.Delete Exit For End If Next Item
  • 19. Activating Custom Menus  Menu code goes in a macro  Macro must be run to make menu appear or disappear  Use WorkBook_Open to add menues  Use WorkBook_BeforeClose to remove  See SayHi.xls in the handouts