Open all CSVs in folder and replace column?

jbass350z

New Member
Joined
Oct 19, 2009
Messages
22
I am using Excel 2007 and need to open all the .csv files in a folder, change the contents of every cell in a column, resave the files and close them. There are hundreds of files, so it would be best to use a loop to step through each file and to the above.

I need to change all the cells in column B to “some text”. Each .csv file has a different number of rows.

I am not very good with VBA and have tried to cut and paste a lot of code together to make this work but have not been successful.

Can someone please help ?
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

pboltonchina

Well-known Member
Joined
Apr 24, 2008
Messages
1,101
Untetsed, so try on a copy of your data
Code:
Sub AllFiles()
    Dim folderPath As String
    Dim filename As String
    Dim wb As Workbook
  
    folderPath = "C:\SAP Imports\Sales Orders\"
    
    If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
    
    filename = Dir(folderPath & "*.csv")
    Do While filename <> ""
      Application.ScreenUpdating = False
        Set wb = Workbooks.Open(folderPath & filename)
         
              Dim LR As Long, i As Long
LR = Range("B" & Rows.Count).End(xlUp).Row
For i = LR To 1 Step -1
    Range("B" & i).Value = "some text" 'change to suit
Next i
    
        filename = Dir
    Loop
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

jbass350z

New Member
Joined
Oct 19, 2009
Messages
22
Untetsed, so try on a copy of your data
Code:
Sub AllFiles()
    Dim folderPath As String
    Dim filename As String
    Dim wb As Workbook
  
    folderPath = "C:\SAP Imports\Sales Orders\"
    
    If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
    
    filename = Dir(folderPath & "*.csv")
    Do While filename <> ""
      Application.ScreenUpdating = False
        Set wb = Workbooks.Open(folderPath & filename)
         
              Dim LR As Long, i As Long
LR = Range("B" & Rows.Count).End(xlUp).Row
For i = LR To 1 Step -1
    Range("B" & i).Value = "some text" 'change to suit
Next i
    
        filename = Dir
    Loop
  Application.ScreenUpdating = True
End Sub

Almost perfect. How do I save and close each .CSV file during the loop without being prompted to save it?
 
Upvote 0

Forum statistics

Threads
1,191,610
Messages
5,987,654
Members
440,104
Latest member
thigarette

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
Top