K Pekosh
K Pekosh

Reputation: 693

Compile many csv files into a single, new csv sheet

I am working on copying the contents of several CSV files into a single, new CSV file, and am having trouble with my vba code. I am aware of the CMD tool to copy .csv files, but this does not work for me, as my directory is stored on the network and I can't path to it from the CMD window (I get an error about using a UNC address). My boss would prefer for the code to have zero human interaction, so moving the files to a directory on the computer, running the CMD, and then moving the results back is not an option.

Per my boss's request, the code needs to do the following:

"Every time the macro is run, the new master file should be saved over when it's run so the report pulls the same file each time."

A logical consequence of this is that the macro should catch a particular string in the resulting file name and "skip over" that file when making a new version. Also, every .csv file has headings, so my ranges are set up to avoid copying them.

Below is the code I have written thus far. When I try to run the macro, I get a few errors to come up with the line:

Set WorkBk = Workbooks.Open(FolderPath & FileName)

They're always 1004 messages, and they either say my created file is either read-only/encrypted, or they tell me that Method 'Open' of object 'Workbooks' failed.

What do I need to change or do to get the below code to work? I am confident in this code because I slightly modified it from code I wrote yesterday to do a similar task with .xlsx files. Any help is greatly appreciated, thank you.

 Sub CSV_Aggregate()
'
'

'
'

Dim CSVAggregation As Worksheet
Dim SummarySheet As Worksheet
Dim FolderPath As String
Dim NRow As Long
Dim FileName As String
Dim WorkBk As Workbook
Dim SourceRange As Range
Dim DestRange As Range


' Points the macro to the proper data source (UPDATE THIS LINE TO YOUR DATA SOURCE!!!)

FolderPath = "\\usilsvr01\lin@mktg\Analytical Services\DIA\Offers Data Question to Exclude"

' Creates a blank workbook to host the aggregation, and names the first worksheet appropriately.

Set CSVAggregation = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
Sheets(1).Name = "DIA Aggregation"

' Heads the worksheet with the relevant fields to be aggregated.

CSVAggregation.Range("A1:C1") = Array("Manufacturer Number", "Offer Code", "Data Question")

' Incrementer to keep track of where new rows should be appended.

NRow = 2
Dim LastRow As Long

    ' Call Dir the first time, pointing it to all Excel files in the folder path.
    FileName = Dir(FolderPath & "*.csv")


    ' Loop until all .csv files in the source folder have been read.

    Do While FileName <> ""

        ' Macro should skip over the previous version of the aggregate file

        If InStr(1, FileName, "Aggregate") > 0 Then
            FileName = Dir()
            End If

        ' Open a workbook in the folder.

        Set WorkBk = Workbooks.Open(FolderPath & FileName)


            ' Loop through data sheets to collect data.


                Sheets(1).Activate ' Make the sheet active, find where the data is, and select the data.
                    LastRow = WorkBk.Worksheets(1).Cells.Find(What:="*", _
                    After:=WorkBk.Worksheets(1).Cells.Range("A1"), _
                    SearchDirection:=xlPrevious, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows).Row
                Set SourceRange = WorkBk.Worksheets(1).Range("A2:C" & LastRow)


                ' Set the destination range to start at column A and
                ' be the same size as the source range.

                Set DestRange = DIAAggregation.Range("A" & NRow)
                Set DestRange = DestRange.Resize(SourceRange.Rows.Count, SourceRange.Columns.Count)

                ' Copy over the values from the source to the destination.

                DestRange.Value = SourceRange.Value

                ' Increment NRow so that data is not overwritten.

                NRow = NRow + DestRange.Rows.Count


        ' Close the source workbook without saving changes.

        WorkBk.Close savechanges:=False

        ' Use Dir to get the next file name.

        FileName = Dir()
    Loop


    ' Call AutoFit on the destination sheet so that all data is readable.

    CSVAggregation.Columns.AutoFit
    CSVAggregation.Rows.AutoFit

    ' Places cursor on the first sell so document doesn't open highlighted or anywhere besides the top.

    CSVAggregation.Range("A1").Select

    ' Creates variable to hold SaveAs name for Aggregation Report.

    Dim workbook_Name As String

        workbook_Name = "CSV Aggregate"


        ' Saves the workbook in the folder that the data is found in (BE SURE TO CHECK TAHT YOU HAVE THE FOLDER/FILES WITH WHICH YOU SHOULD BE WORKING!!!!)

        ActiveWorkbook.SaveAs FileName:=(FolderPath & workbook_Name), FileFormat:=6


End Sub

Upvotes: 2

Views: 663

Answers (2)

user4691433
user4691433

Reputation:

You can use the pushd command to get around the UNC/network folder issue in cmd. It assigns a temporary drive letter to the network folder and allows you to continue as normal.

Upvotes: 1

K Pekosh
K Pekosh

Reputation: 693

Okay, I was able to make a few changes to get my code to work.

Here is the final code:

Sub CSV_Aggregate()
'
'

'
'

Dim CSVAggregation As Worksheet
Dim SummarySheet As Worksheet
Dim FolderPath As String
Dim NRow As Long
Dim FileName As String
Dim WorkBk As Workbook
Dim SourceRange As Range
Dim DestRange As Range


' Points the macro to the proper data source (UPDATE THIS LINE TO YOUR DATA SOURCE!!!)

FolderPath = "\\usilsvr01\lin@mktg\Analytical Services\DIA\Offers Data Question to Exclude\"

' Creates a blank workbook to host the aggregation, and names the first worksheet appropriately.

Set CSVAggregation = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
Sheets(1).Name = "DIA Aggregation"

' Heads the worksheet with the relevant fields to be aggregated.

CSVAggregation.Range("A1:C1") = Array("Manufacturer Number", "Offer Code", "Data Question")

' Incrementer to keep track of where new rows should be appended.

NRow = 2
Dim LastRow As Long

    ' Call Dir the first time, pointing it to all Excel files in the folder path.
    FileName = Dir(FolderPath & "*.csv")


    ' Loop until all .csv files in the source folder have been read.

    Do While FileName <> ""

        ' Macro should skip over the previous version of the aggregate file

        If InStr(1, FileName, "Aggregate") > 0 Then
            FileName = Dir()
            End If

        ' Open a workbook in the folder.

        Set WorkBk = Workbooks.Open(FolderPath & FileName, , True)


            ' Loop through data sheets to collect data.


                Sheets(1).Activate ' Make the sheet active, find where the data is, and select the data.
                    LastRow = WorkBk.Worksheets(1).Cells.Find(What:="*", _
                    After:=WorkBk.Worksheets(1).Cells.Range("A1"), _
                    SearchDirection:=xlPrevious, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows).Row
                Set SourceRange = WorkBk.Worksheets(1).Range("A2:C" & LastRow)


                ' Set the destination range to start at column A and
                ' be the same size as the source range.

                Set DestRange = CSVAggregation.Range("A" & NRow)
                Set DestRange = DestRange.Resize(SourceRange.Rows.Count, SourceRange.Columns.Count)

                ' Copy over the values from the source to the destination.

                DestRange.Value = SourceRange.Value

                ' Increment NRow so that data is not overwritten.

                NRow = NRow + DestRange.Rows.Count


        ' Close the source workbook without saving changes.

        WorkBk.Close savechanges:=False

        ' Use Dir to get the next file name.

        FileName = Dir()
    Loop


    ' Call AutoFit on the destination sheet so that all data is readable.

    CSVAggregation.Columns.AutoFit
    CSVAggregation.Rows.AutoFit

    ' Places cursor on the first sell so document doesn't open highlighted or anywhere besides the top.

    CSVAggregation.Range("A1").Select

    ' Creates variable to hold SaveAs name for Aggregation Report.

    Dim workbook_Name As String

        workbook_Name = "CSV Aggregate"


        ' Saves the workbook in the folder that the data is found in (BE SURE TO CHECK TAHT YOU HAVE THE FOLDER/FILES WITH WHICH YOU SHOULD BE WORKING!!!!)

        ActiveWorkbook.SaveAs FileName:=(FolderPath & workbook_Name), FileFormat:=6


End Sub

I added a final "\" at the FilePath declaration.

I also rewrote the set WorkBk line as:

Set WorkBk = Workbooks.Open(FolderPath & FileName, , True)

This resolved the "read only" error I was getting.

Upvotes: 1

Related Questions