Day 4 VBA Hero – Inputting variables


After declaring a variable, we will likely want to put a value in it.

Here is an example of how to put a variable in each type as described in day 3.

Sub ShowAllVariables()

Dim strTest As String, intTest As Integer
Dim lgTest As Long, dblTest As Double
Dim rngTest As Range, objTest As Object
Dim dtTest As Date, varTest As Variant

' an apostrophe signifies that this is now a comment.
' the varTest variable could have also been written without the last two words,
' but for the sake of this exercise, it has been added

strTest = "This is a string" 'String always needs to be in quotation marks
intTest = 10000 'this is basic number format, no spaces
lgTest = 200000000 'as it is numeric, no quotation marks
dblTest = 32.45 'same thing here, numeric thus no quotation marks
Set rngTest = Range("A1:A2") 'For a range you need to start with SET
Set objTest = ActiveWorkbook ' Same thing with an object
dtTest = Now()
varTest = "Anything you want"

Debug.Print strTest
Debug.Print intTest
Debug.Print lgTest
Debug.Print dblTest
Debug.Print dtTest
Debug.Print varTest

End Sub

The above code will “print” each variable in the bottom of your Visual Basic Editor (see image below .

I have included an image in the Visual Basic Editor to show what it looks like in there. All of the colours are added automatically.

In day 5, we start coding. The basics are pretty much all there.

Verified by MonsterInsights