what is variables and why we declare variables in vba

vksharma00001

Board Regular
Joined
Jun 9, 2014
Messages
116
Hi Experts

Can anyone suggest me that what is variables and why we declare variables in vba ?

Thanks in advance
Vijay K Sharma
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
It's easier to explain the purpose of a variable than what a variable is. A variable is an object. You see how that doesn't really tell you anything about what a variable is? Now the purpose of a variable is to hold a value. So lets say you have a variable called myVariable. Now lets say you have a line of code that says myVariable = 2. So now it is equal to 2. Think about why you would want to use a variable that is equal to 2 rather than just using the number 2 in your code. One example is if you are creating a loop. Each time the code runs through the loop, it needs to add 1 to the variable. This is how it keeps track of how many times it runs through the loop. Here's an example.
Code:
myVariable = 1
Do Until myVariable = 5
'some code
myVariable = myVariable + 1
Loop
Looking at the code above, it will contine to add 1 to myVariable each time it goes through the loop. Once myVariable equals 6, it is greater than 5 so it exists the loop. Try accomplishing the same thing by unsing a number instead of a variable. You can't. This is only one reason you would use a variable. Now for your other question, why do you declare a variable. Well I don't declare variables. This is an example of declaring a variable...
Dim myVariable as Integer
I never do this. VB automatically assumes a declaration based on what the variable equals. So I don't like to make my code longer than it has to by making lines and lines of declaration code. There are instances that you have to declare a variable such as when you create an array variable. In this case, I just wait for VB to give me a declaration error message when I run the code before I make declare the variable. I don't like to do more work than I have to. But some programmers do things the right way. You really should declare all of your variables. The reason is because you may be using more resources than needed. For example lets say myVariable = 2 and I didn't declare it as an Integer because I didn't declare it at all. VB will assume it is a Double. Double uses up way more system resources than Integer. Honestly I wouldn't care less, but if you are creating code for a very large program, it could slow the system down dramatically. So in that instance, I would delare my variables.
 
Upvote 0
It's easier to explain the purpose of a variable than what a variable is. A variable is an object. You see how that doesn't really tell you anything about what a variable is? Now the purpose of a variable is to hold a value. So lets say you have a variable called myVariable. Now lets say you have a line of code that says myVariable = 2. So now it is equal to 2. Think about why you would want to use a variable that is equal to 2 rather than just using the number 2 in your code. One example is if you are creating a loop. Each time the code runs through the loop, it needs to add 1 to the variable. This is how it keeps track of how many times it runs through the loop. Here's an example.
Code:
myVariable = 1
Do Until myVariable = 5
'some code
myVariable = myVariable + 1
Loop
Looking at the code above, it will contine to add 1 to myVariable each time it goes through the loop. Once myVariable equals 6, it is greater than 5 so it exists the loop. Try accomplishing the same thing by unsing a number instead of a variable. You can't. This is only one reason you would use a variable. Now for your other question, why do you declare a variable. Well I don't declare variables. This is an example of declaring a variable...
Dim myVariable as Integer
I never do this. VB automatically assumes a declaration based on what the variable equals. So I don't like to make my code longer than it has to by making lines and lines of declaration code. There are instances that you have to declare a variable such as when you create an array variable. In this case, I just wait for VB to give me a declaration error message when I run the code before I make declare the variable. I don't like to do more work than I have to. But some programmers do things the right way. You really should declare all of your variables. The reason is because you may be using more resources than needed. For example lets say myVariable = 2 and I didn't declare it as an Integer because I didn't declare it at all. VB will assume it is a Double. Double uses up way more system resources than Integer. Honestly I wouldn't care less, but if you are creating code for a very large program, it could slow the system down dramatically. So in that instance, I would delare my variables.
I mean the following in the nicest possible way... I would elaborate but I don't have the time or energy now but for starters I just don't want anyone to get bad information and not know about it... The topic of variables is fundamental to good programming.

@WarPiglet: I'm afraid so very many of your statements above are totally wrong. Even down to the very foundation of what variables are and when/why we should use them and why we should always declare them. I encourage you to research the topic. The article Michael referred to should help improve your knowledge of variables considerably.

@Vijay: Please stick with the article referred to by Michael.

I also suggest you read this article and all associates sub-articles.
 
Upvote 0
Thank you so much Jon and Hiker95 for providing such a valuable understanding on this topic. Really thankful to all of you.
 
Upvote 0
vksharma00001,

Thanks for the feedback.

You are very welcome. Glad we could help.

And, come back anytime.


And, see if something below, in my most up to date list will help you:

Training / Books / Sites as of 10/26/2014

What Is VBA?
VBA is an acronym for Visual Basic for Applications. VBA should not be confused with VB, which is standard Visual Basic. Visual Basic for Applications is a programming feature designed by Microsoft for use with their Microsoft Office

MrExcel's Products: Books, CDs, Podcasts Discuss topics related to Holy Macro! Products: Learn Excel from MrExcel, VBA and Macros for Microsoft Excel,Holy Macro! It's 2500 VBA Examples CD, Guerilla Data Analysis Using Microsoft Excel and Excel Knowledge Base CD and the MrExcel Podcasts.
MrExcel's Products: Books, CDs, Podcasts

There are over 1800 Excel videos/tutorials here:
ExcelIsFun - YouTube

Getting Started with VBA.
DataPig Technologies

If you are serious about learning VBA try
http://www.add-ins.com/vbhelp.htm

Excel Tutorials and Tips - VBA - macros - training
MrExcel.com | Excel Resources | Excel Seminars | Excel Products

MrExcel's Excel Tutorial Listing
MrExcel.com | Excel Resources | Excel Seminars | Excel Products

Free VBA Course
Free VBA Course

Excel 2007 VBA materials to learn here:
VBA for Excel 2007 tutorial-VBA programming?

The macro recorder actually is your friend but look here:
Beyond Excel's recorder

Introduction to Variables, Constants and Data Types
Introduction to Variables, Constants and Data Types

Here's a good primer on the scope of variables.
Understanding Scope

Using Variables in Excel VBA Macro Code
Excel VBA Variables. Using Variables in Excel VBA Macro Code

See David McRitchie's site if you just started with VBA
Getting Started with Macros and User Defined Functions

What is a VBA Module and How is a VBA Module Used?
What Is A VBA Module and how is a VBA Module Used?

Events And Event Procedures In VBA
Events In Excel VBA

Here is a good introductory tutorial using a VBA Class:
Classes In VBA

There's a chapter on classes in VBA Developer's Handbook which is also good.
VBA Developer's Handbook, 2nd Edition: Ken Getz, Mike Gilbert: 0025211229781: Amazon.com: Books

Ron's Excel Tips
Excel for Windows Tips

Ron de Bruin's Mail from Excel and make/mail PDF files (Windows)
Mail from Excel and make/mail PDF files (Windows)

How do I create and use a PERSONAL file for my VBA code
How do I create and use a PERSONAL file for my VBA code

Anthony's Excel VBA Page - Excel Application - Excel Consultant - Excel Consulting (see 3 tutorials in Excel VBA Basic Tutorial Series)

BET: Microsoft Excel Visual Basic

Debugging VBA
Debug Excel VBA Macro Code
TechBookReport - Debugging Excel VBA Code

Start at the beginning...

Creating An XLA Add-In For Excel, Writing User Defined Functions In VBA
Creating An XLA Add In

Build an Excel Add-In
VBA Tips: Build an Excel Add-In

Creating custom functions
http://office.microsoft.com/en-us/excel/HA011117011033.aspx

Writing Your First VBA Function in Excel
http://www.exceltip.com/st/Writing_Your_First_VBA_Function_in_Excel/631.html

VBA for Excel (Macros)
Excel Macros (VBA) Tutorial

Excel Macros Tutorial
Excel Macros (VBA) Tutorial

Excel Macros & Programming
EXCEL Macros Tutorial

VBA Lesson 11: VBA Code General Tips and General Vocabulary
http://www.excel-vba.com/vba-code-2-1-tips.htm

Excel VBA -- Adding Code to a Workbook
http://www.contextures.com/xlvba01.html

Beyond Excel's recorder

Helpful as a simple concise reference for the basics, and, using the macro recorder is endlessly powerful.
MS Excel: Cells

Learn to debug:
Debugging VBA

How To: Assign a Macro to a Button or Shape
How To: Assign a Macro to a Button or Shape - Peltier Tech Blog

User Form Creation
http://www.contextures.com/xlUserForm01.html

Build a UserForm for Excel
http://www.fontstuff.com/ebooks/free/fsuserforms.pdf

When To Use a UserForm & What to Use a UserForm For
When to use Userform & What To Use Them For. Excel Training VBA 2 lesson 2

Excel Tutorials / Video Tutorials - Functions
http://www.contextures.com/xlFunctions02.html

How to insert Buttons, radio buttons and check boxes in Excel
How to insert Buttons, radio buttons and check boxes in Excel - Bing Videos

INDEX MATCH - Excel Index Function and Excel Match Function
http://www.contextures.com/xlFunctions03.html

Multi or two way vlook up and index match tutorial
Get Digital Help - Excel formulas, array formulas, vba, charts, pivot tables, templates and much more.

Excel Data Validation
http://www.contextures.com/xlDataVal08.html#Larger
http://www.contextures.com/excel-data-validation-add.html

Excel -- Data Validation -- Create Dependent Lists
http://www.contextures.com/xlDataVal02.html

Create dependent drop down lists containing unique distinct values in excel

Your Quick Reference to Microsoft Excel Solutions
XL-CENTRAL.COM : For your Microsoft Excel Solutions

New! Excel Recorded Webinars
DataPig Technologies

Fuzzy Matching - new version plus explanation

Programming The VBA Editor - Created by Chip Pearson at Pearson Software Consulting LLC
This page describes how to write code that modifies or reads other VBA code.
Programming In The VBA Editor

VBA and Macros for Microsoft Excel, by Bill Jelen "Mr.Excel" and Tracy Syrstad

Excel Hacks 100 Industrial-Strength Tips & Tools, by David & Traina Hawley

VBA and Macros for Microsoft Excel 2007, by Bill Jelen "Mr.Excel" and Tracy Syrstad

John Walkenbach's power programming with Excel books.

Excel 2010 Power Programming with VBA, Mr. Spreadsheet's Bookshelf

by Stephen/ Bovey, Rob/ Green, John Bullen (Paperback - Feb 11, 2005)
Professional Excel Development

by Rob Bovey, Stephen Bullen, John Green, and Robert Rosenberg (Paperback - Sep 26, 2001)
Excel 2002 VBA: Programmers Reference

Professional Excel Development by Rob Bovey, Dennis Wallentin, Stephen Bullen, & John Green

DonkeyOte: My Recommended Reading, Volatility
Volatile Excel Functions -Decision Models

A list of Reference Books
AJP Excel Information

Sumproduct
http://www.xldynamic.com/source/xld.SUMPRODUCT.html

Arrays
VBA Array Basics
VBA Arrays
Excel: Introduction to Array Formulas - Xtreme Visual Basic Talk
Excel VBA Chpter 21: Array in Excel VBA

Array Dimensions in Visual Basic - Working with Dimensions (code and graphics)
Array Dimensions in Visual Basic

Visual Basic Array Tutorial written by Adam Wehmann
Visual Basic Arrays Tutorial

Shortcut Keys in Excel 2000 through 2007

Pivot Intro
Using Pivot Tables and Pivot Charts in Microsoft Excel
Office 2010 Class #36: Excel PivotTables Pivot Tables 15 examples (Data Analysis) - YouTube
Office 2010 Class #36: Excel PivotTables Pivot Tables 15 examples (Data Analysis) - YouTube
Getting Started with Pivot Tables
http://www.contextures.com/xlPivot01.html#Start
Overview of PivotTable and PivotChart reports
Overview of PivotTable and PivotChart reports - Excel
Build a Pivot Table in Excel VBA
Excel VBA - Build A Pivot Table In Excel VBA Tutorials

Conditional Formatting
Use a formula to apply conditional formatting - Excel

Email from XL - VBA
Mail from Excel example pages

Outlook VBA
Writing VBA code for Microsoft Outlook

Excel Function Dictionary by Peter Noneley
http://www.xlfdic.com/
www.xlfdic.com Excel Function Dictionary 150 example functions and formula.

Function Translations
Excel 2007 function name translations - Dictionary Chart Front Page

Dynamic Named Ranges
http://www.contextures.com/xlNames01.html

How to create Excel Dashboards
http://www.contextures.com/excel-dashboards.html
Excel Dashboards - Templates, Tutorials, Downloads and Examples | Chandoo.org - Learn Microsoft Excel Online
Excel Dashboards - Templates, Tutorials, Downloads and Examples | Chandoo.org - Learn Microsoft Excel Online
Free Microsoft Excel Dashboard Widgets to Download
Gauge

Excel Dashboard / Scorecard Ebook
Excel Dashboards and Scorecards Ebook | How to Create Dashboards in Excel

Mike Alexander from Data Pig Technologies
Excel 2007 Dashboards and Reports For Dummies: 9780470228142: Computer Science Books @ Amazon.com

Templates
CPearson.com Topic Index
http://www.contextures.com/excel-template-golf-scores.html
Free Microsoft Excel Template Links & Search Engine

Microsoft Excel Cascading Listboxes Tutorial
Microsoft Excel Cascading Listboxes Tutorial - YouTube

Date & Time stamping:
McGimpsey & Associates : Excel : Time and date stamps

Get Formula / Formats thru custom functions:
Show FORMULA or FORMAT of another cell

A nice informative MS article "Improving Performance in Excel 2007"
Improving Performance in Excel 2007

Progress Meters
Progress meters
xcelfiles.com

How to convert text to numbers in Excel
How to convert text to numbers in Excel

How to parse data from the web - Brett Fret has a video in YouTube
Excel VBA - Easily parse XML data from a file or website using XML Maps - YouTube

Excel VBA MAC shortcuts
http://www.mrexcel.com/forum/excel-questions/465614-mac-shortcuts-excel.html

Macintosh Keyboard Shortcuts to use in Microsoft Excel
http://office.microsoft.com/en-us/mac-excel-help/excel-keyboard-shortcuts-HA102927337.aspx

Excel shortcut and function keys - Excel

And, as your skills increase, try answering threads on sites like:
MrExcel.com | Excel Resources | Excel Seminars | Excel Products
Excel Help Forum
Excel Templates | Excel Add-ins and Excel Help with formulas and VBA Macros
http://www.vbaexpress.com
Excel, Access, PowerPoint and Word VBA Macro Automation Help

If you are willing to spend money for the training, then something here should work for you...
Amazon.com: excel tutorial dvd

Advanced Excel Training - Online Excel Course
Excel Training: The Top Advanced Excel Course Online - Udemy

Excel VBA Programmer Interview - Excel Test for interview
http://www.vbaexpress.com/forum/showthread.php?48406-Excel-VBA-Programmer-Interview&
 
Upvote 0

Forum statistics

Threads
1,223,730
Messages
6,174,162
Members
452,548
Latest member
Enice Anaelle

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top