More basic code that won't work

D

Dave Neve

Hi

As a lot of code in my book doesn't quite seem to work on my system, (VBA
Word 2002) I've tried sth off a site recommended by the group.

It's to work out the volume of a cylinder.

Private Sub OK_Click( )
r = Val(radius.Text)
h = Val(hght.Text)
pi = 22 / 7
v = pi * (r ^ 2) * h
volume.Text= Str$(v)
End Sub
Problem is that an error appears concerning the value r (non defined
variable).

Why can't I find some simple exercises to create macros which actually run
on my system?

Why is r a non defined variable when I've typed in a value?

I don't think the mistakes are from me as I copy and paste code.

Thanks in advance
 
J

Jezebel

Typed in a value where?

First. Read help and some other sources on how variables work in program
code. These issues apply, one way or another, to ALL programming languages.
If your code module includes the statement 'Option Explicit' right at the
top (and it should) then you have to declare each variable before you use
it:

Dim r as double (or string, long, boolean, etc)

Make sure you understand about variable types and scope before you go any
further, or you will find yourself heading rapidly toward strabismus,
insanity, and death. (It's not at all a difficult issue, but a critically
important one.)

Second, learn about objects and properties. Your code implies that you have
a 'radius' object with a 'Text' property (and a 'hght' object likewise).
What are these objects, where do they come from, and how does your code get
a reference to them?
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi Dave,

You must explicity declare (DIM) the variables r, h, pi and v.

Put the cursor after Option Explicit at the top of the code pane and press
F1.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi Dave,

Having now read your earlier posts, I suggest that you see the article
“Getting To Grips With VBA Basics In 15 Minutes” at:

http://www.mvps.org/word/FAQs/MacrosVBA/VBABasicsIn15Mins.htm

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
J

Jay Freedman

In addition to the advice Jezebel and Doug have given you, I think
you're suffering an additional confusion: The code you posted is
clearly intended to be the Click event procedure for a button named OK
on a userform that contains textboxes named radius, hght, and volume.
If you've simply copied this code from a web site and pasted it into
an ordinary module, it will not function.

If you're trying to begin understand VBA, I advise you not to go
anywhere near userforms until you've mastered the simpler parts of the
language.
 
D

Dave Neve

Hi

Thanks everyone for coming to my aid.

I expect to be posting for a while as VB isn't a one day thing.

To answer Jay, yes, I had created a Userform with the correct objects (as
far as I know).

I think my main problem is the wide selection of VB (.Net , applications,
versions etc) which each need minor alterations to code (beyond me).

I am working on it from my end in that I am reading books (VB.Net) and
looking at sites when recommended.

Just so you know my level when helping me, I am pre computer generation but
I can use a fair range of software with most options and my computer is well
maintained and protected.(backups, Ghost imges etc)

I've also just managed to publish a small web site with FP thanks to a
Microsoft group.

In other words, I can understand general issues but less so if it concerns
'code'.

Sorry for this lengthy introductory message.

Regards

Dave Neve
 
J

Jonathan West

Just so you know my level when helping me, I am pre computer generation but
I can use a fair range of software with most options and my computer is well
maintained and protected.(backups, Ghost imges etc)

I've also just managed to publish a small web site with FP thanks to a
Microsoft group.

In other words, I can understand general issues but less so if it concerns
'code'.

Hi Dave,

These articles might help you quite a bit. They will give you a set of basic
principles to start from on how to get your code to work.

Getting To Grips With VBA Basics In 15 Minutes
http://www.mvps.org/word/FAQs/MacrosVBA/VBABasicsIn15Mins.htm

Why variables should be declared properly
http://www.mvps.org/word/FAQs/MacrosVBA/DeclareVariables.htm

The art of defensive programming
http://www.mvps.org/word/FAQs/MacrosVBA/MaintainableCode.htm
 

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

Top