One-line code to test for number between -32 and +32?

E

Edward Mendelson

This should be obvious, but I'm afraid it isn't to me, so I'll be grateful
for any help.

Is there a way to write the If statement represented by CAPITAL letters
below on a single line? (And not as two nested If statements, one for more
than -32, one for less than 32)?

dim myNumber as Long
If myNumber IS MORE THAN -32 BUT LESS THAN +32 Then ...

Please forgive what may be the most elementary question asked here in years.

Edward Mendelson
 
S

Steve Lang

Hi Edward,

This is basic, but not as elementary as some of MY questions!

If mynumber >-32 and mynumber<32 then

end if

HTH and have a great day!

Steve
 
C

Chad DeMeyer

Edward,

As Steve indicates you have to use an AND operator. Please note that you
can use any number of the logical operators in constructing a single IF
statement:
AND - both conditions must be true
EQV - either both conditions true or both conditions false
IMP - returns true unless condition1 is true and condition2 is false, unless
null values are involved in which case it gets real complicated
NOT - condition must be false
OR - returns true if either condition is true
XOR - one and only one of the two conditions must be true

Like mathematical expressions, these expressions can be grouped in
parentheses to specify the order in which each expression is evaluated.

Regards,
Chad
 
H

Helmut Weber

Hi Edward,
if abs(i) < 33 then
Time to go to bed. Try 32 instead.
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98

Gruss
Helmut Weber
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 
E

Edward Mendelson

Helmut Weber said:
Hi Edward,

Hi Helmut,

That's exactly what I was trying to remember from my reading of some
Absolute Beginner's Guide to VBA (I should have remembered abs from the
Absolute in the title).

Yes, I was finishing a macro that would display the font and symbol code for
any selected character, and wanted to treat the characters below 32
differently from others. But when I told the macro to use <32, it gave the
special treatment to all symbol fonts, and I was trying to avoid that!

I've e-mailed you the code, and hope to post all these WP-symbol related
macros soon. (Once again, all of the "engine" was written by you...)

Best wishes,

Edward Mendelson
 
K

Klaus Linke

Yes, I was finishing a macro that would display the font and symbol code
for
any selected character, and wanted to treat the characters below 32
differently from others. But when I told the macro to use <32, it gave the
special treatment to all symbol fonts, and I was trying to avoid that!


In that case, you could use

If (AscW(Selection.Text) And &HFFFF&) < 32 Then
' ...

AscW returns an signed integer (2 bytes). Although 2 bytes are enough for
all 2-byte (Unicode) characters, codes above &H7FFF (= 32767) will show as
negative numbers.

The statement above forces Word to put that code in a signed long integer
(4 bytes) and masks the upper two bytes.

If you like hex numbers better anyway, and just need to display the code
(and not do comparisons or other calculations with it), you can also use
Hex( ) to turn the number in a string containing the hexadecimal
representation:
MsgBox Hex(AscW(Selection.Text))

Greetings,
Klaus
 

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