remove columns from multiple Excel-files in a folder

gjvv

New Member
Joined
Mar 20, 2023
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I have multiple .xls files in a folder, and from each file/workbook I need to delete some columns (B to D, column L , column S to V).
Then I would save the files into the same folder, to .xlsx, if possible. IF not possible, I would save them automatically in to a subfolder "converted".
In steps:

1. Select the folder with the files (.xls)
2. Open the first file
3. Remove the columns
4. Save as .xlsx (same file name), (or save in different folder "converted" within the original folder)
5. Close the file
6. Open next file (then step 3,4,5 again)

I tried many codes from other topics here, but every single one of them did something I just didn't want.

Please, some help here.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
The following code should give an idea.
The code assumes the worksheet that columns will be removed is the first worksheet in the workbooks.
I recommend saving the files in the subfolder, and the code does that. However, you can set strConv = ""

VBA Code:
Sub doIt()
Dim strPath As String
Dim strConv As String
Dim strFile As String
Dim wrk As Workbook
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False 'To silence the Overwrite File message - if the files are being overwritten
    ' The xls file folder path
    strPath = "C:\SomeFolder\"
    ' Set the following variable as an emptry string to overwrite the existing file
    strConv = "converted\"
    strFile = Dir(strPath & "*.xls")
    On Error GoTo ErrHandler
    Do While Len(strFile) > 0
        DoEvents
        Set wrk = Application.Workbooks.Open(strPath & strFile)
        With wrk
            .Worksheets(1).Range("B:D,L:L,S:V").Delete
            .SaveAs strPath & strConv & Left(strFile, Len(strFile) - 4), xlWorkbookDefault
            .Close
        End With
        strFile = Dir
    Loop
ErrHandler:
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    If Err.Number Then
        MsgBox Err.Description, vbOKOnly + vbCritical
    End If
End Sub
 
Upvote 0
The following code should give an idea.
The code assumes the worksheet that columns will be removed is the first worksheet in the workbooks.
I recommend saving the files in the subfolder, and the code does that. However, you can set strConv = ""

VBA Code:
Sub doIt()
Dim strPath As String
Dim strConv As String
Dim strFile As String
Dim wrk As Workbook
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False 'To silence the Overwrite File message - if the files are being overwritten
    ' The xls file folder path
    strPath = "C:\SomeFolder\"
    ' Set the following variable as an emptry string to overwrite the existing file
    strConv = "converted\"
    strFile = Dir(strPath & "*.xls")
    On Error GoTo ErrHandler
    Do While Len(strFile) > 0
        DoEvents
        Set wrk = Application.Workbooks.Open(strPath & strFile)
        With wrk
            .Worksheets(1).Range("B:D,L:L,S:V").Delete
            .SaveAs strPath & strConv & Left(strFile, Len(strFile) - 4), xlWorkbookDefault
            .Close
        End With
        strFile = Dir
    Loop
ErrHandler:
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    If Err.Number Then
        MsgBox Err.Description, vbOKOnly + vbCritical
    End If
End Sub
Thnx! I will try this!
 
Upvote 0
The following code should give an idea.
The code assumes the worksheet that columns will be removed is the first worksheet in the workbooks.
I recommend saving the files in the subfolder, and the code does that. However, you can set strConv = ""

VBA Code:
Sub doIt()
Dim strPath As String
Dim strConv As String
Dim strFile As String
Dim wrk As Workbook
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False 'To silence the Overwrite File message - if the files are being overwritten
    ' The xls file folder path
    strPath = "C:\SomeFolder\"
    ' Set the following variable as an emptry string to overwrite the existing file
    strConv = "converted\"
    strFile = Dir(strPath & "*.xls")
    On Error GoTo ErrHandler
    Do While Len(strFile) > 0
        DoEvents
        Set wrk = Application.Workbooks.Open(strPath & strFile)
        With wrk
            .Worksheets(1).Range("B:D,L:L,S:V").Delete
            .SaveAs strPath & strConv & Left(strFile, Len(strFile) - 4), xlWorkbookDefault
            .Close
        End With
        strFile = Dir
    Loop
ErrHandler:
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    If Err.Number Then
        MsgBox Err.Description, vbOKOnly + vbCritical
    End If
End Sub
I got an error "Method SaveAs of object_Workbook failed"
 
Upvote 0
I got an error "Method SaveAs of object_Workbook failed"
Make sure you have the folder "converted" if you are using the sub folder.

To debug: Add the line below:
VBA Code:
        With wrk
            .Worksheets(1).Range("B:D,L:L,S:V").Delete
            Debug.print strPath & strConv & Left(strFile, Len(strFile) - 4) '<--- Add this line
            .SaveAs strPath & strConv & Left(strFile, Len(strFile) - 4), xlWorkbookDefault
            .Close
        End With

Run the code and look at the path printed in the immediate window (Ctrl + G). It is the path that the updated workbook is supposed to be saved in. There must be something wrong with the file path/name.

I have the following folder structure and it works as expected:

1729672297446.png
 
Upvote 0
Make sure you have the folder "converted" if you are using the sub folder.

To debug: Add the line below:
VBA Code:
        With wrk
            .Worksheets(1).Range("B:D,L:L,S:V").Delete
            Debug.print strPath & strConv & Left(strFile, Len(strFile) - 4) '<--- Add this line
            .SaveAs strPath & strConv & Left(strFile, Len(strFile) - 4), xlWorkbookDefault
            .Close
        End With

Run the code and look at the path printed in the immediate window (Ctrl + G). It is the path that the updated workbook is supposed to be saved in. There must be something wrong with the file path/name.

I have the following folder structure and it works as expected:

View attachment 118416
ha, ok! Thanx!
 
Upvote 0

Forum statistics

Threads
1,222,903
Messages
6,168,939
Members
452,227
Latest member
sam1121

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