Results 1 to 4 of 4

Macro to Autofit Columns of CSV Files in a folder

Threaded View

  1. #1
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,765

    Macro to Autofit Columns of CSV Files in a folder

    I have tried to write code to autofit Cols A to D of all csv files in folder C:\Journals, but cannot get this to work


     Sub AutofitColumnsCSVFiles()
        Dim MyFolder As String
        Dim MyFile As String
        Dim wb As Workbook
        Dim ws As Worksheet
        
        ' Define the folder path
        MyFolder = "C:\Journals\"
        
        ' Disable screen updating to speed up the process
        Application.ScreenUpdating = False
        
        ' Check if the folder exists
        If Dir(MyFolder, vbDirectory) = "" Then
            MsgBox "Folder not found!", vbExclamation
            Exit Sub
        End If
        
        ' Loop through CSV files in the folder
        MyFile = Dir(MyFolder & "*.csv")
        Do While MyFile <> ""
            ' Open the CSV file
            Set wb = Workbooks.Open(MyFolder & MyFile)
            ' Loop through all worksheets in the workbook
            For Each ws In wb.Worksheets
                ' Set the width of columns A to D to autofit
                ws.Columns("A:D").AutoFit
            Next ws
            ' Save and close the workbook
            wb.Close SaveChanges:=True
            ' Get the next CSV file
            MyFile = Dir
        Loop
        
        ' Enable screen updating
        Application.ScreenUpdating = True
        
        MsgBox "Columns A to D autofitted for all CSV files in the folder.", vbInformation
    End Sub
    It would be appreciated if someone could kindly amend my code
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Copy specific columns from multiple files in folder and combine those columns in workbook
    By JaffarAhamed in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 09-13-2023, 04:11 AM
  2. [SOLVED] List Files in a Folder Macro detects an Invisible File not existing in the Folder Issue
    By e4excel in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 01-06-2022, 03:49 AM
  3. [SOLVED] Macro to copy .xlsm files in folder and sub-folder to foldr C:\old pull files
    By Howardc1001 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-15-2021, 09:55 PM
  4. [SOLVED] Macro to list and open .xlsm files in folder and sub-folder
    By Howardc1001 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-24-2019, 07:35 PM
  5. AutoFit columns with macro
    By deano3141 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-14-2014, 08:58 AM
  6. Macro to create a folder and then save all files in that folder
    By gokzee in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-27-2013, 01:49 PM
  7. Replies: 1
    Last Post: 03-12-2013, 04:45 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1