Amend column contents within multiple CSV files where all of the files contain columns with the same order and format

TonWales

New Member
Joined
Oct 28, 2023
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
I have a folder containing multiple CSV files. All of the files contain columns in the same order and format. One of the columns (column C) has incorrect data and needs to be replaced with correct data.

So, I wish to remove the contents of Column C (row 11 through to row 2029) in all of the CSV files within the same folder and replace with contents (Column C, from Row 11 to Row 2029) from a 'master' CSV file.

I have been struggling to figure out a macro to go through every CSV in the folder that will do the necessary amendments efficiently. Can this be done in Excel using VBA?

I'd be very grateful if someone could help me with a suitable macro. Thank you :)
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
How many csv files need to be updated? I am sure it can be done with a macro by looping through all the csv files. Are all the files in a specific folder? Is the master csv file in the same folder? How big are the csv files in KB? Do you have some code that is not working as expected?

Doug
 
Upvote 0
Hi, I have over 600 csv files to amend but this can be done in several chunks if that makes things easier.
Yes, all the files, including the master csv file, are in a specific folder. File size is small, between 4 - 8KBs.
I've had a few attempts at writing code (essentially amending what I've found on the web as I'm very new to VBA) but the difficulty is specifying the cell range within a specific column that requires amending within in the macro. I hope this makes sense?
 
Upvote 0
Right, if you said 20 files, I would have said it would be quicker to open them one at a time and copy/paste. There may be a way to do it without opening each csv in Excel, but I am not aware of how to do that. I would get the macro to loop through all csv files that need to change, open each in Excel, replace the data as specified, save, and close. I have no idea how long it would take to process 600 files. I do know it would be much faster than doing it manually:)

I have to go do yard work, I will contemplate an approach while working. Maybe someone else will have this worked-out before I get a chance to get back to it... In any case, I believe to be possible.

Maybe post some of your code and identify where it doesn't seem to work right...

Doug
 
Upvote 0
I have a folder containing multiple CSV files. All of the files contain columns in the same order and format. One of the columns (column C) has incorrect data and needs to be replaced with correct data.

So, I wish to remove the contents of Column C (row 11 through to row 2029) in all of the CSV files within the same folder and replace with contents (Column C, from Row 11 to Row 2029) from a 'master' CSV file.

I have been struggling to figure out a macro to go through every CSV in the folder that will do the necessary amendments efficiently. Can this be done in Excel using VBA?

I'd be very grateful if someone could help me with a suitable macro. Thank you :)
Try this but back up all of your files before you do.

The Master.csv file needs to be loaded for this to work.

Try it on a few files first as it takes about 6 minutes.

VBA Code:
Private Sub subSwopData()
Dim strPath As String
Dim strFile As String
Dim intCount As Integer
Dim dteTimeStart As Date

    dteTimeStart = Now()
    
    ActiveWorkbook.Save
    
    strPath = "C:\Dump\Amend column contents within multiple CSV files\"
    ' CHANGE THE LINE ABOVE AS APPROPRIATE.
    
    strFile = Dir(strPath)

    Application.ScreenUpdating = False
    
    Do While strFile <> ""
        If Right(strFile, 3) = "csv" And strFile <> "Master.csv" Then
            Workbooks.Open strPath & strFile
            ActiveWorkbook.Sheets(1).Range("C11:C2029").Value = Workbooks("Master.csv").Sheets(1).Range("C11:C2029").Value
            ActiveWorkbook.Close savechanges:=True
            intCount = intCount + 1
        End If
        strFile = Dir
    Loop
    
    Application.ScreenUpdating = True
        
    MsgBox intCount & " CSV files updated from the master file." & vbCrLf & _
        "Start Time : " & Format(dteTimeStart, "HH:MM:SS") & vbCrLf & "End Time : " & Format(Time(), "HH:MM:SS"), vbOKOnly, "Confirmation"
    
End Sub
 
Upvote 0
Try this but back up all of your files before you do.

The Master.csv file needs to be loaded for this to work.

Try it on a few files first as it takes about 6 minutes.

VBA Code:
Private Sub subSwopData()
Dim strPath As String
Dim strFile As String
Dim intCount As Integer
Dim dteTimeStart As Date

    dteTimeStart = Now()
   
    ActiveWorkbook.Save
   
    strPath = "C:\Dump\Amend column contents within multiple CSV files\"
    ' CHANGE THE LINE ABOVE AS APPROPRIATE.
   
    strFile = Dir(strPath)

    Application.ScreenUpdating = False
   
    Do While strFile <> ""
        If Right(strFile, 3) = "csv" And strFile <> "Master.csv" Then
            Workbooks.Open strPath & strFile
            ActiveWorkbook.Sheets(1).Range("C11:C2029").Value = Workbooks("Master.csv").Sheets(1).Range("C11:C2029").Value
            ActiveWorkbook.Close savechanges:=True
            intCount = intCount + 1
        End If
        strFile = Dir
    Loop
   
    Application.ScreenUpdating = True
       
    MsgBox intCount & " CSV files updated from the master file." & vbCrLf & _
        "Start Time : " & Format(dteTimeStart, "HH:MM:SS") & vbCrLf & "End Time : " & Format(Time(), "HH:MM:SS"), vbOKOnly, "Confirmation"
   
End Sub

Change the path in the above code where indicated.
 
Upvote 0
Solution
Dear Herakles,

That's amazing! Your code does exactly what I required.
Thanks very much indeed, I'm most grateful :)

Cheers
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,189
Members
452,616
Latest member
intern444

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