GetAttr Returns 22 and 17

Oorang

Well-known Member
Joined
Mar 4, 2005
Messages
2,071
I was trying to make use of GetAttr and the help file states GetAttr will return the following:

vbNormal 0 Normal.
vbReadOnly 1 Read-only.
vbHidden 2 Hidden.
vbSystem 4 System file. Not available on the Macintosh.
vbDirectory 16 Directory or folder.
vbArchive 32 File has changed since last backup. Not available on the Macintosh.
vbAlias 64 Specified file name is an alias. Available only on the Macintosh.

But I have also seen it return 17 & 22. Does anyone know what the vbfriendly names are or these or anything about them?
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,344
Office Version
  1. 365
Platform
  1. Windows

Oorang

Well-known Member
Joined
Mar 4, 2005
Messages
2,071
Ahhh. now I get it. 22 is a combination of Directory (16) Hidden(2) and System(4). I didn't realize it added them all up. Thanks:)
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,344
Office Version
  1. 365
Platform
  1. Windows

Oorang

Well-known Member
Joined
Mar 4, 2005
Messages
2,071

ADVERTISEMENT

LOL well don't feel bad, i just used your advice on using AND to reveal the attributes.
 

tusharm

MrExcel MVP
Joined
May 28, 2002
Messages
11,029
Yes! In fact the correct (and safe) way to use information returned in individual bits is to AND the returned value with the constant. An example would be
Code:
if (rslt and vbNormal) =vbnormal then...
if (rslt and vbReadOnly) =vbreadonly then...
if (rslt and vbHidden) =vbHidden then...
...
If you use the above method, you don't need to know what bit each attribute corresponds to nor do you have to worry about MS adding additional bits of information to the returned result. Your code won't know about them but neither will it fault. It will just gracefully ignore the additional information.

Edit: Added parenthesis to get the correct operator precedence.
Oorang said:
LOL well don't feel bad, i just used your advice on using AND to reveal the attributes.
 

Oorang

Well-known Member
Joined
Mar 4, 2005
Messages
2,071

ADVERTISEMENT

Although I did notice something odd, if I am using AND in conjunction with <>:
Code:
If GetAttr(X) AND vbDirectory <> 0 then
I have to enclose it in parentheses or it won't work:
Code:
If (GetAttr(X) AND vbDirectory) <> 0 then
Not sure what's up with that.
 

just_jon

Legend
Joined
Sep 3, 2002
Messages
10,473
Oorang said:
Although I did notice something odd, if I am using AND in conjunction with <>:
Code:
If GetAttr(X) AND vbDirectory <> 0 then
I have to enclose it in parentheses or it won't work:
Code:
If (GetAttr(X) AND vbDirectory) <> 0 then
Not sure what's up with that.

Shouldn't it be

If GetAttr(X) <>0 AND vbDirectory <> 0 then

unless GetAttr() is a Boolean array?
 

tusharm

MrExcel MVP
Joined
May 28, 2002
Messages
11,029
Nothing odd about it. It's normal operator precedence. In fact, while typing my post something was bugging me but I didn't follow through on it.

Oorang said:
Although I did notice something odd, if I am using AND in conjunction with <>:
Code:
If GetAttr(X) AND vbDirectory <> 0 then
I have to enclose it in parentheses or it won't work:
Code:
If (GetAttr(X) AND vbDirectory) <> 0 then
Not sure what's up with that.
 

Oorang

Well-known Member
Joined
Mar 4, 2005
Messages
2,071
Shouldn't it be

If GetAttr(X) <>0 AND vbDirectory <> 0 then

unless GetAttr() is a Boolean array?
The use of and in this case is doing a bitwise comparison. vbDirectory is a constant of 16 and GetAttr(x) will return a number that is the sum of all the file attribute constants. So to see if vbDirectory is set and you do GetAttr(X) which returns 22, to see if vbDirectory is part of that 22 you do
GetAttr(X) AND vbDirectory which returns 16 if it is and 0 if it isn't. However for some reason you can do If (GetAttr(X) AND vbDirectory) = 16 with no problems but to make If GetAttr(X) AND vbDirectory <> 0 work you need the parentheses.
Why?
No Clue.
But vbDirectory will always = 16 so you can't do If GetAttr(X) <>0 AND vbDirectory <> 0 because then you are looking at each one individually instead of compared. (I think) :wink:
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,114
Messages
5,835,462
Members
430,358
Latest member
zzc1128

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top