Setting File attributes to Hidden via VBA

omairhe

Well-known Member
Joined
Mar 26, 2009
Messages
2,040
Office Version
  1. 2019
Platform
  1. Windows
Hey Excel Gurus,

How do I set windows file attributes to hidden state. The cells below shall determine which workbooks need to be hidden and which of the workbooks must remain unhidden.

1604660444908.png



Please note that there are no formulas in range D2:D13 and Cell F2. However only E2:E13 have formulas in them that determines which workbooks need hiding or unhiding.

To clarify what my requirement is the same as achieving from the properties windows "Hidden" state as below.

1604660791774.png


Will appreicate a lot.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
VBA Code:
Sub changeFileAttributes()

    Dim path$, fileName$, i&
    path = Range("F2").Value & "\"
    For i = 2 To Cells(Rows.Count, 4).End(3).Row
        fileName = Dir(path & Replace(Cells(i, 4).Value, "-", "*") & ".*", vbHidden + vbNormal)

        Do While fileName <> ""
            SetAttr path & fileName, IIf(Cells(i, 5).Value = "unhide", vbNormal, vbHidden)
            fileName = Dir()
        Loop
    Next i

End Sub
 
Upvote 0
Solution
VBA Code:
Sub changeFileAttributes()

    Dim path$, fileName$, i&
    path = Range("F2").Value & "\"
    For i = 2 To Cells(Rows.Count, 4).End(3).Row
        fileName = Dir(path & Replace(Cells(i, 4).Value, "-", "*") & ".*", vbHidden + vbNormal)

        Do While fileName <> ""
            SetAttr path & fileName, IIf(Cells(i, 5).Value = "unhide", vbNormal, vbHidden)
            fileName = Dir()
        Loop
    Next i

End Sub
Hello, I tried and the code didn't work as expected the first time. then I tested a little more and the problem seems to be the filenames. the code will work on files where the names are 01-2020.xlsm But the files I am working with goes like 01-2020 (omair.enam).xlsm
In such an instance the code doesn:t seem to work nor it gives any errors.
Will appreciate further assistance.
 
Upvote 0
This code is assumed to work. It leaves, as opposed to @veyselemre's code, existing file attributes such as Archive or Read-Only unaffected.

VBA Code:
Sub ChangeFileAttr()

    Dim FSO As Object, oFolder As Object, oFile As Object
    Dim rng As Range, c As Range
    Dim sPath As String, sPrefix As String
    Dim vbAttr As VbFileAttribute

    Set FSO = CreateObject("Scripting.FileSystemObject")

    With ActiveSheet
        sPath = .Range("F2").Text
        If FSO.FolderExists(sPath) Then
            Set oFolder = FSO.GetFolder(sPath)
            Set rng = .Range("D2:D" & .Cells(.Rows.Count, "D").End(xlUp).Row)
            For Each c In rng
                sPrefix = Trim(c.Text)
                For Each oFile In oFolder.Files
                If StrComp(Left(oFile.Name, Len(sPrefix)), sPrefix, vbTextCompare) = 0 Then
                    vbAttr = oFile.Attributes
                    If StrComp(c.Offset(0, 1), "hide", vbTextCompare) = 0 Then
                        vbAttr = vbAttr Or vbHidden
                    Else
                        vbAttr = (vbAttr Or vbHidden) Xor vbHidden
                    End If
                    oFile.Attributes = vbAttr
                End If
                Next oFile
            Next c
        End If
    End With
End Sub
 
Upvote 0
for the code that was sent to me on post 2 I had to add one more asterisk * before .* on the following line like this

VBA Code:
        fileName = Dir(path & Replace(Cells(i, 4).Value, "-", "*") & "*.*", vbHidden + vbNormal)

I checked and both codes working.
Thanks veyselemre and GWteB
 
Upvote 0
VBA Code:
Sub changeFileAttributes()

    Dim path$, fileName$, i&
    path = Range("F2").Value & "\"
    For i = 2 To Cells(Rows.Count, 4).End(3).Row
        fileName = Dir(path & Replace(Cells(i, 4).Value, "-", "*") & ".*", vbHidden + vbNormal)

        Do While fileName <> ""
            SetAttr path & fileName, IIf(Cells(i, 5).Value = "unhide", vbNormal, vbHidden)
            fileName = Dir()
        Loop
    Next i

End Sub
One last favour I need . I need to know how I can make this work only from within an Active Workbook and Sheet named "HSheet" . At the moment I need to be on HSheet for this to work and will not work if I am at any other sheet. Thanks.
 
Upvote 0
Ok I got this by searching the web a little.

VBA Code:
Sub ChangeAtt()
    Dim path$, fileName$, i&
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("HSheet")
    path = ws.Range("B17").Value
    For i = 2 To ws.Cells(Rows.Count, 4).End(3).Row
        fileName = Dir(path & (ws.Cells(i, 4).Value) & "*", vbHidden + vbNormal)
        Do While fileName <> ""
            SetAttr path & fileName, IIf(ws.Cells(i, 5).Value = "unhide", vbNormal, vbHidden)
            fileName = Dir()
        Loop
    Next i
End Sub

Needed to set wb and ws properties
 
Upvote 0
I have very little English. Check out the solution if I don't get it wrong.
VBA Code:
Sub ChangeAtt()
    If ActiveWorkbook.Name <> "HSheet.xlsm" Or ActiveSheet.Name <> "HSheet" Then
        MsgBox "ActiveWorkbook.Name <> 'HSheet' Or ActiveSheet.Name <> 'HSheet'", vbCritical
        Exit Sub
    End If
    Dim path$, fileName$, i&
   
    path = Range("B17").Value
    For i = 2 To Cells(Rows.Count, 4).End(3).Row
        fileName = Dir(path & (Cells(i, 4).Value) & "*", vbHidden + vbNormal)
        Do While fileName <> ""
            SetAttr path & fileName, IIf(ws.Cells(i, 5).Value = "unhide", vbNormal, vbHidden)
            fileName = Dir()
        Loop
    Next i
    MsgBox "Ok..."
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

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