Tutorial by Examples

Incorrect Code Sub DoSomething() GoSub DoThis DoThis: Debug.Print "Hi!" Return End Sub Why doesn't this work? Execution enters the DoSomething procedure, jumps to the DoThis label, prints "Hi!" to the debug output, returns to the instruction immediately afte...
Incorrect code Sub DoSomething() Dim row As Integer For row = 1 To 100000 'do stuff Next End Sub Why doesn't this work? The Integer data type is a 16-bit signed integer with a maximum value of 32,767; assigning it to anything larger than that will overflow the type and ...
Incorrect code Sub DoSomething() Dim foo(1 To 10) Dim i As Long For i = 1 To 100 foo(i) = i Next End Sub Why doesn't this work? foo is an array that contains 10 items. When the i loop counter reaches a value of 11, foo(i) is out of range. This error occurs whenever...
Incorrect code Public Sub DoSomething() DoSomethingElse "42?" End Sub Private Sub DoSomethingElse(foo As Date) ' Debug.Print MonthName(Month(foo)) End Sub Why doesn't this work? VBA is trying really hard to convert the "42?" argument into a Date value. When it ...
Incorrect code Sub DoSomething() Dim foo As Collection With foo .Add "ABC" .Add "XYZ" End With End Sub Why doesn't this work? Object variables hold a reference, and references need to be set using the Set keyword. This error occurs whenever ...
Incorrect code Sub DoSomething() On Error GoTo CleanFail DoSomethingElse CleanFail: Debug.Print Err.Number Resume Next End Sub Why doesn't this work? If the DoSomethingElse procedure raises an error, execution jumps to the CleanFail line label, prints the error number, a...

Page 1 of 1