Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 将多个csv文件编译成一个新的csv工作表_Vba_Excel_Csv - Fatal编程技术网

Vba 将多个csv文件编译成一个新的csv工作表

Vba 将多个csv文件编译成一个新的csv工作表,vba,excel,csv,Vba,Excel,Csv,我正在将几个CSV文件的内容复制到一个新的CSV文件中,我的vba代码有问题。我知道CMD工具可以复制.csv文件,但这对我不起作用,因为我的目录存储在网络上,我无法从CMD窗口找到它的路径(我收到一个关于使用UNC地址的错误)。我的老板希望代码不需要人工交互,所以将文件移动到计算机上的一个目录中,运行CMD,然后将结果移回不是一个选项 根据我老板的要求,代码需要执行以下操作: 每次运行宏时,新的主文件都应在运行时保存,以便报表每次提取相同的文件 这样做的一个逻辑结果是,宏应该捕获结果文件名中的

我正在将几个CSV文件的内容复制到一个新的CSV文件中,我的vba代码有问题。我知道CMD工具可以复制.csv文件,但这对我不起作用,因为我的目录存储在网络上,我无法从CMD窗口找到它的路径(我收到一个关于使用UNC地址的错误)。我的老板希望代码不需要人工交互,所以将文件移动到计算机上的一个目录中,运行CMD,然后将结果移回不是一个选项

根据我老板的要求,代码需要执行以下操作:

每次运行宏时,新的主文件都应在运行时保存,以便报表每次提取相同的文件

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

下面是到目前为止我编写的代码。当我尝试运行宏时,会出现一些错误,从而得出以下行:

Set WorkBk=Workbooks.Open(文件夹路径和文件名)

他们总是1004条消息,他们要么说我创建的文件是只读/加密的,要么告诉我对象“工作簿”的方法“打开”失败

要使下面的代码正常工作,我需要更改或执行什么操作?我对这段代码很有信心,因为我从昨天编写的代码中稍微修改了它,以便对.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()
'
'
'
'
将分类作为工作表
将汇总表作为工作表进行调整
将FolderPath设置为字符串
暗淡无光
将文件名设置为字符串
将WorkBk设置为工作簿
将源范围变暗为范围
变暗减小范围为范围
'将宏指向正确的数据源(将此行更新到您的数据源!!!)
FolderPath=“\\usilsvr01”\lin@mktg\Analytical Services\DIA\提供了要排除的数据问题“
'创建一个空白工作簿以承载聚合,并正确命名第一个工作表。
设置CSVAggregation=Workbooks.Add(xlWBATWorksheet)。工作表(1)
表(1).Name=“直径聚合”
'使用要聚合的相关字段标题工作表。
CSVAggregation.Range(“A1:C1”)=数组(“制造商编号”、“报价代码”、“数据问题”)
'递增,以跟踪新行应追加到的位置。
NRow=2
最后一排一样长
'第一次调用Dir,将其指向文件夹路径中的所有Excel文件。
FileName=Dir(FolderPath&“*.csv”)
'循环,直到已读取源文件夹中的所有.csv文件。
文件名“”时执行此操作
'宏应跳过聚合文件的早期版本
如果InStr(1,文件名,“聚合”)>0,则
FileName=Dir()
如果结束
'打开文件夹中的工作簿。
设置WorkBk=Workbooks.Open(文件夹路径和文件名)
'循环浏览数据表以收集数据。
工作表(1)。激活“激活工作表,查找数据所在位置并选择数据”。
LastRow=WorkBk.Worksheets(1).Cells.Find(What:=“*”_
之后:=工作表(1).单元格范围(“A1”)_
搜索方向:=xlPrevious_
LookIn:=xl公式_
SearchOrder:=xlByRows).行
设置SourceRange=WorkBk.Worksheets(1).Range(“A2:C”和LastRow)
'将目标范围设置为从A列开始,然后
'与源范围的大小相同。
Set DestRange=DIAAggregation.Range(“A”&NRow)
Set DestRange=DestRange.Resize(SourceRange.Rows.Count、SourceRange.Columns.Count)
'将值从源复制到目标。
DestRange.Value=SourceRange.Value
'增加NRow,使数据不被覆盖。
NRow=NRow+DestRange.Rows.Count
'关闭源工作簿而不保存更改。
WorkBk.Close savechanges:=False
'使用Dir获取下一个文件名。
FileName=Dir()
环
'在目标工作表上调用AutoFit,以便所有数据都可读。
csvagggregation.Columns.AutoFit
csvagggregation.Rows.AutoFit
'将光标放在第一个文件上,这样文件就不会突出显示或打开顶部以外的任何位置。
csvagggregation.Range(“A1”)。选择
'创建变量以保存聚合报告的另存为名称。
将工作簿名称设置为字符串
工作簿\u Name=“CSV聚合”
'将工作簿保存在数据所在的文件夹中(确保选中您应该使用的文件夹/文件!!!!)
ActiveWorkbook.SaveAs文件名:=(文件夹路径和工作簿名称),文件格式:=6
端接头

好的,我可以做一些更改以使代码正常工作

以下是最终代码:

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
Sub-CSV_-Aggregate()
'
'
'
'
将分类作为工作表
将汇总表作为工作表进行调整
将FolderPath设置为字符串
暗淡无光
将文件名设置为字符串
将WorkBk设置为工作簿
将源范围变暗为范围
变暗减小范围为范围
'将宏指向正确的数据源(将此行更新到您的数据源!!!)
FolderPath=“\\usilsvr01”\lin@mktg\Analytical Services\DIA\提供了要排除的数据问题\“
'创建一个空白工作簿以承载聚合,并正确命名第一个工作表。
设置CSVAggregation=Workbooks.Add(xlWBATWorksheet)。工作表(1)
表(1).Name=“直径聚合”
'使用要聚合的相关字段标题工作表。
CSVAggregation.Range(“A1:C1”)=数组(“制造商编号”、“报价代码”、“数据问题”)
'递增,以跟踪新行应追加到的位置。
NRow=2
最后一排一样长
'第一次调用Dir,将其指向文件夹路径中的所有Excel文件。
文件名