Macro to remove all formulas and delete hidden sheets, columns and rows from multiple workbooks

Sorin P

New Member
Joined
Feb 9, 2022
Messages
8
Office Version
  1. 2016
Platform
  1. Windows
Hello!

I have to remove all data from multiple files and delete everything that is hidden, so I am looking for a macro that first it will remove all formulas (paste as values) from the files in folders and subfolders, afterwards remove hidden sheets, rows and columns.
I have found an old thread closest to my situation. Can anyone help me with the modifications?

VBA Code:
Sub RemoveFormulas()
  
    Dim fName$, wkb As Workbook, wks As Worksheet
    Const folder_path$ = "C:\Temp\Test\"
    Dim fso As Object, fld As Object, fl As Object

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    Set fso = CreateObject("Scripting.FileSystemObject")

    Call ProcessFolder(fso.GetFolder(folder_path))

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    MsgBox "Well done!", vbInformation
  
End Sub

Private Sub ProcessFolder(fld As Object)
    Dim fl As Object, subfld As Object
    For Each fl In fld.Files
        Call RemoveFormulasInternal(fl.Path)
    Next
    If fld.SubFolders.Count > 0 Then
        For Each subfld In fld.SubFolders
            Call ProcessFolder(subfld)
        Next
    End If
End Sub

Private Sub RemoveFormulasInternal(file_path$)
    Dim wkb As Workbook, wks As Worksheet
    Set wkb = Workbooks.Open(file_path, UpdateLinks:=False)
    For Each wks In wkb.Sheets
        wks.Range("A1:AZ100").Copy
        wks.Range("A1").PasteSpecial xlPasteValues
        Application.CutCopyMode = False
    Next
    wkb.Close SaveChanges:=True
End Sub

Thank you very much!
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Forum statistics

Threads
1,214,661
Messages
6,120,796
Members
448,994
Latest member
rohitsomani

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