557 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
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.
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!).
|
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!
Parts of this blog |
---|
|
Some other pages relevant to the above blogs include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2023. All Rights Reserved.