Modifying CVS File

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have the following code to open up a csv file


I am trying to autofit all the columns and then save and close the file

However when opening up the file, the columns are not autofitted to the length of the largest item in each column

Kindly amend my code below so that the columns autofit


Code:
 Sub OpenAndModifyCSVFiles()
    Application.ScreenUpdating = False
    Dim strFile As String
    Dim strFileType As String
    Dim strPath As String
    Dim lngLoop As Long
     Dim LR As Long, i As Long


    strPath = "C:\Parts stock"
    strFileType = "*.csv" 'Split with semi-colon if you want to specify the file types. Example ->> "*.xls;*.jpeg;*.doc;*.gif"
    


     For lngLoop = LBound(Split(strFileType, ";")) To UBound(Split(strFileType, ";"))
        strFile = Dir(strPath & "\" & Split(strFileType, ";")(lngLoop))
        Do While strFile <> ""
            With Workbooks.Open(strPath & "\" & strFile)
               Range("A:R").EntireColumn.AutoFit
                  
                End With
                ActiveWorkbook.Save
                ActiveWorkbook.Close
            strFile = Dir
        Loop
    Next lngLoop
    strFile = vbNullString
    strFileType = vbNullString
    strPath = vbNullString
    lngLoop = Empty
     
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
CSV files don't have column widths, they simply have lists of values separated by commas.

If you want to have column widths you have to save it as an Excel Spreadsheet.
From thread #2 : https://www.petri.com/forums/forum/...ing/13988-autofit-colums-in-csv-file-and-save

A CSV file saves tabular data (numbers and text) in plain text format. The column width is a property of a cell object, which doesn't exist in a CSV file. Either do without or save your file as a standard Excel xlsx file type.
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,392
Members
449,081
Latest member
JAMES KECULAH

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