狠狠撸

狠狠撸Share a Scribd company logo
Macros Basic elementary knowledge
1
Abhirampobbisetty
1. Coding Tip 1
Always key in your code in lower case letters. If the spelling is right, the necessary letters will be
capitalized. If no letter gets capitalized.... check your spelling.
Exercise1-1
Step 1: Open a new workbook in Excel and use the ALT/F11 keys to go to the visual basic editor.
Step 2: In the code window of any of the sheets copy/paste the following macro:
Sub protest()
activecel.cop
End Sub
Notice that there are no capital letters in activecel.cop because both words are misspelled.
Step 3: Add a second "l" to "activecell" and an "y" to "copy" and then click "Enter". The sentence
now reads: Activecell.Copy with a capital "A" and a capital "C" because both words are spelled
correctly.
You now understand that significant letters are capitalised in each correctly spelled VBA word
when you move away from the line.
Step 5: Close Excel without saving anything
Macros Basic elementary knowledge
2
Abhirampobbisetty
2. Managing VBA Coding Errors
The Visual Basic Editor will help you avoid errors in coding in many different ways. You will not
have to wait at the end to be told that there is something wrong with your macro.
SyntaxErrors
The VBE will also tell you that there is a syntax error in what you have just written by making the
font red and showing you a message box.
Exercise1
Step 1: Open a new workbook in Excel and use the ALT/F11 keys to go to the visual basic editor
(VBE).
Step 2: In the code window of any of the sheet copy/paste the following line of
code:Range(A1").Select and click "Enter".
You get the following message box telling you that you are missing a "list separator". Look for the
error before the segment highlighted in blue. We can deduce that VBA is talking about the
missing quotation mark.
Step 3: Click on the "OK" button.
Step 4: Add the missing quotation mark, use the mouse to move the cursor to the end of the
sentence and click "Enter". The font is black meaning that everything is correct.
Macros Basic elementary knowledge
3
Abhirampobbisetty
Exercise2
Step 1: In the code window that you have used for exercise 1 copy/paste the following line of
code:
Range("A1".Select and click "Enter".
You get the following message box telling you that you are missing a "list separator". Look for the
error before the segment highlighted in blue. We can deduce that VBE is talking about the
missing parenthesis. Both the quotation marks in the exercise above and the parenthesis in this
exercise are considered as "list separator" by the VBE.
Step 2: Click on the "OK" button.
Step 3: Add the missing parenthesis, use the mouse to move the cursor to the end of the
sentence and click "Enter". The font is black meaning that everything is correct.
Step 4: Close Excel without saving anything
ScreenUpdating (Application.ScreenUpdating)
When you do not want to see your screen follow the actions of your VBA procedure (macro), you
start and end your code with the following sentences:
Application.ScreenUpdating = False
Then at the end:
Application.ScreenUpdating = True
Macros Basic elementary knowledge
4
Abhirampobbisetty
For reference :
Step 1: ALT+F11
Step 2: Copy the following macro in the code window of any sheet. As you can read: starting in
cell A1 a value of "99" will be entered in the selected cell then the cursor will move one cell
down to enter "99", repeat the process until the row number of the selected cell is 3000 and
come back to cell A1.
Sub testLesson13b1()
Range("A1").Select
Do Until Selection.Row = 3000
Selection.Value = 99
Selection.Offset(1, 0).Select
Loop
Range("A1").Select
End Sub
Step 3: Run the macro from Excel as you did with the previous one.
Step 4: Remove all the "99" from the cells
Step 5: Copy the following macro in the code window of a new workbook and run it. Two lines of
code have been added to the previous macro to prevent all the steps of the action to be seen on
the screen.
Sub testLesson13b2()
Application.ScreenUpdating = False
Range("A1").Select
Do Until Selection.Row = 3000
Selection.Value = 99
Selection.Offset(1, 0).Select
Loop
Range("A1").Select
Macros Basic elementary knowledge
5
Abhirampobbisetty
Application.ScreenUpdating = True
End Sub
Step 6: Run the macro from Excel as you did with the previous one. You will see a blank sheet, no
movement whatsoever and then a sheet where cells A1 to A3000 are equal to "99".
Sometimes you or the users might want to see the action. Some other times you or the user do
not want to see the action. It is up to you to use the sentence or not.
You can even use the pair of sentences (as below) anywhere within a long macro to refresh the
screen at significant points in the process. With the pair of sentences you call for a refreshment
withApplication.ScreenUpdating = True and then interrupt the refreshment process until the
next refreshment with Application.ScreenUpdating = False. Before the end of the macro you will
use a finalApplication.ScreenUpdating = True.
The pair of refreshing sentences:
Application.ScreenUpdating = True
Application.ScreenUpdating = False
Step 7: Close the workbook without saving anything
Ad

Recommended

Theatre Booking System Lesson 3
Theatre Booking System Lesson 3
mrbelshaw
?
Getting started with Microsoft Excel Macros
Getting started with Microsoft Excel Macros
Nick Weisenberger
?
How to apply a formula and macro in excel......by irfan afzal
How to apply a formula and macro in excel......by irfan afzal
1995786
?
Microsoft Office 2003 Creating Macros
Microsoft Office 2003 Creating Macros
S Burks
?
Spreadsheet Errors
Spreadsheet Errors
EranSchitzer
?
Learn Excel Macro
Learn Excel Macro
AbhisheK Kumar Rajoria
?
Bookkeping tool user guide
Bookkeping tool user guide
Nazmul Huda
?
Question 2B
Question 2B
Stefanie Wong
?
Using Microsoft Excel2 Calculations
Using Microsoft Excel2 Calculations
Jack Frost
?
Ex13 lesson02
Ex13 lesson02
sadejoseph
?
Formulae
Formulae
thangamalar94
?
Grade 5 Computer
Grade 5 Computer
Joel Linquico
?
Grade 6 Computer
Grade 6 Computer
Joel Linquico
?
Mail innovations
Mail innovations
imauld
?
Excel
Excel
Brian Lowery
?
Sequence diagram
Sequence diagram
Abhinav Bhatnagar
?
Lesson9 Working With Basic Functions
Lesson9 Working With Basic Functions
guevarra_2000
?
Excel 2007 slide enter formulas
Excel 2007 slide enter formulas
rezaulslide
?
Assignment1 min function[1] powerpoint
Assignment1 min function[1] powerpoint
s0kkerstar7
?
Ma3696 Lecture 1
Ma3696 Lecture 1
Brunel University
?
E_Pick in TM1
E_Pick in TM1
Sonum International
?
Excel.02
Excel.02
MusTufa Nullwala
?
Top 10 excel tips
Top 10 excel tips
Excel Prodigy Training & Consultancy Private Limited
?
Microsoft Excel 2013 Basics course
Microsoft Excel 2013 Basics course
Muhammad Shakir
?
Advance MS Excel
Advance MS Excel
acute23
?
Lesson9 working with basic functions
Lesson9 working with basic functions
ricsanmae
?
Acct120 Class #14 Microsoft Excel Features
Acct120 Class #14 Microsoft Excel Features
Adjem
?
P1 presentacio?n plan de viabilidad pop&mitas
Ines Sofia Ortiz
?
Vampire cinema
Vampire cinema
nerea-rp
?
Advocacy: Anti-bullying
Advocacy: Anti-bullying
MandyZhou
?

More Related Content

What's hot (19)

Using Microsoft Excel2 Calculations
Using Microsoft Excel2 Calculations
Jack Frost
?
Ex13 lesson02
Ex13 lesson02
sadejoseph
?
Formulae
Formulae
thangamalar94
?
Grade 5 Computer
Grade 5 Computer
Joel Linquico
?
Grade 6 Computer
Grade 6 Computer
Joel Linquico
?
Mail innovations
Mail innovations
imauld
?
Excel
Excel
Brian Lowery
?
Sequence diagram
Sequence diagram
Abhinav Bhatnagar
?
Lesson9 Working With Basic Functions
Lesson9 Working With Basic Functions
guevarra_2000
?
Excel 2007 slide enter formulas
Excel 2007 slide enter formulas
rezaulslide
?
Assignment1 min function[1] powerpoint
Assignment1 min function[1] powerpoint
s0kkerstar7
?
Ma3696 Lecture 1
Ma3696 Lecture 1
Brunel University
?
E_Pick in TM1
E_Pick in TM1
Sonum International
?
Excel.02
Excel.02
MusTufa Nullwala
?
Top 10 excel tips
Top 10 excel tips
Excel Prodigy Training & Consultancy Private Limited
?
Microsoft Excel 2013 Basics course
Microsoft Excel 2013 Basics course
Muhammad Shakir
?
Advance MS Excel
Advance MS Excel
acute23
?
Lesson9 working with basic functions
Lesson9 working with basic functions
ricsanmae
?
Acct120 Class #14 Microsoft Excel Features
Acct120 Class #14 Microsoft Excel Features
Adjem
?

Viewers also liked (13)

P1 presentacio?n plan de viabilidad pop&mitas
Ines Sofia Ortiz
?
Vampire cinema
Vampire cinema
nerea-rp
?
Advocacy: Anti-bullying
Advocacy: Anti-bullying
MandyZhou
?
Jessica May - Occupational identity
Jessica May - Occupational identity
Jess May
?
創業管理:兼職創業者完整課程 Week 2
創業管理:兼職創業者完整課程 Week 2
Johnson Health Tech. Co., Ltd.
?
Grading visible learners nagel-slides visible learning institute san diego
Grading visible learners nagel-slides visible learning institute san diego
Nags4444
?
1
1
Zulaa Angel
?
創業管理:兼職創業者完整課程 week7
創業管理:兼職創業者完整課程 week7
Johnson Health Tech. Co., Ltd.
?
創業管理:兼職創業者完整課程 Week 4
創業管理:兼職創業者完整課程 Week 4
Johnson Health Tech. Co., Ltd.
?
Album arts analysed examples
Album arts analysed examples
mo_usman
?
Trabajo de animales de ingles
Trabajo de animales de ingles
José Antonio Morente Ruiz
?
Java file
Java file
Divya Nain
?
Wishclubteamusa_russia
Wishclubteamusa_russia
wishclubteamusa
?
P1 presentacio?n plan de viabilidad pop&mitas
Ines Sofia Ortiz
?
Vampire cinema
Vampire cinema
nerea-rp
?
Advocacy: Anti-bullying
Advocacy: Anti-bullying
MandyZhou
?
Jessica May - Occupational identity
Jessica May - Occupational identity
Jess May
?
Grading visible learners nagel-slides visible learning institute san diego
Grading visible learners nagel-slides visible learning institute san diego
Nags4444
?
Album arts analysed examples
Album arts analysed examples
mo_usman
?
Ad

Similar to Macros (20)

Excel vba
Excel vba
Almeda Asuncion
?
???????????????????
???????????????????
banpotza
?
VISUAL
VISUAL
emi kenye
?
Excel Tutorials - Deleting the Empty Rows
Excel Tutorials - Deleting the Empty Rows
Merve Nur Ta?
?
Basic Functions - Excel 2013 Tutorial
Basic Functions - Excel 2013 Tutorial
SpreadsheetTrainer
?
Excel Basics presentation for every working professionals
Excel Basics presentation for every working professionals
KapilKumarJha
?
Excel Formulas Functions
Excel Formulas Functions
simply_coool
?
A Quick Simple MS Excel Macro
A Quick Simple MS Excel Macro
Pranav Ghode
?
Vba 2 (students copy)
Vba 2 (students copy)
Sherwin Keith Rivera
?
Excel Formulas Functions 2007
Excel Formulas Functions 2007
simply_coool
?
Excel Vba Basic Tutorial 1
Excel Vba Basic Tutorial 1
rupeshkanu
?
Autocad excel vba
Autocad excel vba
rjg_vijay
?
Tutorials on Macro
Tutorials on Macro
Anurag Deb
?
Ms excel 2007 tutorial
Ms excel 2007 tutorial
jks2010
?
Mfc programming tutorial automation step by-step
Mfc programming tutorial automation step by-step
namtranvanpt
?
Ms Office Lecture By Rashid Sazzad .pptx
Ms Office Lecture By Rashid Sazzad .pptx
RashidSazzad
?
Forgot Password to Unprotect Excel Sheet in Two Cases
Forgot Password to Unprotect Excel Sheet in Two Cases
Aadewea
?
Microsoft excel part 1
Microsoft excel part 1
Hoboken Public Library
?
Excel VBA.pptx
Excel VBA.pptx
GiyaShefin
?
Vbabook ed2
Vbabook ed2
NilsonVallecillo
?
???????????????????
???????????????????
banpotza
?
Excel Tutorials - Deleting the Empty Rows
Excel Tutorials - Deleting the Empty Rows
Merve Nur Ta?
?
Basic Functions - Excel 2013 Tutorial
Basic Functions - Excel 2013 Tutorial
SpreadsheetTrainer
?
Excel Basics presentation for every working professionals
Excel Basics presentation for every working professionals
KapilKumarJha
?
Excel Formulas Functions
Excel Formulas Functions
simply_coool
?
A Quick Simple MS Excel Macro
A Quick Simple MS Excel Macro
Pranav Ghode
?
Excel Formulas Functions 2007
Excel Formulas Functions 2007
simply_coool
?
Excel Vba Basic Tutorial 1
Excel Vba Basic Tutorial 1
rupeshkanu
?
Autocad excel vba
Autocad excel vba
rjg_vijay
?
Tutorials on Macro
Tutorials on Macro
Anurag Deb
?
Ms excel 2007 tutorial
Ms excel 2007 tutorial
jks2010
?
Mfc programming tutorial automation step by-step
Mfc programming tutorial automation step by-step
namtranvanpt
?
Ms Office Lecture By Rashid Sazzad .pptx
Ms Office Lecture By Rashid Sazzad .pptx
RashidSazzad
?
Forgot Password to Unprotect Excel Sheet in Two Cases
Forgot Password to Unprotect Excel Sheet in Two Cases
Aadewea
?
Ad

Macros

  • 1. Macros Basic elementary knowledge 1 Abhirampobbisetty 1. Coding Tip 1 Always key in your code in lower case letters. If the spelling is right, the necessary letters will be capitalized. If no letter gets capitalized.... check your spelling. Exercise1-1 Step 1: Open a new workbook in Excel and use the ALT/F11 keys to go to the visual basic editor. Step 2: In the code window of any of the sheets copy/paste the following macro: Sub protest() activecel.cop End Sub Notice that there are no capital letters in activecel.cop because both words are misspelled. Step 3: Add a second "l" to "activecell" and an "y" to "copy" and then click "Enter". The sentence now reads: Activecell.Copy with a capital "A" and a capital "C" because both words are spelled correctly. You now understand that significant letters are capitalised in each correctly spelled VBA word when you move away from the line. Step 5: Close Excel without saving anything
  • 2. Macros Basic elementary knowledge 2 Abhirampobbisetty 2. Managing VBA Coding Errors The Visual Basic Editor will help you avoid errors in coding in many different ways. You will not have to wait at the end to be told that there is something wrong with your macro. SyntaxErrors The VBE will also tell you that there is a syntax error in what you have just written by making the font red and showing you a message box. Exercise1 Step 1: Open a new workbook in Excel and use the ALT/F11 keys to go to the visual basic editor (VBE). Step 2: In the code window of any of the sheet copy/paste the following line of code:Range(A1").Select and click "Enter". You get the following message box telling you that you are missing a "list separator". Look for the error before the segment highlighted in blue. We can deduce that VBA is talking about the missing quotation mark. Step 3: Click on the "OK" button. Step 4: Add the missing quotation mark, use the mouse to move the cursor to the end of the sentence and click "Enter". The font is black meaning that everything is correct.
  • 3. Macros Basic elementary knowledge 3 Abhirampobbisetty Exercise2 Step 1: In the code window that you have used for exercise 1 copy/paste the following line of code: Range("A1".Select and click "Enter". You get the following message box telling you that you are missing a "list separator". Look for the error before the segment highlighted in blue. We can deduce that VBE is talking about the missing parenthesis. Both the quotation marks in the exercise above and the parenthesis in this exercise are considered as "list separator" by the VBE. Step 2: Click on the "OK" button. Step 3: Add the missing parenthesis, use the mouse to move the cursor to the end of the sentence and click "Enter". The font is black meaning that everything is correct. Step 4: Close Excel without saving anything ScreenUpdating (Application.ScreenUpdating) When you do not want to see your screen follow the actions of your VBA procedure (macro), you start and end your code with the following sentences: Application.ScreenUpdating = False Then at the end: Application.ScreenUpdating = True
  • 4. Macros Basic elementary knowledge 4 Abhirampobbisetty For reference : Step 1: ALT+F11 Step 2: Copy the following macro in the code window of any sheet. As you can read: starting in cell A1 a value of "99" will be entered in the selected cell then the cursor will move one cell down to enter "99", repeat the process until the row number of the selected cell is 3000 and come back to cell A1. Sub testLesson13b1() Range("A1").Select Do Until Selection.Row = 3000 Selection.Value = 99 Selection.Offset(1, 0).Select Loop Range("A1").Select End Sub Step 3: Run the macro from Excel as you did with the previous one. Step 4: Remove all the "99" from the cells Step 5: Copy the following macro in the code window of a new workbook and run it. Two lines of code have been added to the previous macro to prevent all the steps of the action to be seen on the screen. Sub testLesson13b2() Application.ScreenUpdating = False Range("A1").Select Do Until Selection.Row = 3000 Selection.Value = 99 Selection.Offset(1, 0).Select Loop Range("A1").Select
  • 5. Macros Basic elementary knowledge 5 Abhirampobbisetty Application.ScreenUpdating = True End Sub Step 6: Run the macro from Excel as you did with the previous one. You will see a blank sheet, no movement whatsoever and then a sheet where cells A1 to A3000 are equal to "99". Sometimes you or the users might want to see the action. Some other times you or the user do not want to see the action. It is up to you to use the sentence or not. You can even use the pair of sentences (as below) anywhere within a long macro to refresh the screen at significant points in the process. With the pair of sentences you call for a refreshment withApplication.ScreenUpdating = True and then interrupt the refreshment process until the next refreshment with Application.ScreenUpdating = False. Before the end of the macro you will use a finalApplication.ScreenUpdating = True. The pair of refreshing sentences: Application.ScreenUpdating = True Application.ScreenUpdating = False Step 7: Close the workbook without saving anything