VBA to loop through all csv files in folder and delete specific columns

Pngn99

New Member
Joined
Jan 29, 2018
Messages
3
I have a folder that contains 100 csv files. I need to delete the same columns from each of these files. I have tried several variations of the code below without success. I keep receiving a syntax error. I have tried everything I can find and keep striking out. Any help would be greatly appreciated!

Sub LoopAllFilesInFolder()
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

myPath = "C:\Desktop\Test"
myExtension = "*.csv*"

myFile = Dir(myPath & myExtension)
Do While MyFile <> ""
If Len(myFile) = 0 Then Exit Do
Set wb = Workbooks.Open(Filename:=myPath & myFile)

Columns("B:Y").EntireColumn.Delete
wb.Close SaveChanges:=True
End If

myFile = Dir
Loop

'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
What happens if you remove the End If line
 
Upvote 0
What error do you get & what line is highlighted?
 
Upvote 0
I cleaned up some of the indents and then it worked. I still have no idea what was causing the issue, but it's done. Thanks much for responding!
 
Upvote 0
Glad you got it sorted & thanks for the feedback
 
Upvote 0
Here is the code that worked for me. Just make sure you put \ after your folder address.

Sub LoopAllFilesInFolder()
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

myPath = "your path\"
myExtension = "*.xls*"

myFile = Dir(myPath & myExtension)
Do While myFile <> ""
If Len(myFile) = 0 Then Exit Do
Set wb = Workbooks.Open(Filename:=myPath & myFile)
Columns("R:S").EntireColumn.Delete
wb.Close SaveChanges:=True


myFile = Dir
Loop

'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
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