+ Reply to Thread
Results 1 to 6 of 6

Hide column in multiple files in subfolders from folder

Hybrid View

  1. #1
    Registered User
    Join Date
    05-26-2020
    Location
    Odense, Denmark
    MS-Off Ver
    2020 - 365
    Posts
    31

    Hide column in multiple files in subfolders from folder

    Hi Forum,

    I've been searching for a VBA solution for a long time and tried multiple threads on the web, but have not been able to make a final solution... So I need help!

    I have a bigger Excel "eco system", which contains multiple worksheets in multiple workbooks.

    Initial facts:
    • The workbooks are placed in subfolders within a main folder
    • The main folder is currently placed inside a Sharepoint environment
    • The Workbooks could contain multiple sheets to be considered
      - If sheet contains date in cell D2, it should be considered
      - sheets with the name "Log" and "Instruction" should never be considered



    What I want the VBA to do:
    • An initial inputbox where to enter week number and year.
    • Find the column where week number and year matches and hide the column.
    • Proceed to next sheet if available or move further on to the next subfolder if available.
    • Some sort of error handling / error messaging. "An error occured in file X, sheet X. Would you like to proceed or stop?"
    • Possibility to use the VBA even though some of the sheets already have the matching column date hidden.
    • Final MsgBox if the VBA succeeded.



    Is there anyone out there, that have the time and VBA experience to help me with a proper solution?
    Hope my explanation make sense... I would gladly add more info if needed. I've attched a simplfied version of the "eco system" for testing.

    BR, Rasmus
    Attached Files Attached Files
    Last edited by Ralleberg; 02-03-2022 at 10:41 AM. Reason: The problem is solved

  2. #2
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2503
    Posts
    1,459

    Re: Hide column in multiple files in subfolders from folder

    Hi Ralleberg,

    I don't have a sharpoint folder source to test on but the below works in a normal environment in regards to the folder and subfolders.

    If it finds that the column is already hidden then it will move on, as far as the error handling goes we would need to see the potential errors that could be created to be able to haandle them. The code will generally just stop if there is an error until the error is handled - it is within the handling part where you could set up an error log. Anyway I digress, I will leave the code below:

    Dim wkNum As Long, dte As Date
    Sub HideColumnsSubfolder()
        Dim FileSystem As Object
        Dim HostFolder As String
        
        wkNum = InputBox("Enter week number") 'week number
        dte = InputBox("Enter date dd/mm/yyyy") ' date
        HostFolder = "C:\Users\JBloggs\Desktop\Disp test\" ' <<< change to your folder
        Set FileSystem = CreateObject("Scripting.FileSystemObject")
        DoFolder FileSystem.GetFolder(HostFolder)
        MsgBox "Done"
    End Sub
    
    Private Sub DoFolder(Folder)
        Dim SubFolder, File, wb As Workbook, ws As Worksheet, w As Long, d As Long, str As String
        
        For Each SubFolder In Folder.SubFolders
            DoFolder SubFolder
        Next
        For Each File In Folder.Files
            Set wb = Workbooks.Open(File)
            For Each ws In wb.Worksheets
                If ws.Name <> "Log" And ws.Name <> "Instruction" Then
                    If IsDate(ws.Range("D2")) Then
                        On Error GoTo nextWS
                            w = ws.Rows(1).Find(What:="Week " & wkNum, LookIn:=xlValues, LookAt:=xlWhole).Column
                            d = ws.Rows(2).Find(What:=dte, LookIn:=xlValues, LookAt:=xlWhole).Column
                        On Error GoTo 0
                        If w = d Then
                            ws.Columns(w).Hidden = True
                        End If
                    End If
                End If
    nextWS:
            Next ws
            wb.Close True
        Next
    End Sub
    Hope this helps

  3. #3
    Registered User
    Join Date
    05-26-2020
    Location
    Odense, Denmark
    MS-Off Ver
    2020 - 365
    Posts
    31

    Re: Hide column in multiple files in subfolders from folder

    Quote Originally Posted by CheeseSandwich View Post
    Hi Ralleberg,

    I don't have a sharpoint folder source to test on but the below works in a normal environment in regards to the folder and subfolders.

    If it finds that the column is already hidden then it will move on, as far as the error handling goes we would need to see the potential errors that could be created to be able to haandle them. The code will generally just stop if there is an error until the error is handled - it is within the handling part where you could set up an error log. Anyway I digress, I will leave the code below:

    Dim wkNum As Long, dte As Date
    Sub HideColumnsSubfolder()
        Dim FileSystem As Object
        Dim HostFolder As String
        
        wkNum = InputBox("Enter week number") 'week number
        dte = InputBox("Enter date dd/mm/yyyy") ' date
        HostFolder = "C:\Users\JBloggs\Desktop\Disp test\" ' <<< change to your folder
        Set FileSystem = CreateObject("Scripting.FileSystemObject")
        DoFolder FileSystem.GetFolder(HostFolder)
        MsgBox "Done"
    End Sub
    
    Private Sub DoFolder(Folder)
        Dim SubFolder, File, wb As Workbook, ws As Worksheet, w As Long, d As Long, str As String
        
        For Each SubFolder In Folder.SubFolders
            DoFolder SubFolder
        Next
        For Each File In Folder.Files
            Set wb = Workbooks.Open(File)
            For Each ws In wb.Worksheets
                If ws.Name <> "Log" And ws.Name <> "Instruction" Then
                    If IsDate(ws.Range("D2")) Then
                        On Error GoTo nextWS
                            w = ws.Rows(1).Find(What:="Week " & wkNum, LookIn:=xlValues, LookAt:=xlWhole).Column
                            d = ws.Rows(2).Find(What:=dte, LookIn:=xlValues, LookAt:=xlWhole).Column
                        On Error GoTo 0
                        If w = d Then
                            ws.Columns(w).Hidden = True
                        End If
                    End If
                End If
    nextWS:
            Next ws
            wb.Close True
        Next
    End Sub
    Hope this helps
    Hi again,

    I need an addition to the code above that I need assistance with. The code looks correctly in the selected folders, but also takes .pdf and other file types into the loop, and makes quite a few corrupt files.
    the code should only take file types like .xlsm, xlsx to be taken into account within the specified folders, but how and where do I add this the code?

    Thank you in advance, Rasmus

  4. #4
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Hide column in multiple files in subfolders from folder

    Here is another approach for you. Place code in a separate workbook not inside eco system and only run the first sub, which then runs the second sub. This only selects the week, not the year. Let us know if you have questions.

    Sub loopAllSubFoldersSelectDirHideWeek()
    Dim x As Long
    Dim FolderName As String
    
    x = Application.InputBox("Enter the week number you want hidden", , , , , , 1)
    
    With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Please select the Main folder"
            .AllowMultiSelect = False
            .Show
            FolderName = .SelectedItems(1) & "\"
        End With
    'Call LoopAllSubFolders("C:\Users\man\OneDrive\Documents\Excel code\TEST\Disp test\", x) 'Use this line to hard code your path if you want
    Call loopAllSubFolders(FolderName, x)
    MsgBox "Complete"
    End Sub
    
    Sub loopAllSubFolders(ByVal folderPath As String, x As Long)
    Dim fileName As String
    Dim fullFilePath As String
    Dim numFolders As Long
    Dim folders() As String
    Dim i As Long, m&
    Dim ws As Worksheet
    
    If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
    fileName = Dir(folderPath & "*.*", vbDirectory)
    While Len(fileName) <> 0
        If Left(fileName, 1) <> "." Then
            fullFilePath = folderPath & fileName
            If (GetAttr(fullFilePath) And vbDirectory) = vbDirectory Then
                ReDim Preserve folders(0 To numFolders) As String
                folders(numFolders) = fullFilePath
                numFolders = numFolders + 1
            Else
                Application.Workbooks.Open (folderPath & fileName)
                For Each ws In ActiveWorkbook.Sheets
                    On Error Resume Next
                    m = 0
                    m = Application.Match("Week " & x, ws.Range("1:1"), 0)
                    If m > 0 Then
                        ws.Columns(m).EntireColumn.Hidden = True
                    End If
                    On Error GoTo 0
                Next
                ActiveWorkbook.Saved = True
                Application.DisplayAlerts = False
                    Workbooks(fileName).Close
                Application.DisplayAlerts = True
            End If
        End If
        fileName = Dir()
    Wend
    
    For i = 0 To numFolders - 1
        loopAllSubFolders folders(i), x
    Next i
    End Sub
    Last edited by maniacb; 01-19-2022 at 11:32 PM.

  5. #5
    Registered User
    Join Date
    05-26-2020
    Location
    Odense, Denmark
    MS-Off Ver
    2020 - 365
    Posts
    31

    Re: Hide column in multiple files in subfolders from folder

    Thank you both CheeseSandwich and maniacb.

    Both approaches did the job. Thank you so much. I've added rep for the answers.

  6. #6
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2503
    Posts
    1,459

    Re: Hide column in multiple files in subfolders from folder

    Maybe one option:
    Private Sub DoFolder(Folder)
        Dim SubFolder, File, wb As Workbook, ws As Worksheet, w As Long, d As Long, str As String
        
        For Each SubFolder In Folder.SubFolders
            DoFolder SubFolder
        Next
        For Each File In Folder.Files
            If Left(Right(File, 5), 4) <> ".xls" Then GoTo nextFile
            Set wb = Workbooks.Open(File)
            For Each ws In wb.Worksheets
                If ws.Name <> "Log" And ws.Name <> "Instruction" Then
                    If IsDate(ws.Range("D2")) Then
                        On Error GoTo nextWS
                            w = ws.Rows(1).Find(What:="Week " & wkNum, LookIn:=xlValues, LookAt:=xlWhole).Column
                            d = ws.Rows(2).Find(What:=dte, LookIn:=xlValues, LookAt:=xlWhole).Column
                        On Error GoTo 0
                        If w = d Then
                            ws.Columns(w).Hidden = True
                        End If
                    End If
                End If
    nextWS:
            Next ws
            wb.Close True
    nextFile:
        Next
    End Sub
    If things don't change they stay the same

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] list of subfolders in folder - without files and sub-subfolders
    By MartyZ in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-11-2022, 10:56 AM
  2. [SOLVED] Move Files from current folder to a new subfolders specified in a column
    By zEKeBv in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-31-2020, 05:48 AM
  3. VBA Copy specific files listed in Excel from folder & subfolders to another folder - FSO
    By HelloFriends in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-03-2019, 02:55 PM
  4. Move pdf files from multiple subfolders to a new folder
    By fabian_76 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-12-2018, 04:57 PM
  5. VBA that goes through all xls files in folder and subfolders
    By Acxer in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-18-2015, 09:06 AM
  6. [SOLVED] Combining multiple files to workbook from folder, including subfolders
    By Punssiliini in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-05-2015, 02:38 PM
  7. Counting files in folder including subfolders also and folder size
    By mido609 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-12-2012, 03:26 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1