VBA Tutorial problem

  • Thread starter Thread starter AFJr
  • Start date Start date
A

AFJr

Hi,

I'm taking this tutorial

http://homepages.ius.edu/WCLANG/vbnotes/vbex2.htm

and cannot run the macro for the "Hello World!" procedure. I get a compile
error with when i try to run this:
- the yellow cursor pointing at the defined "Sub Hello World()"
- "CurrentSheet =" is selected

Here is the code from my sheet1 object:

Sub HelloWorld()

Set CurrentSheet = Application.ActiveSheet

CurrentSheet.Cells(2, 5) = "Hello World!"

End Sub

Any help would be appreciated. I am running Excel97 (I know, I know, its old)
 
Sounds like you have Option Explicit in your modules, which is a good
practice, so try

Sub HelloWorld()
Dim CurrentSheet As Worksheet

Set CurrentSheet = Application.ActiveSheet

CurrentSheet.Cells(2, 5) = "Hello World!"

End Sub



--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
Thanks Bob, that was it!

I though I might need to dimension CurrentSheet but didn't know why and
didn't know what to dimension it to. I remember reading in some book about
Option Explicit, I'll have to go back and hunt it down.

Thanks for your help!
--

AFJr


Bob Phillips said:
Sounds like you have Option Explicit in your modules, which is a good
practice, so try

Sub HelloWorld()
Dim CurrentSheet As Worksheet

Set CurrentSheet = Application.ActiveSheet

CurrentSheet.Cells(2, 5) = "Hello World!"

End Sub



--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
Option Explicit is a module setting that means that you HAVE to declare all
variables, otherwise you get the error.

It is a good practice, because if you leave to implicit variable
declaration, you get problems. For instance, look at

myValue= 125
MsgBox myValu * 1.175

The MsgBox will show zero, because although a variable myValue was loaded
with 125, the code then (mistakenly) used a variable myValu, the e was
omitted, which has no value. If the variable is declared, it will show as a
compile error, the code doesn't run and produce wrong results.

--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



AFJr said:
Thanks Bob, that was it!

I though I might need to dimension CurrentSheet but didn't know why and
didn't know what to dimension it to. I remember reading in some book about
Option Explicit, I'll have to go back and hunt it down.

Thanks for your help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top