How to delete multiple columns in excel without open files

navikaran1

New Member
Joined
Dec 14, 2018
Messages
2
I have many Excel file with multiple columns in the same folder. I just want to delete some Column, Each excel file contain the same heading.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Would this work?
Code:
Sub DeleteColumn()
'   Housekeeping
    MyPath = "C:\Junk"
    MyPath = Application.InputBox("Path:", "Path", MyPath)
    If "\" <> Right(MyPath, 1) Then
       MyPath = MyPath & "\"
    End If
    MyExt = "*.xls"
    FileKt = 0
    MyFile = ""
    
    MyFile = Dir(MyPath & MyExt)
    Do While MyFile <> "" And FileKt < 2000
        Workbooks.Open Filename:=MyPath & MyFile
        FileKt = FileKt + 1
        If FileKt Mod 500 = 0 Then
            'MsgBox "Processed " & FileKt & " files"
        End If
        If Application.ProtectedViewWindows.Count > 0 Then
            Application.ActiveProtectedViewWindow.Edit
        End If
         'Do the delete of the column here<do your="" delete="" of="" the="" column="" here="">
        ActiveWorkbook.Save
        ActiveWindow.Close
        MyFile = Dir
    Loop
    'MsgBox "Processed a total of " & FileKt & " files."
End Sub</do>
 
Last edited:
Upvote 0
Would this work?
Code:
Sub DeleteColumn()
'   Housekeeping
    MyPath = "C:\Junk"
    MyPath = Application.InputBox("Path:", "Path", MyPath)
    If "\" <> Right(MyPath, 1) Then
       MyPath = MyPath & "\"
    End If
    MyExt = "*.xls"
    FileKt = 0
    MyFile = ""
    
    MyFile = Dir(MyPath & MyExt)
    Do While MyFile <> "" And FileKt < 2000
        Workbooks.Open Filename:=MyPath & MyFile
        FileKt = FileKt + 1
        If FileKt Mod 500 = 0 Then
            'MsgBox "Processed " & FileKt & " files"
        End If
        If Application.ProtectedViewWindows.Count > 0 Then
            Application.ActiveProtectedViewWindow.Edit
        End If
         'Do the delete of the column here<do your="" delete="" of="" the="" column="" here="">
        ActiveWorkbook.Save
        ActiveWindow.Close
        MyFile = Dir
    Loop
    'MsgBox "Processed a total of " & FileKt & " files."
End Sub</do>






thank you for reply,


Where we dfine the cloume which i want to delte,
and when i run this vba, Its open that file.
 
Upvote 0
That code will just open and close .xls files in the specified directory (C:\Junk in this case). You can change MyExt to "*.xls*" which will pick up .xls, .xlsx, .xlsm and what other .xls* file is in the folder - please be careful.
In the Do While loop the file found in the directory will be opened, counted, read-only turned off, and then saved and closed, then loop back for the next file till no more are found or 2000 files processed - increase if needed.
Just prior to the save there's a comment to "'Do the delete of the column here" - this is where you can specify the column, or preferred: find the column name first prior to delete, and delete if column exists.
 
Upvote 0

Forum statistics

Threads
1,223,702
Messages
6,173,961
Members
452,539
Latest member
delvey

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