Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
38 views
VBA Guide Sheet
Uploaded by
Mostafa Abd Elalem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save VBA Guide Sheet For Later
Download
Save
Save VBA Guide Sheet For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
38 views
VBA Guide Sheet
Uploaded by
Mostafa Abd Elalem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save VBA Guide Sheet For Later
Carousel Previous
Carousel Next
Save
Save VBA Guide Sheet For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 7
Search
Fullscreen
Excel Macros — Quick Guide to VBA Coding Contents Adding a Button to an Excel Spreadshect.... Variable Types in VBA IF Statements IN VBA... Strings of Text in VBA Programing Loops in VBA... Cell Properti Record Macro. Addi a Button to an Excel Spreadsheet Locate the Controls panel on the Developer toolbar, and then click the Insert item. From the Insert menu, click the first item, which is a button: Now move your mouse to your spreadsheet. Hold down your Ce mouse and draw out a 5 rectangular button. As soon as On ZS © you let go of the left mouse O45 wl button you'll see the Assign ActiveX Controls Macro dialogue box appear: ofMaal BosBen. Select your Macro from the list and click OK. You can edit the text on a button. Right click the button to see a menu appear. From the menu, select Edit Text:When you select Edit Text, a cursor will appear at the start of the text, and you can name the button Variable Types in VBA In order to set a variable, you must declare the type of variable you are setting. This can be done using ‘Dim’ Example: Dim MyVariable as integer Example 2: Dim MyVariable as Integer = 5 Sub Vartapie Practice) Dim Myitunber As Inceger Variable types and values can be declared in the same or different lines in VBA. Variable names cannot start wymamber = 10 with @ number, have spaces, or include special End Sub characters The various types of variables you can declare are: As Integer AsLong (alternative to As Integer, used when storing many numbers in one variable) As Single (used to store precision data, and store many decimal places) As Double (similar to As Single, but more precise) As String (used when storing words or characters) IF Statements in VBA Programming in any language is heavily reliant on Conditional Logic like the IF Statement. It allows you to go down different paths, depending on an initial condition. The structure of a VBA IF Statement looks like this: If
Then<'CODE HERE> End If You start with the word If (uppercase "I" lowercase "f"). After a space, you have a condition that you want to test. This conditional is something that can either be TRUE or FALSE. After your condition, you type a space followed by the word Then (uppercase ). An If Statement ends with the words End If. Create a new Sub in your coding window, and call it If_Test_1. Add the following code for your Sub: Dim MyNummber As Integer Sub If_Test_10) MyNumber = 10 lf MyNumber = 10 Then MsgBox “Number = 10" MyNumber = 10 If MyNumber Dim MyNunmber As Integer MsgBox "Number 10 Then End If End if End Sub Operator Meaning = Has a value of < Less than > Greater than <= Less than or equal to >= Greater than or equal to <> Not equal to Sub If Test_30) Dam Myunmber As Integer MyNumber = MS Miogbox Miunber ¢" ie Less than 20" End If End SubStrings of Text in VBA Setting up a variable to hold text is quite straightforward. You simply Dim a variable As String Dim MyString As String To store text inside of your variable you need to surround it with double quotes: MyString = "Some text” Even if you place numbers between double quotes they still gets treated as text and not Integers MyString = "25" The above line means store 25 as text, and NOT store the number 25 You can place text into a cell on your spreadsheet Dim MyString As String MyString = "Some text" ActiveCell. Value = MyString And you can get text out of cell on your spreadsheet Dim MyString As String MyString = ActiveCell. Value Programing Loops in VBA The most common type of loop is called a For Loop. Bear in mind that a For Loop goes round and round until it meets an end condition. Once the end condition is met then the programming flow will continue downward, in its natural direction.The following will run you through an Seb TeopExanpie () example of a sample code involving a For Dim scaxcimaber as Tavege= Loop. Dim EndNunber As Integer Dim answer As Integer answer = 1 Endlumber = 5 We've added a new Integer variable For StartNumber = 1 To End¥unber called answer. The message box has been moved to the end. Between the For line answer = answer + StartNunber and the Next line we have some new code. This: Next StartNunber MsgBox answer answer = answer + StartNumber End Sub To understand this line, start after the equal sign: answer + StartNumber This says, "Add together whatever is stored in the variable called answer and whatever is stored in the variable called StartNumber. However, we haven't stored anything in the answer variable yet. So what value does it hold? If you don't store a value in an Integer variable then it gets set to 0. TheStartNumber variable is 1 the first time round the loop. So the sum on the right of the equal sign is really this: o+4 When VBA has finished calculating this it needs to store the result somewhere. That "somewhere" is whatever you have to the left of the equal sign. We have the variable called answer to the left of the equal sign. So this is where VBA stores the result of 0 + 1. In other words, the answer variable will be overwritten with the new value. The next time round the loop the two variables to the right of the equal sign will hold the following values: 142 The third time round the loop the two variables to the right of the equal sign will be this: 343 But by going round the loop 5 times, we've added up the numbers from 4 to 5. This gives a value of 15. Run your programme and test it out. The message box should display an answer of 15.Now run the code. The message box displays 5 times, once for each time round the loop. This time, the values will be the same as from our table above, from the left-hand column under answer = Cell Properties As well as referring to cells on a spreadsheet with Range you can use Cells. The Cells. property has an Item property that you use to reference the cells on your spreadsheet. Worksheets(“Sheet1”).Activate [selects the sheet] Range(“A1”).Select or Range(“A1:B1”).Select [this selects the cell] Or alternatively: Cells.ltem(Row, Column) The Row is always a number. But the column can be a number or letter: Cells.ltem(1, 1) Cells.Item(1, "A")x"* You can shorten this even further and get rid of the Item property altogether: Cells(1, 1) Cells(1, "A") After selecting a cell, you can do a range of functions on that cell like Range(“A1”).copy, Range(“A1”).paste, Range(“A1”).Value = “NewValue”, Range(“A1”).Offset(1,1) are a couple examples Record Macro 1. On the Tools menu, point to Macro, and then click Record New Macro. In the Macro name box, enter a name for the macro. NotesThe first character of the macro name must be a letter. Other characters can be letters, numbers, or underscore characters. Spaces are not allowed in a macro name; an underscore character works well as a word separator Do not use a macro name that is also a cell reference or you can get an error message that the macro name is not valid. If you want to run the macro by pressing a keyboard shortcut key, enter a letter in the Shortcut key box. You can use CTRL# letter (for lowercase letters) or CTRL+SHIFT+ letter (for uppercase letters), where letter is any letter key on the keyboard. The shortcut key letter you use cannot be a number or special character such as @ or # NOTE The shortcut key will override any equivalent default Microsoft Excel shortcut keys while the workbook that contains the macro is open In the Store macro in box, click the location where you want to store the macro. If you want a macro to be available whenever you use Excel, select Personal Macro Workbook. . If you want to include a description of the macro, type it in the Description box. Click OK. If you want the macro to run relative to the position of the active cell, record it using relative cell references. On the Stop Recording toolbar, click Relative Reference so that itis selected. Excel will continue to record macros with relative references until you quit Excel or until you click Relative Reference again, so that itis not selected. Carry out the actions you want to record. . On the Stop Recording toolbar, click Stop Recording . [31]
You might also like
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
From Everand
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
Mark Manson
4/5 (6126)
Principles: Life and Work
From Everand
Principles: Life and Work
Ray Dalio
4/5 (627)
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
From Everand
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
Brené Brown
4/5 (1148)
Never Split the Difference: Negotiating As If Your Life Depended On It
From Everand
Never Split the Difference: Negotiating As If Your Life Depended On It
Chris Voss
4.5/5 (932)
The Glass Castle: A Memoir
From Everand
The Glass Castle: A Memoir
Jeannette Walls
4/5 (8215)
Grit: The Power of Passion and Perseverance
From Everand
Grit: The Power of Passion and Perseverance
Angela Duckworth
4/5 (631)
Sing, Unburied, Sing: A Novel
From Everand
Sing, Unburied, Sing: A Novel
Jesmyn Ward
4/5 (1253)
The Perks of Being a Wallflower
From Everand
The Perks of Being a Wallflower
Stephen Chbosky
4/5 (8365)
Shoe Dog: A Memoir by the Creator of Nike
From Everand
Shoe Dog: A Memoir by the Creator of Nike
Phil Knight
4.5/5 (860)
Her Body and Other Parties: Stories
From Everand
Her Body and Other Parties: Stories
Carmen Maria Machado
4/5 (877)
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
From Everand
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
Margot Lee Shetterly
4/5 (954)
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
From Everand
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
Ben Horowitz
4.5/5 (361)
Steve Jobs
From Everand
Steve Jobs
Walter Isaacson
4/5 (2923)
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
From Everand
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
Ashlee Vance
4.5/5 (484)
The Emperor of All Maladies: A Biography of Cancer
From Everand
The Emperor of All Maladies: A Biography of Cancer
Siddhartha Mukherjee
4.5/5 (277)
A Man Called Ove: A Novel
From Everand
A Man Called Ove: A Novel
Fredrik Backman
4.5/5 (4972)
Angela's Ashes: A Memoir
From Everand
Angela's Ashes: A Memoir
Frank McCourt
4.5/5 (444)
Brooklyn: A Novel
From Everand
Brooklyn: A Novel
Colm Tóibín
3.5/5 (2061)
The Art of Racing in the Rain: A Novel
From Everand
The Art of Racing in the Rain: A Novel
Garth Stein
4/5 (4281)
The Yellow House: A Memoir (2019 National Book Award Winner)
From Everand
The Yellow House: A Memoir (2019 National Book Award Winner)
Sarah M. Broom
4/5 (100)
The Little Book of Hygge: Danish Secrets to Happy Living
From Everand
The Little Book of Hygge: Danish Secrets to Happy Living
Meik Wiking
3.5/5 (447)
The World Is Flat 3.0: A Brief History of the Twenty-first Century
From Everand
The World Is Flat 3.0: A Brief History of the Twenty-first Century
Thomas L. Friedman
3.5/5 (2283)
Yes Please
From Everand
Yes Please
Amy Poehler
4/5 (1987)
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
From Everand
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
Gilbert King
4.5/5 (278)
Bad Feminist: Essays
From Everand
Bad Feminist: Essays
Roxane Gay
4/5 (1068)
The Outsider: A Novel
From Everand
The Outsider: A Novel
Stephen King
4/5 (1993)
The Woman in Cabin 10
From Everand
The Woman in Cabin 10
Ruth Ware
3.5/5 (2641)
A Tree Grows in Brooklyn
From Everand
A Tree Grows in Brooklyn
Betty Smith
4.5/5 (1936)
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
From Everand
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
Viet Thanh Nguyen
4.5/5 (125)
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
From Everand
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
Dave Eggers
3.5/5 (692)
Team of Rivals: The Political Genius of Abraham Lincoln
From Everand
Team of Rivals: The Political Genius of Abraham Lincoln
Doris Kearns Goodwin
4.5/5 (1912)
Wolf Hall: A Novel
From Everand
Wolf Hall: A Novel
Hilary Mantel
4/5 (4074)
On Fire: The (Burning) Case for a Green New Deal
From Everand
On Fire: The (Burning) Case for a Green New Deal
Naomi Klein
4/5 (75)
Rise of ISIS: A Threat We Can't Ignore
From Everand
Rise of ISIS: A Threat We Can't Ignore
Jay Sekulow
3.5/5 (143)
Fear: Trump in the White House
From Everand
Fear: Trump in the White House
Bob Woodward
3.5/5 (830)
Manhattan Beach: A Novel
From Everand
Manhattan Beach: A Novel
Jennifer Egan
3.5/5 (901)
GM 1927 31a PDCA Wall Scorecard
PDF
No ratings yet
GM 1927 31a PDCA Wall Scorecard
10 pages
John Adams
From Everand
John Adams
David McCullough
4.5/5 (2542)
The Light Between Oceans: A Novel
From Everand
The Light Between Oceans: A Novel
M L Stedman
4.5/5 (790)
GM 1927 14 Maintenance Checklist
PDF
No ratings yet
GM 1927 14 Maintenance Checklist
7 pages
GM 1927 16 Process Control Plan Audit PCPA
PDF
No ratings yet
GM 1927 16 Process Control Plan Audit PCPA
7 pages
Impact Detail Report 850455495
PDF
No ratings yet
Impact Detail Report 850455495
3 pages
Writing Effective Action Plans
PDF
No ratings yet
Writing Effective Action Plans
6 pages
Excel The Smart Way 51 Tips Ebook Final
PDF
No ratings yet
Excel The Smart Way 51 Tips Ebook Final
59 pages
5S CQI GQA Checklist
PDF
No ratings yet
5S CQI GQA Checklist
4 pages
The Unwinding: An Inner History of the New America
From Everand
The Unwinding: An Inner History of the New America
George Packer
4/5 (45)
Little Women
From Everand
Little Women
Louisa May Alcott
4/5 (105)
The Constant Gardener: A Novel
From Everand
The Constant Gardener: A Novel
John le Carré
3.5/5 (109)
Related titles
Click to expand Related Titles
Carousel Previous
Carousel Next
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
From Everand
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
Principles: Life and Work
From Everand
Principles: Life and Work
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
From Everand
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
Never Split the Difference: Negotiating As If Your Life Depended On It
From Everand
Never Split the Difference: Negotiating As If Your Life Depended On It
The Glass Castle: A Memoir
From Everand
The Glass Castle: A Memoir
Grit: The Power of Passion and Perseverance
From Everand
Grit: The Power of Passion and Perseverance
Sing, Unburied, Sing: A Novel
From Everand
Sing, Unburied, Sing: A Novel
The Perks of Being a Wallflower
From Everand
The Perks of Being a Wallflower
Shoe Dog: A Memoir by the Creator of Nike
From Everand
Shoe Dog: A Memoir by the Creator of Nike
Her Body and Other Parties: Stories
From Everand
Her Body and Other Parties: Stories
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
From Everand
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
From Everand
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
Steve Jobs
From Everand
Steve Jobs
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
From Everand
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
The Emperor of All Maladies: A Biography of Cancer
From Everand
The Emperor of All Maladies: A Biography of Cancer
A Man Called Ove: A Novel
From Everand
A Man Called Ove: A Novel
Angela's Ashes: A Memoir
From Everand
Angela's Ashes: A Memoir
Brooklyn: A Novel
From Everand
Brooklyn: A Novel
The Art of Racing in the Rain: A Novel
From Everand
The Art of Racing in the Rain: A Novel
The Yellow House: A Memoir (2019 National Book Award Winner)
From Everand
The Yellow House: A Memoir (2019 National Book Award Winner)
The Little Book of Hygge: Danish Secrets to Happy Living
From Everand
The Little Book of Hygge: Danish Secrets to Happy Living
The World Is Flat 3.0: A Brief History of the Twenty-first Century
From Everand
The World Is Flat 3.0: A Brief History of the Twenty-first Century
Yes Please
From Everand
Yes Please
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
From Everand
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
Bad Feminist: Essays
From Everand
Bad Feminist: Essays
The Outsider: A Novel
From Everand
The Outsider: A Novel
The Woman in Cabin 10
From Everand
The Woman in Cabin 10
A Tree Grows in Brooklyn
From Everand
A Tree Grows in Brooklyn
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
From Everand
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
From Everand
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
Team of Rivals: The Political Genius of Abraham Lincoln
From Everand
Team of Rivals: The Political Genius of Abraham Lincoln
Wolf Hall: A Novel
From Everand
Wolf Hall: A Novel
On Fire: The (Burning) Case for a Green New Deal
From Everand
On Fire: The (Burning) Case for a Green New Deal
Rise of ISIS: A Threat We Can't Ignore
From Everand
Rise of ISIS: A Threat We Can't Ignore
Fear: Trump in the White House
From Everand
Fear: Trump in the White House
Manhattan Beach: A Novel
From Everand
Manhattan Beach: A Novel
GM 1927 31a PDCA Wall Scorecard
PDF
GM 1927 31a PDCA Wall Scorecard
John Adams
From Everand
John Adams
The Light Between Oceans: A Novel
From Everand
The Light Between Oceans: A Novel
GM 1927 14 Maintenance Checklist
PDF
GM 1927 14 Maintenance Checklist
GM 1927 16 Process Control Plan Audit PCPA
PDF
GM 1927 16 Process Control Plan Audit PCPA
Impact Detail Report 850455495
PDF
Impact Detail Report 850455495
Writing Effective Action Plans
PDF
Writing Effective Action Plans
Excel The Smart Way 51 Tips Ebook Final
PDF
Excel The Smart Way 51 Tips Ebook Final
5S CQI GQA Checklist
PDF
5S CQI GQA Checklist
The Unwinding: An Inner History of the New America
From Everand
The Unwinding: An Inner History of the New America
Little Women
From Everand
Little Women
The Constant Gardener: A Novel
From Everand
The Constant Gardener: A Novel