将许多csv文件编译成一个新的csv表单

我正在将几个CSV文件的内容复制到一个新的CSV文件中,而且我的vba代码有问题。 我知道的CMD工具来复制.csv文件,但这不适合我,因为我的目录存储在networking上,我不能从CMD窗口path(我得到一个有关使用UNC错误地址)。 我的老板更喜欢代码没有人为交互,所以将文件移动到计算机上的一个目录,运行CMD,然后移回结果不是一个选项。

根据我老板的要求,代码需要做以下工作:

“每次运行macros时,新的主文件应该在运行时保存下来,以便报告每次都提取相同的文件。”

这样做的一个合乎逻辑的结果是,macros应该在生成的文件名中捕获一个特定的string,并在创build新版本时“跳过”该文件。 另外,每个.csv文件都有标题,因此我的范围设置为避免复制它们。

以下是我迄今为止所写的代码。 当我尝试运行macros时,出现了一些错误提示:

Set WorkBk = Workbooks.Open(FolderPath & FileName)

他们总是1004消息,他们要么说我创build的文件是只读/encryption,或者他们告诉我, 对象'工作簿'的方法'打开'失败

我需要改变或做些什么才能使下面的代码工作? 我对这段代码很有信心,因为我从昨天写的代码稍微修改了一下,用.xlsx文件做类似的任务。 任何帮助非常感谢,谢谢。

  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 

好的,我可以做一些改变,让我的代码工作。

这是最后的代码:

 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 

我在FilePath声明中添加了最后一个“\”。

我还重写了设置的WorkBk行,如下所示:

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

这解决了我所得到的“只读”错误。

您可以使用pushd命令绕过cmd的UNC / network文件夹问题。 它将一个临时驱动器号分配给networking文件夹,并允许您正常继续。