VBA to delete all columns except "" for all workbooks in a folder

thedeadzeds

Active Member
Joined
Aug 16, 2011
Messages
451
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I've found the following code which works great. Is there a way to get this code to open all workbooks in a folder and run this for all worksheets called 'export' .

Thanks


VBA Code:
Sub DeleteSelectedColumns()
Dim currentColumn As Integer
Dim columnHeading As String

For currentColumn = ActiveSheet.UsedRange.Columns.Count To 1 Step -1
    columnHeading = ActiveSheet.UsedRange.Cells(1, currentColumn).Value

'Check whether to preserve the column
    Select Case columnHeading
    'Insert name of columns to preserve
        Case "Date", "Name", "Amount Owing", "Balance"
            'Do nothing
        Case Else
            'Delete the column
            ActiveSheet.Columns(currentColumn).Delete
        End Select
    Next
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
You could loop through a folder, open only the Excel documents and then run the code if one of the names of the sheets are 'export'.

VBA Code:
Sub LoopThroughFiles()

    Application.ScreenUpdating = False 'comment this out if you would like to see what is going on

    Dim wbk As Workbook
    Dim MyFile As String
    Dim ws As Worksheet
   
    Dim MyFolder As String
    MyFolder = ("C:\testFolder\")
    MyFile = Dir(MyFolder) 'DIR gets the first file of the folder
    'Loop through all files in a folder until DIR cannot find anymore
    Do While MyFile <> ""
       
        If InStr(CStr(MyFile), ".xls") Or InStr(CStr(MyFile), ".xlsx") Or InStr(CStr(MyFile), ".xlsm") Or InStr(CStr(MyFile), ".xlm") Then
            'Opens the file and assigns to the wbk variable for future use
            Set wbk = Workbooks.Open(Filename:=MyFolder & MyFile)
            'Checks to see the sheet name and then runs the code if it matches
            For Each ws In wbk.Worksheets
                If ws.Name = "export" Then DeleteSelectedColumns
            Next
            wbk.Close
           
        End If
       
       
        MyFile = Dir 'DIR gets the next file in the folder
    Loop
   
    Application.ScreenUpdating = True
 
    MsgBox ("Completed")
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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