Microsoft training courses | Wise Owl - home page

Phone (01457) 858877 or email

VARIABLES AND CONSTANTS IN EXCEL VISUAL BASIC

Part four of an eight-part series of blogs

This blog explains the nuts and bolts of Excel VBA variables, including how to declare them, how to set their scope and all sorts of other tips and tricks. The blog also covers constants in Excel macros.

  1. Variables in Excel Visual Basic macros
  2. What is a Variable? VBA macro variables.
  3. Declaring Variables in Visual Basic for Applications
  4. Using Variables (Assigning and Retrieving Values) (this article)
  5. Forcing Declaration of Variables (Option Explicit)
  6. Useful Things to Know about Variables
  7. The Scope of Variables in VBA programs
  8. Constants in Excel Visual Basic for Applications

This blog is part of our Excel macros online tutorial series.  Alternatively, we run training courses in the UK in Excel and also in Visual Basic for Applications (and are always looking for partners in the US or other English-language countries!).

Posted by Andy Brown on 05 September 2011 | 4 comments

Using Variables (Assigning and Retrieving Values)

First, a quick reminder of what we're trying to do with our macro:

We're trying to copy the vote ... ... into the results sheet

There are only two basic commands with a variable - you can either assign a value to it, or read a value from it:

'put the current cell value into the variable HeroName

HeroName = ActiveCell.Value

'retrieve value from variable HeroName and store it in the current cell

ActiveCell.Value = HeroName

Bearing this in mind, let's have a look at our macro (it's listed in full at the bottom of this page).  First, we declare the variables we're going to use:

Sub RecordVote()

'the name of each superhero

Dim HeroName As String

'the rating assigned to them

Dim HeroRating As Long

The next thing to do is to find out the current hero's name and rating, and store these in variables:

'go to the votes sheet and get the value of the superhero, and their rating

Worksheets("Votes").Select

HeroName = Range("C4").Value

HeroRating = Range("C6").Value

Now we need to go the results sheet, and go down to the cell where the votes will be recorded:

'go to top of results

Worksheets("Results").Select

Range("B4").Select

'go to first blank cell

ActiveCell.End(xlDown).Select

ActiveCell.Offset(1, 0).Select

Now it's simply a case of transferring the information we've stored in the two variables into the worksheet:

'write variable values into this blank row

ActiveCell.Value = HeroName

ActiveCell.Offset(0, 1).Value = HeroRating

Finally (and this bit has nothing to do with variables), I've put two slightly complicated lines to copy the formatting down from the row above:

'copy formats from cell above (included here just to make macro complete)

Range(ActiveCell.Offset(-1, 0), ActiveCell.Offset(-1, 1)).Copy

ActiveCell.PasteSpecial xlPasteFormats

Application.CutCopyMode = False

Here's the full code, if you want to try it out:

Sub RecordVote()

'the name of each superhero

Dim HeroName As String

'the rating assigned to them

Dim HeroRating As Long

'go to the votes sheet and get the value of the superhero, and their rating

Worksheets("Votes").Select

HeroName = Range("C4").Value

HeroRating = Range("C6").Value

'go to top of results

Worksheets("Results").Select

Range("B4").Select

'go to first blank cell

ActiveCell.End(xlDown).Select

ActiveCell.Offset(1, 0).Select

'write variable values into this blank row

ActiveCell.Value = HeroName

ActiveCell.Offset(0, 1).Value = HeroRating

'copy formats from cell above (included here just to make macro complete)

Range(ActiveCell.Offset(-1, 0), ActiveCell.Offset(-1, 1)).Copy

ActiveCell.PasteSpecial xlPasteFormats

Application.CutCopyMode = False

End Sub

Variables are that simple!

VARIABLES AND CONSTANTS IN EXCEL VISUAL BASIC

Part four of an eight-part series of blogs

This blog explains the nuts and bolts of Excel VBA variables, including how to declare them, how to set their scope and all sorts of other tips and tricks. The blog also covers constants in Excel macros.

  1. Variables in Excel Visual Basic macros
  2. What is a Variable? VBA macro variables.
  3. Declaring Variables in Visual Basic for Applications
  4. Using Variables (Assigning and Retrieving Values) (this article)
  5. Forcing Declaration of Variables (Option Explicit)
  6. Useful Things to Know about Variables
  7. The Scope of Variables in VBA programs
  8. Constants in Excel Visual Basic for Applications

This blog is part of our Excel macros online tutorial series.  Alternatively, we run training courses in the UK in Excel and also in Visual Basic for Applications (and are always looking for partners in the US or other English-language countries!).

Comments on this blog

This blog has 4 comments:

Comment added by AndresLunasFarah on 14 June 2012 at 19:48 GMT

Hi! I found extremely usefull this blog and the explanations are very clear. So far i have follow all the examples in the line of learning to use Macros in Excel 2010 but i found in a dead end: in the full code found in the post "Using Variables(Assigning and Retrieving Values", the line that says : "ActiveCell.Offset(0,1).Value= HeroRating" gives me an error when i execute the marco, recording the name and rating of the hero i wrote in the last line of my sheet, that is row #1048576. Please give me some help about this because i need learn basic VBA in Excel for my University Tesis.

Thank“s a lot for this priceless blog.

Reply from Andy Brown (blog author)

Hi

Thanks for the kind comments.  I'm afraid I can't go through specific macros, but I can give you a couple of hints. 

Firstly, when running your macros press F8 to step through them line by line, and after running each line (they appear in yellow) check back in Excel that the active cell is where you think it should be. 

Secondly, you've ended up on the last row of the spreadsheet. The usual way to get there is with a command like:

ActiveCell.End(xlDown).Select

If you execute this at the bottom of a column of cells, you'll end up at the bottom of a worksheet.

 
Comment added by Will on 19 June 2012 at 17:17 GMT
Finally, someone who understands me and my suitcases (George and Oliver).
 
Comment added by subby on 26 June 2012 at 16:08 GMT
This is an absolute gem of a blog!  Really helped me get my head round the seemingly confusing world of VBA and the touches of humour are always appreciated :)  Thank you for this well thought-out, well structured and easy-to-read blog, I hope there's more to come
 
Comment added by Kanaka on 31 August 2012 at 10:06 GMT
Great tutorials! I only wish I had found this site when I first started with Excel VBA. It would have saved me hours of hunting and pecking on the web. Puhleeese, keep up the fantastic work.
 
Mahalo Nui Loa, Darryl

All content copyright Wise Owl Business Solutions Ltd 2013. All rights reserved.