Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers 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 ...
After you've written a subroutine and, optionally, saved your code, you can run it to make it carry out the instructions you've written. This part of the lesson shows you how to do exactly that!
You can use the file you have created over the previous parts of this lesson. If you don't have this code you can click here to download a copy.
You can click here to download a file containing the completed code.
To begin this part of the lesson, you need a copy of the basic procedure we created in the previous parts. You can either use the version you have written, download a copy from the link in the Files Needed section above, or copy and paste the code shown below into a new module:
Sub My_First_Program()
'create a worksheet
Worksheets.Add
'enter values into cells
Range("A1").Value = "Wise Owl"
Range("A2").Value = Date
'format cells
Range("A1:A2").Interior.Color = rgbCoral
End Sub
You can run a procedure from within the VBE. Start by clicking somewhere within the subroutine you want to run:
Click anywhere between Sub and End Sub to select the procedure. If you don't do this, when you choose to run the code you'll see a dialog box asking you which procedure you want to run.
You can now choose one of the options from the table below to run your code:
Menu | Keyboard | Toolbar |
---|---|---|
Run | Run Sub/UserForm | F5 |
If anything goes wrong when you do this you can learn about how to solve problems in the next part of the lesson.
Assuming that you didn't encounter any errors, you can now switch back to the Excel window (you can press Alt + F11 to do this) to see the results:
You should see a new worksheet with values and formatting applied to these cells. We've increased the column width so that we can read the date.
While we're in the Excel window, it's worth mentioning that you can also run your subroutine from here. To do this, pick one of the options from the table below:
Ribbon | Keyboard |
---|---|
Developer | Macros | Alt + F8 |
You can then pick which subroutine you want to run from the dialog box which appears:
Excel refers to your subroutines as macros. Pick one from the list and either double-click its name or click Run to run it.
You should now have another new worksheet containing your name and the current date.
You can now either continue with the Extra Practice section below, or move to the next part of this lesson to learn how to solve problems in your code.
Although you don't have much to practise yet, you may want to experiment with making some small changes to your code.
Remember that you can press Ctrl + Spacebar after the = sign to see the IntelliSense list.
You should find that the new worksheet contains the current time.
Alter this line so that it formats every cell between A1 and A3.
Pick from any of the rgb colours in the IntelliSense list (press Ctrl + J to make the list appear).
Your cells should be a different colour on the new worksheet.
You can use the same range of colours for the font as for the interior of the cells.
The AutoFit method makes the column as wide as its widest value.
The final result of your hard work!
Sub My_First_Program()
'create a worksheet
Worksheets.Add
'enter values into cells
Range("A1").Value = "Wise Owl"
Range("A2").Value = Date
Range("A3").Value = Time
'format cells
Range("A1:A3").Interior.Color = rgbCornflowerBlue
Range("A1:A3").Font.Color = rgbWhite
'change column width
Columns("A").AutoFit
End Sub
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 2024. All Rights Reserved.