What is the correct use of the Dir function second parameter? (VbNormal, vbHidden, etc)

Jamualson

Board Regular
Joined
Feb 17, 2021
Messages
145
Office Version
  1. 2019
Platform
  1. Windows
I don't understand what the second parameter does. Seems no matter if I add multiple second parameters, nothing changes and code just works fine. But I don't understand why.

It would be awesome if I could see an example where I could test it to see the use of the second parameter in practice.

Thank you very much if you have an idea

This first part is ok:

VBA Code:
Sub test()

If Dir("C:\Windows", vbNormal) = "" Then

    cells(1,1).value = "Doesn't exist."

End If

End Sub

But let's say I add a VbHidden or any other parameter (doesn't matter if its a folder or a file) --> nothing happens. If file exist, it won't launch, even if I set it to vbHidden or vbReadOnly. I would expect that it would not run since the specified file is neither Hidden neither ReadOnly.

VBA Code:
Sub test2()

If Dir("C:\Folder\test.txt", vbNormal + vbReadOnly) = "" Then

    cells(1,1).value = "Doesn't exist."      'this part never happens. If file exists, it is handled as existing even if it's not hidden or not read only.

else

msgbox "Exists"     'always this part runs

End If

End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
So i just put an image file on my desktop and hid it. Right click properties, hidden.

VBA Code:
Filename = Dir("C:\Users\steve\Desktop\*jpg", vbNormal)

Do While Filename <> ""
    MsgBox Filename
    Filename = Dir
Loop

That produced nothing. Then i removed the hidden checkbox and i get a message box on rerunning the code with the filename. Is that what you mean?
 
Upvote 0

Forum statistics

Threads
1,214,424
Messages
6,119,400
Members
448,893
Latest member
AtariBaby

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
Back
Top