VBA for Inserting Columns

haa

New Member
Joined
Feb 28, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi all, I'm trying to do the following and need a VBA code than can cut down the tediousness and time it takes to complete this manually and 1 by 1

I have a folder with 40 csv files. For each file, I want to
1) Replace column A title with 'OO Route'
2) Fill the contents of that column with the name of the file

How can I do that at once for all 40 csv files?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
This macro should be the safest, but not the fastest, method. Change the csvFolder path in the code and test it on a subset of the .csv files in a 'test' folder.

VBA Code:
Public Sub Modify_CSV_Files()

    Dim FSO As Object
    Dim FStext As Object
    Dim FSfile As Object
    Dim csvFolder As String
    Dim csvLines As Variant, p As Long, i As Long
        
    csvFolder = "C:\path\to\csv folder\"  '<---- CHANGE THIS
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    For Each FSfile In FSO.GetFolder(csvFolder).Files
        If LCase(FSfile.Name) Like "*.csv" Then
            Set FStext = FSO.OpenTextFile(FSfile.Path)
            csvLines = Split(FStext.ReadAll, vbCrLf)
            FStext.Close
            p = InStr(csvLines(0), ",")
            csvLines(0) = "OO Route" & Mid(csvLines(0), p)
            For i = 1 To UBound(csvLines) - 1
                p = InStr(csvLines(i), ",")
                csvLines(i) = Left(FSfile.Name, InStrRev(FSfile.Name, ".") - 1) & Mid(csvLines(i), p)
            Next
            Set FStext = FSO.CreateTextFile(FSfile.Path)
            FStext.Write Join(csvLines, vbCrLf)
            FStext.Close
        End If
    Next
    
End Sub

A much faster method would be to open each .csv file using Workbooks.Open and change the values in column A (no looping required). However if the .csv files contain date strings, or strings that Excel interprets as dates, it may change the format of those strings in the output .csv.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,953
Members
449,095
Latest member
nmaske

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