Location via proxy:
[ UP ]
[Report a bug]
[Manage cookies]
No cookies
No scripts
No ads
No referrer
Show this form
Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Loading...
Loading...
User Settings
close menu
Welcome to Scribd!
Upload
Read for free
FAQ and support
Language (EN)
Sign in
0 ratings
0% found this document useful (0 votes)
18 views
VBA Guide Sheet
Uploaded by
Mostafa Abd Elalem
Copyright:
© All Rights Reserved
Available Formats
Download
as PDF or read online from Scribd
Download
Save
Save VBA Guide Sheet For Later
0%
0% found this document useful, Mark this document as useful
0%
0% found this document not useful, Mark this document as not useful
Embed
Share
Print
Report
VBA Guide Sheet
Uploaded by
Mostafa Abd Elalem
0 ratings
0% found this document useful (0 votes)
18 views
7 pages
Document Information
click to expand document information
Copyright
© © All Rights Reserved
Available Formats
PDF or read online from Scribd
Share this document
Share or Embed Document
Sharing Options
Share on Facebook, opens a new window
Facebook
Share on Twitter, opens a new window
Twitter
Share on LinkedIn, opens a new window
LinkedIn
Share with Email, opens mail client
Email
Copy link
Copy link
Did you find this document useful?
0%
0% found this document useful, Mark this document as useful
0%
0% found this document not useful, Mark this document as not useful
Is this content inappropriate?
Report
Copyright:
© All Rights Reserved
Available Formats
Download
as PDF or read online from Scribd
Download now
Download as pdf
Save
Save VBA Guide Sheet For Later
0 ratings
0% found this document useful (0 votes)
18 views
7 pages
VBA Guide Sheet
Uploaded by
Mostafa Abd Elalem
Copyright:
© All Rights Reserved
Available Formats
Download
as PDF or read online from Scribd
Save
Save VBA Guide Sheet For Later
0%
0% found this document useful, Mark this document as useful
0%
0% found this document not useful, Mark this document as not useful
Embed
Share
Print
Report
Download now
Download as pdf
Jump to Page
You are on page 1
of 7
Search inside document
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
Rating: 4 out of 5 stars
4/5 (5892)
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
Brene Brown
Rating: 4 out of 5 stars
4/5 (1103)
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
Rating: 4.5 out of 5 stars
4.5/5 (871)
Principles: Life and Work
From Everand
Principles: Life and Work
Ray Dalio
Rating: 4 out of 5 stars
4/5 (617)
Sing, Unburied, Sing: A Novel
From Everand
Sing, Unburied, Sing: A Novel
Jesmyn Ward
Rating: 4 out of 5 stars
4/5 (1216)
The Glass Castle: A Memoir
From Everand
The Glass Castle: A Memoir
Jeannette Walls
Rating: 4.5 out of 5 stars
4.5/5 (1731)
Grit: The Power of Passion and Perseverance
From Everand
Grit: The Power of Passion and Perseverance
Angela Duckworth
Rating: 4 out of 5 stars
4/5 (597)
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
Rating: 4 out of 5 stars
4/5 (912)
The Perks of Being a Wallflower
From Everand
The Perks of Being a Wallflower
Stephen Chbosky
Rating: 4.5 out of 5 stars
4.5/5 (2109)
Shoe Dog: A Memoir by the Creator of Nike
From Everand
Shoe Dog: A Memoir by the Creator of Nike
Phil Knight
Rating: 4.5 out of 5 stars
4.5/5 (543)
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
Rating: 4.5 out of 5 stars
4.5/5 (352)
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
Rating: 4.5 out of 5 stars
4.5/5 (474)
Her Body and Other Parties: Stories
From Everand
Her Body and Other Parties: Stories
Carmen Maria Machado
Rating: 4 out of 5 stars
4/5 (830)
Bad Feminist: Essays
From Everand
Bad Feminist: Essays
Roxane Gay
Rating: 4 out of 5 stars
4/5 (1043)
The Outsider: A Novel
From Everand
The Outsider: A Novel
Stephen King
Rating: 4 out of 5 stars
4/5 (1903)
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
From Everand
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
Viet Thanh Nguyen
Rating: 4.5 out of 5 stars
4.5/5 (122)
The Emperor of All Maladies: A Biography of Cancer
From Everand
The Emperor of All Maladies: A Biography of Cancer
Siddhartha Mukherjee
Rating: 4.5 out of 5 stars
4.5/5 (272)
The Little Book of Hygge: Danish Secrets to Happy Living
From Everand
The Little Book of Hygge: Danish Secrets to Happy Living
Meik Wiking
Rating: 3.5 out of 5 stars
3.5/5 (414)
Brooklyn: A Novel
From Everand
Brooklyn: A Novel
Colm Toibin
Rating: 3.5 out of 5 stars
3.5/5 (1955)
Angela's Ashes: A Memoir
From Everand
Angela's Ashes: A Memoir
Frank McCourt
Rating: 4.5 out of 5 stars
4.5/5 (443)
A Man Called Ove: A Novel
From Everand
A Man Called Ove: A Novel
Fredrik Backman
Rating: 4.5 out of 5 stars
4.5/5 (4848)
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
Rating: 4 out of 5 stars
4/5 (99)
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
Rating: 3.5 out of 5 stars
3.5/5 (2270)
Steve Jobs
From Everand
Steve Jobs
Walter Isaacson
Rating: 4.5 out of 5 stars
4.5/5 (811)
The Art of Racing in the Rain: A Novel
From Everand
The Art of Racing in the Rain: A Novel
Garth Stein
Rating: 4 out of 5 stars
4/5 (4226)
Yes Please
From Everand
Yes Please
Amy Poehler
Rating: 4 out of 5 stars
4/5 (1921)
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
Rating: 4.5 out of 5 stars
4.5/5 (269)
A Tree Grows in Brooklyn
From Everand
A Tree Grows in Brooklyn
Betty Smith
Rating: 4.5 out of 5 stars
4.5/5 (1933)
Team of Rivals: The Political Genius of Abraham Lincoln
From Everand
Team of Rivals: The Political Genius of Abraham Lincoln
Doris Kearns Goodwin
Rating: 4.5 out of 5 stars
4.5/5 (235)
The Woman in Cabin 10
From Everand
The Woman in Cabin 10
Ruth Ware
Rating: 3.5 out of 5 stars
3.5/5 (2564)
Wolf Hall: A Novel
From Everand
Wolf Hall: A Novel
Hilary Mantel
Rating: 4 out of 5 stars
4/5 (4002)
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
Rating: 3.5 out of 5 stars
3.5/5 (232)
Fear: Trump in the White House
From Everand
Fear: Trump in the White House
Bob Woodward
Rating: 3.5 out of 5 stars
3.5/5 (802)
John Adams
From Everand
John Adams
David McCullough
Rating: 4.5 out of 5 stars
4.5/5 (2410)
The Light Between Oceans: A Novel
From Everand
The Light Between Oceans: A Novel
M.L. Stedman
Rating: 4.5 out of 5 stars
4.5/5 (789)
Manhattan Beach: A Novel
From Everand
Manhattan Beach: A Novel
Jennifer Egan
Rating: 3.5 out of 5 stars
3.5/5 (880)
GM 1927 31a PDCA Wall Scorecard
Document
10 pages
GM 1927 31a PDCA Wall Scorecard
Mostafa Abd Elalem
No ratings yet
On Fire: The (Burning) Case for a Green New Deal
From Everand
On Fire: The (Burning) Case for a Green New Deal
Naomi Klein
Rating: 4 out of 5 stars
4/5 (74)
Rise of ISIS: A Threat We Can't Ignore
From Everand
Rise of ISIS: A Threat We Can't Ignore
Jay Sekulow
Rating: 3.5 out of 5 stars
3.5/5 (137)
The Constant Gardener: A Novel
From Everand
The Constant Gardener: A Novel
John Le Carré
Rating: 3.5 out of 5 stars
3.5/5 (107)
The Unwinding: An Inner History of the New America
From Everand
The Unwinding: An Inner History of the New America
George Packer
Rating: 4 out of 5 stars
4/5 (45)
Impact Detail Report 850455495
Document
3 pages
Impact Detail Report 850455495
Mostafa Abd Elalem
No ratings yet
GM 1927 14 Maintenance Checklist
Document
7 pages
GM 1927 14 Maintenance Checklist
Mostafa Abd Elalem
No ratings yet
GM 1927 16 Process Control Plan Audit PCPA
Document
7 pages
GM 1927 16 Process Control Plan Audit PCPA
Mostafa Abd Elalem
No ratings yet
Writing Effective Action Plans
Document
6 pages
Writing Effective Action Plans
Mostafa Abd Elalem
No ratings yet
Excel The Smart Way 51 Tips Ebook Final
Document
59 pages
Excel The Smart Way 51 Tips Ebook Final
Mostafa Abd Elalem
No ratings yet
5S CQI GQA Checklist
Document
4 pages
5S CQI GQA Checklist
Mostafa Abd Elalem
No ratings yet
Little Women
From Everand
Little Women
Louisa May Alcott
Rating: 4 out of 5 stars
4/5 (105)