Loop through a folder with multiple csv files, delete empty column and save as text files

isanka88

New Member
Joined
Dec 12, 2020
Messages
13
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi Good People.
I need a Macro to do the following
1. Loop through a folder with multiple csv files
2. In each csv file, delete all columns except Column G
3. Delete header Row
4. Delete empty rows in Column A
5. Save all CSV files as text files in the same folder with the same file name.

All CSV files have the same format.

Below code does 1, 2, 3 & 5. I need to modify this to include 4. ( After 1,2,3 executed there will be only A column. I need to delete empty rows in column A)


VBA Code:
Option Explicit

Sub Delete_First_Last_Columns_From_CSV_Files()

    Dim source_folder_name As String
    source_folder_name = "C:\Users\Isanka Rangana\Desktop\Macro\CSV Files" 'change the path to the source folder accordingly
   
    If Right(source_folder_name, 1) <> "\" Then
        source_folder_name = source_folder_name & "\"
    End If
   
    If Len(source_folder_name) = 0 Then
        MsgBox "The path to the source folder is invalid!", vbExclamation, "Invalid Path"
        Exit Sub
    End If
   
    Application.ScreenUpdating = False

    
    Dim columns_to_delete As Variant
    columns_to_delete = Array("Date", " Time", "ColC", "ColE", "ColF", "ColG") 'change and/or add column headers as desired
   
    Dim current_filename As String
    current_filename = Dir(source_folder_name & "*.csv", vbNormal) 
         Dim rows_to_delete As Variant
    rows_to_delete = Array("Date", " Time", "ColC", "ColE", "ColF", "ColG") 'change and/or add column headers as desired
   
       Dim file_count As Long
    While Len(current_filename) > 0
        file_count = file_count + 1
        Delete_Columns_from_CSV_File source_folder_name & current_filename, columns_to_delete
         Delete_rows_from_CSV_File source_folder_name & current_filename, rows_to_delete
        current_filename = Dir
    Wend
       
    Application.ScreenUpdating = True
   
    MsgBox "Number of files processed: " & file_count, vbInformation, "Files Processed"
   
End Sub

Private Sub Delete_Columns_from_CSV_File(ByVal source_filename As String, ByVal columns_to_delete As Variant)

    Dim source_workbook As Workbook
    Set source_workbook = Workbooks.Open(Filename:=source_filename)
   
    Dim source_worksheet As Worksheet
    Set source_worksheet = source_workbook.Worksheets(1)
   
    Dim column_found As Range
    Dim i As Long
    For i = LBound(columns_to_delete) To UBound(columns_to_delete)
        Set column_found = source_worksheet.Rows(1).Find(what:=columns_to_delete(i), LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
        If Not column_found Is Nothing Then
            column_found.EntireColumn.Delete
        End If
    Next i
    source_workbook.Close SaveChanges:=True
    
    End Sub
        
    Private Sub Delete_rows_from_CSV_File(ByVal source_filename As String, ByVal rows_to_delete As Variant)

    Dim source_workbook As Workbook
    Set source_workbook = Workbooks.Open(Filename:=source_filename)
   
    Dim source_worksheet As Worksheet
    Set source_worksheet = source_workbook.Worksheets(1)
   
    Dim rows_found As Range
    Dim i As Long
    For i = LBound(rows_to_delete) To UBound(rows_to_delete)
        Set rows_found = source_worksheet.Rows(1)
        If Not rows_found Is Nothing Then
            rows_found.EntireRow.Delete
        End If
    Next i
       
 source_workbook.SaveAs FileFormat:= _
    xlText, CreateBackup:=False
    
    source_workbook.Close SaveChanges:=True

End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
...I need to modify this to include 4.

VBA Code:
Private Sub Delete_empty_rows_in_Column_A(ByVal source_filename As String)

    Dim source_workbook As Workbook
    Set source_workbook = Workbooks.Open(Filename:=source_filename)

    Dim WS As Worksheet
    Dim FilterRange As Range, DataRange As Range, DeleteRange As Range

    Set WS = source_workbook.Worksheets(1)
    WS.AutoFilterMode = False
    Application.ScreenUpdating = False

    Set FilterRange = WS.Range("A1", WS.Range("A" & WS.Rows.Count).End(xlUp))    'FilterRange must always include a header row
    With FilterRange
        Set DataRange = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count)
    End With

    FilterRange.AutoFilter Field:=1, Criteria1:=""
    Set DeleteRange = Application.Intersect(FilterRange.SpecialCells(xlCellTypeVisible).EntireRow, DataRange.EntireRow)
    WS.AutoFilterMode = False

    If Not DeleteRange Is Nothing Then
        DeleteRange.Delete                            'delete empty rows
        source_workbook.Close SaveChanges:=True
    Else
        source_workbook.Close SaveChanges:=False
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hey rlv01
Thank you very much for your valuable input. I tried to add your code. But it does not delete empty rows in my csv files. The format of my CSV files are as below. Could you kindly check. I need to remove empty rows
 

Attachments

  • Capture.PNG
    Capture.PNG
    74.8 KB · Views: 10
Upvote 0
1. A lot depends on how you added that code. Perhaps you can post that. You must provide the file name as you do for your other subroutines.
2. Use the Visual Basic Editor (VBE) to single step through the code to make sure you are opening the file you expect to open and transition to the subroutine I posted.
 
Upvote 0
Hey rlv01
Thank you for helping me. This is how I inserted your code. Could you kindly help me to sort this out. I am newbie in visual basic
VBA Code:
Option Explicit

Sub Delete_First_Last_Columns_From_CSV_Files()

    Dim source_folder_name As String
    source_folder_name = "C:\Users\Isanka Rangana\Desktop\Macro\CSV Files" 'change the path to the source folder accordingly
   
    If Right(source_folder_name, 1) <> "\" Then
        source_folder_name = source_folder_name & "\"
    End If
   
    If Len(source_folder_name) = 0 Then
        MsgBox "The path to the source folder is invalid!", vbExclamation, "Invalid Path"
        Exit Sub
    End If
   
    Application.ScreenUpdating = False

    
    Dim columns_to_delete As Variant
    columns_to_delete = Array("Date", " Time", "ColC", "ColE", "ColF", "ColG") 'change and/or add column headers as desired
   
    Dim current_filename As String
    current_filename = Dir(source_folder_name & "*.csv", vbNormal)
    
    
    
         Dim rows_to_delete As Variant
    rows_to_delete = Array("Date", " Time", "ColC", "ColE", "ColF", "ColG") 'change and/or add column headers as desired
   

   
    Dim file_count As Long
    While Len(current_filename) > 0
        file_count = file_count + 1
        Delete_Columns_from_CSV_File source_folder_name & current_filename, columns_to_delete
         Delete_rows_from_CSV_File source_folder_name & current_filename, rows_to_delete
        current_filename = Dir
    Wend
    
   
    Application.ScreenUpdating = True
   
    MsgBox "Number of files processed: " & file_count, vbInformation, "Files Processed"
   
End Sub

Private Sub Delete_Columns_from_CSV_File(ByVal source_filename As String, ByVal columns_to_delete As Variant)

    Dim source_workbook As Workbook
    Set source_workbook = Workbooks.Open(Filename:=source_filename)
   
    Dim source_worksheet As Worksheet
    Set source_worksheet = source_workbook.Worksheets(1)
   
    Dim column_found As Range
    Dim i As Long
    For i = LBound(columns_to_delete) To UBound(columns_to_delete)
        Set column_found = source_worksheet.Rows(1).Find(what:=columns_to_delete(i), LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
        If Not column_found Is Nothing Then
            column_found.EntireColumn.Delete
        End If
    Next i
    source_workbook.Close SaveChanges:=True
    
    End Sub
    
    
    Private Sub Delete_empty_rows_in_Column_A(ByVal source_filename As String)

    Dim source_workbook As Workbook
    Set source_workbook = Workbooks.Open(Filename:=source_filename)

    Dim WS As Worksheet
    Dim FilterRange As Range, DataRange As Range, DeleteRange As Range

    Set WS = source_workbook.Worksheets(1)
    WS.AutoFilterMode = False
    Application.ScreenUpdating = False

    Set FilterRange = WS.Range("A1", WS.Range("A" & WS.Rows.Count).End(xlUp))    'FilterRange must always include a header row
    With FilterRange
        Set DataRange = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count)
    End With

    FilterRange.AutoFilter Field:=1, Criteria1:=""
    Set DeleteRange = Application.Intersect(FilterRange.SpecialCells(xlCellTypeVisible).EntireRow, DataRange.EntireRow)
    WS.AutoFilterMode = False

    If Not DeleteRange Is Nothing Then
        DeleteRange.Delete                            'delete empty rows
        source_workbook.Close SaveChanges:=True
    Else
        source_workbook.Close SaveChanges:=False
    End If
    Application.ScreenUpdating = True
End Sub
    
    
    
    Private Sub Delete_rows_from_CSV_File(ByVal source_filename As String, ByVal rows_to_delete As Variant)

    Dim source_workbook As Workbook
    Set source_workbook = Workbooks.Open(Filename:=source_filename)
   
    Dim source_worksheet As Worksheet
    Set source_worksheet = source_workbook.Worksheets(1)
   
    Dim rows_found As Range
    Dim i As Long
    For i = LBound(rows_to_delete) To UBound(rows_to_delete)
        Set rows_found = source_worksheet.Rows(1)
        If Not rows_found Is Nothing Then
            rows_found.EntireRow.Delete
        End If
    Next i
    
   
 source_workbook.SaveAs FileFormat:= _
    xlText, CreateBackup:=False
     
    
    source_workbook.Close SaveChanges:=True

End Sub
 
Upvote 0
Hey rlv01
Thank you for helping me. This is how I inserted your code. Could you kindly help me to sort this out. I am newbie in visual basic

VBA Code:
Sub Delete_First_Last_Columns_From_CSV_Files()

    Dim source_folder_name As String
    source_folder_name = "C:\Users\Isanka Rangana\Desktop\Macro\CSV Files"    'change the path to the source folder accordingly

    If Right(source_folder_name, 1) <> "\" Then
        source_folder_name = source_folder_name & "\"
    End If

    'new path validation
    With CreateObject("Scripting.FileSystemObject")
        If Not .FolderExists(source_folder_name) Then
            MsgBox "The path to the source folder is invalid!" & vbCr & vbCr & "Folder: " & source_folder_name, vbOKOnly Or vbExclamation, "Invalid Path"
            Exit Sub
        End If
    End With

    Application.ScreenUpdating = False

    Dim columns_to_delete As Variant
    columns_to_delete = Array("Date", " Time", "ColC", "ColE", "ColF", "ColG")    'change and/or add column headers as desired

    Dim current_filename As String
    current_filename = Dir(source_folder_name & "*.csv", vbNormal)

    Dim rows_to_delete As Variant
    rows_to_delete = Array("Date", " Time", "ColC", "ColE", "ColF", "ColG")    'change and/or add column headers as desired

    Dim file_count As Long
    While Len(current_filename) > 0
        file_count = file_count + 1
        Delete_Columns_from_CSV_File source_folder_name & current_filename, columns_to_delete
        DoEvents                                                               '<- added
        Delete_empty_rows_in_Column_A source_folder_name & current_filename    '<- added
        DoEvents                                                               '<- added
        Delete_rows_from_CSV_File source_folder_name & current_filename, rows_to_delete
        DoEvents                                                               '<- added
        current_filename = Dir
    Wend

    Application.ScreenUpdating = True
    MsgBox "Number of files processed: " & file_count, vbInformation, "Files Processed"
End Sub
 
Upvote 0
Solution
Hey rlv01
First of all let me thank you for your valuable support. It is almost out of verbal definitions to thank you. I managed to make the code as below. Now I want to Save all CSV files as text files in the same folder with the same file name. I tried below but it did not work. Could you kindly help me on this?
source_workbook.SaveAs FileFormat:= _
xlText, CreateBackup:=False

VBA Code:
Option Explicit

Sub Delete_First_Last_Columns_From_CSV_Files()

    Dim source_folder_name As String
    source_folder_name = "C:\Users\Isanka Rangana\Desktop\Macro\CSV Files" 'change the path to the source folder accordingly
   
    If Right(source_folder_name, 1) <> "\" Then
        source_folder_name = source_folder_name & "\"
    End If
   
 'new path validation
    With CreateObject("Scripting.FileSystemObject")
        If Not .FolderExists(source_folder_name) Then
            MsgBox "The path to the source folder is invalid!" & vbCr & vbCr & "Folder: " & source_folder_name, vbOKOnly Or vbExclamation, "Invalid Path"
            Exit Sub
        End If
    End With
   
    Application.ScreenUpdating = False

    
    Dim columns_to_delete As Variant
    columns_to_delete = Array("Date", " Time", "ColC", "ColE", "ColF", "ColG") 'change and/or add column headers as desired
   
    Dim current_filename As String
    current_filename = Dir(source_folder_name & "*.csv", vbNormal)
    
    
    
         Dim rows_to_delete As Variant
    rows_to_delete = Array("Date", " Time", "ColC", "ColE", "ColF", "ColG") 'change and/or add column headers as desired
   

   
    Dim file_count As Long
    While Len(current_filename) > 0
        file_count = file_count + 1
        
        
        
        
        Delete_Columns_from_CSV_File source_folder_name & current_filename, columns_to_delete
        DoEvents
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        Delete_empty_rows_in_Column_A source_folder_name & current_filename    '<- added
        DoEvents
        
        
        
        
        
        
        
        
        
        
        
        
        
        Delete_rows_from_CSV_File source_folder_name & current_filename, rows_to_delete
        DoEvents
        current_filename = Dir
    
    
    
    
    Wend
    
   
    Application.ScreenUpdating = True
   
    MsgBox "Number of files processed: " & file_count, vbInformation, "Files Processed"
   
End Sub

Private Sub Delete_Columns_from_CSV_File(ByVal source_filename As String, ByVal columns_to_delete As Variant)

    Dim source_workbook As Workbook
    Set source_workbook = Workbooks.Open(Filename:=source_filename)
   
    Dim source_worksheet As Worksheet
    Set source_worksheet = source_workbook.Worksheets(1)
   
    Dim column_found As Range
    Dim i As Long
    For i = LBound(columns_to_delete) To UBound(columns_to_delete)
        Set column_found = source_worksheet.Rows(1).Find(what:=columns_to_delete(i), LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
        If Not column_found Is Nothing Then
            column_found.EntireColumn.Delete
        End If
    Next i
    source_workbook.Close SaveChanges:=True
    
    End Sub
    

Private Sub Delete_empty_rows_in_Column_A(ByVal source_filename As String)

    Dim source_workbook As Workbook
    Set source_workbook = Workbooks.Open(Filename:=source_filename)

    Dim WS As Worksheet
    Dim FilterRange As Range, DataRange As Range, DeleteRange As Range

    Set WS = source_workbook.Worksheets(1)
    WS.AutoFilterMode = False
    Application.ScreenUpdating = False

    Set FilterRange = WS.Range("A1", WS.Range("A" & WS.Rows.Count).End(xlUp))    'FilterRange must always include a header row
    With FilterRange
        Set DataRange = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count)
    End With

    FilterRange.AutoFilter Field:=1, Criteria1:=""
    Set DeleteRange = Application.Intersect(FilterRange.SpecialCells(xlCellTypeVisible).EntireRow, DataRange.EntireRow)
    WS.AutoFilterMode = False

    If Not DeleteRange Is Nothing Then
        DeleteRange.Delete                            'delete empty rows
        source_workbook.Close SaveChanges:=True
   
    End If
    Application.ScreenUpdating = True
End Sub

    
    Private Sub Delete_rows_from_CSV_File(ByVal source_filename As String, ByVal rows_to_delete As Variant)

    Dim source_workbook As Workbook
    Set source_workbook = Workbooks.Open(Filename:=source_filename)
   
    Dim source_worksheet As Worksheet
    Set source_worksheet = source_workbook.Worksheets(1)
   
    Dim rows_found As Range
    Dim i As Long
    For i = LBound(rows_to_delete) To UBound(rows_to_delete)
        Set rows_found = source_worksheet.Rows(1)
        If Not rows_found Is Nothing Then
            rows_found.EntireRow.Delete
        End If
    Next i
    
     source_workbook.SaveAs FileFormat:= _
    xlText, CreateBackup:=False
    
    source_workbook.Close SaveChanges:=True


End Sub
 
Upvote 0
@isanka88 - the marked solution post is supposed to be the solution post that answered your question that would also help future readers.
The subroutine that you posted containing the solution that answers the original question has been provided in post #2 and also detailed in post #6 by @rlv01. Therefore, I switched the marked solution post accordingly.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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