Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 90161

Add column with file name to multiple Excel files

$
0
0

I have a folder that has 370 CSV files in it, and each of those files are logs from specific days. I eventually want to combine all of these into 1 CSV file, but before I can do that I need a way to differentiate between each day's data within the combined file since that is not already one of the columns. The file names are already the date of the log, so I am looking for a way to easily add the file name down all of the rows in each of the files in the next available column, which is "AA". I have run the following as a test to see if it adds the "Test" to all of the columns, which it does, but I do not know what to call in order to get it to write the filename in the column:

    Sub LoopThroughFolder()

    Dim MyFile As String, Str As String, MyDir As String, Wb As Workbook
    Dim Rws As Long, Rng As Range
    Set Wb = ThisWorkbook
    MyDir = "C:\Users\UserDir\Desktop\folder1\"'Directory
    MyFile = Dir(MyDir & "*.csv")    'File extension
    ChDir MyDir
    Application.ScreenUpdating = 0
    Application.DisplayAlerts = 0

    Do While MyFile <> ""
        Workbooks.Open (MyFile)
            Range("AA1:AA600").Value = "Test"'New Column Name
            ActiveWorkbook.Save
            ActiveWorkbook.Close True
        MyFile = Dir()
    Loop

    End Sub

Thank you for your help!


Viewing all articles
Browse latest Browse all 90161

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>