将CSV文件与Excel VBA结合使用

我有一个文件夹中的一些CSV文件。 它们都包含3个特定的列。 总列数和顺序可能会有所不同。

我想连接所有3列与下划线,并将其写入运行代码的工作表中的单个列中。

这是我到目前为止:

Option Explicit Sub test() Dim i As Long Dim LastRow As Long Dim Columns() Columns = Array("Column1", "Column2", "Column3") 'Find Columns by Name For i = 0 To 2 Columns(i) = Rows(1).Find(What:=Columns(i), LookIn:=xlValues, LookAt:=xlWhole, _ MatchCase:=False, SearchFormat:=False).Column Next i 'Debug.Print Columns(0) 'Debug.Print Columns(1) 'Debug.Print Columns(2) LastRow = Cells(Rows.Count, "A").End(xlUp).Row For i = 2 To LastRow Cells(i, 1) = Cells(i, Columns(0)) & "_" & Cells(i, Columns(1)) & "_" & Cells(i, Columns(2)) Next i End Sub 

正如你所看到的,这是做我想要的,但只为活动工作表。 我实际上想循环遍历与活动工作表相同的文件夹中的所有csv文件,并将结果写入运行代码的工作表的第一个工作表的第一列(显然不是csv本身)。 我怎样才能做到这一点?

谢谢!

这是一个将通过文件夹循环的代码

 Sub Button1_Click() Dim MyFile As String, Str As String, MyDir As String, Wb As Workbook Set Wb = ThisWorkbook 'change the address to suite MyDir = "C:\WorkBookLoop\" MyFile = Dir(MyDir & "*.xls") 'change file extension ChDir MyDir Application.ScreenUpdating = 0 Application.DisplayAlerts = 0 Do While MyFile <> "" Workbooks.Open (MyFile) 'do something here MyFile = Dir() Loop End Sub 

这取决于您如何命名您从CSV文件创build的工作表。 您可以将所有工作表添加到集合中,并使用For...Each循环来执行该循环内的整个search和连接过程。 请注意,您必须明确定义第一个工作表名称,因为这不会通过连续的循环更改:

 Option Explicit Sub test() Dim i As Long Dim LastRow As Long Dim Columns() Dim frontSheet as Worksheet Dim wSheets as New Collection Dim ws as Worksheet Set frontSheet = Sheets("name of front sheet") 'Add all your CSV sheets to wSheets using the .Add() method. For Each ws in wSheets Columns = Array("Column1", "Column2", "Column3") 'Find Columns by Name For i = 0 To 2 Columns(i) = ws.Rows(1).Find(What:=Columns(i), LookIn:=xlValues, LookAt:=xlWhole, _ MatchCase:=False, SearchFormat:=False).Column Next i 'Debug.Print Columns(0) 'Debug.Print Columns(1) 'Debug.Print Columns(2) LastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row For i = 2 To LastRow frontsheet.Cells(i, 1) = ws.Cells(i, Columns(0)) & "_" & ws.Cells(i, Columns(1)) & "_" & ws.Cells(i, Columns(2)) Next i Next ws End Sub 

在excel中打开CSV文件通常很慢并且很麻烦,但是VBA可以使用TextStream它们作为文本文件读取。 此外,文件脚本对象可以让你直接处理文件和目录。 像这样的东西可能是一个更好的方法,如果你不需要保存在工作表中的文件后来:

 Sub SearchFoldersForCSV() Dim fso As Object Dim fld As Object Dim file As Object Dim ts As Object Dim strPath As String Dim lineNumber As Integer Dim lineArray() As String Dim cols() As Integer Dim i As Integer Dim frontSheet As Worksheet Dim frontSheetRow As Integer Dim concatString As String Set frontSheet = Sheets("name of front sheet") frontSheetRow = 1 strPath = "C:\where-im-searching\" Set fso = CreateObject("Scripting.FileSystemObject") Set fld = fso.GetFolder(strPath) For Each file In fld.Files If (Right(file.Name, 3) = "csv") Then Debug.Print file.Name Set ts = file.OpenAsTextStream() lineNumber = 0 Do While Not ts.AtEndOfStream lineNumber = lineNumber + 1 lineArray = Split(ts.ReadLine, ",") If (lineNumber = 1) Then 'We are at the first line of the .CSV so 'find index in lineArray of columns of interest 'Add extra ElseIf as required For i = LBound(lineArray) To UBound(lineArray) If lineArray(i) = "Column 1" Then cols(1) = i ElseIf lineArray(i) = "Column 2" Then cols(2) = i ElseIf lineArray(i) = "Column 3" Then cols(3) = i End If Next i Else 'Read and store the column of interest from this 'row by reading the lineArray indices found above. concatString = "" For i = LBound(cols) To UBound(cols) concatString = concatString & lineArray(i) & "_" Next i concatString = Left(concatString, Len(concatString) - 1) frontSheet.Cells(frontSheetRow, 1).Value = concatString frontSheetRow = frontSheetRow + 1 End If Loop ts.Close End If Next file End Sub 

你可以在这里find关于FileSystemObjectTextStream更多信息。