Excelmacrosselect从CSV导入的内容和位置

我有一个.csv文件导出从我的银行帐户,这是目前正在导入Excel通过Excel自动导入,然后与我的macros(删除一些列,格式更改,concat,等等..没有什么特别的)处理。

但是,.csv文件没有一致的格式,并且有一些列会改变它们的位置(例如,列“IBAN”有时是第2列,有时是第5列),或者根本不存在,导致我的macros坠毁。

我需要的是一些代码,它将首先与.csv一起工作,检查列“IBAN”的.csv并在检查后导入它,所以总是让我们说列号。 1和我的macros将处理列号。 1没有问题。

有任何想法吗?

像这样的东西应该工作,而且不复杂。 您可以使用find函数的其他参数以指定您正在search的位置:

 Public Function GetColumnRange(ByVal sSearch As String, r As Object, rSearchArea As Range) If Not rSearchArea.Find(sSearch, , , xlWhole) Is Nothing Then Set r = rSearchArea.Find(sSearch, , , xlWhole) r.Select GetColumnRange = True End If 

End Function Public Sub CSV_Reformat()Dim wb As Workbook Dim ws As Worksheet

 Dim arrArgs() As Variant Dim cColl As Collection Dim rHolder As Object Set cColl = New Collection arrArgs() = Array("IBAN", "Arg2", "Arg3") ' Use the code you have to load the .CSV file and to open it ' Assumes that wb is set to the .CSV file ' Assumes ws is the first sheet in the .CSV file Set wb = ActiveWorkbook ' Replace this with your actual .CSV file Set ws = wb.Sheets(1) For i = LBound(arrArgs()) To UBound(arrArgs()) If GetColumnRange(arrArgs(i), rHolder, ws.UsedRange) = True Then cColl.Add rHolder End If Next For i = 1 To cColl.Count Set rHolder = cColl(i) ' Do whatever you need to do with the range here ' For example, you could get the column number: Debug.Print rHolder.Column Next End Sub 

如果您的CSV文件较大,我也build议考虑使用这个数组。 你可以使用下面的方法加载数组:

 Dim arrData() as Variant Dim i as Long, Dim j as Long Dim lOutput as Long Dim bool as Boolean ' Assumes, as before, that ws is the worksheet we are working in arrData() = ws.UsedRange.Value 

然后你可以创build一个新的数组来输出:

 Dim arrOut() as Variant redim arrOut(0 to Ubound(arrData()) - 1, 0 to i) ' Reduce it by one row since we are creating a zero based array. i is the ' number of columns you want in the output. ' Then loop over the data array and put the right columns into your output For i = 1 to Ubound(arrData(), 2) ' Loop through the headers in your data bool = True Select Case arrData(1, i) Case "IBAN" lOutput = 0 ' Allows you to determine where the data will be put in your array Case "Arg2" lOutput = 1 Case "Arg3" lOutput = 2 Case Else bool = False End Select If bool = True Then For j = 1 to Ubound(arrData(), 1) arrOut(j - 1, lOutput) = arrData(j, i) Next End If Next 

这应该允许您从.CSV文件中select某些数据并将其加载到数组中。 然后,您可以根据需要将数据输出到一个范围。 例如

 With wsOutput Set r = .Range("A1").Resize(Ubound(arrOut(), 1) + 1, Ubound(arrOut(), 2) + 1) r.Value = arrOut() End With 

以下代码使用ADODB在CSV文件上执行SQL查询,从而只按照您希望的顺序导入所需的列。

 Sub SQL_Extract() Dim objConnection As Object Dim objRecordset As Object Dim CSVFilename As String Dim CSVFilepath As String CSVFilename = "myCSVFile.csv" ' Change to name of your CSV file CSVFilepath = "C:\Temp\DownloadFolder\" ' Change to location of CSV file ' Set up connections & dataset objects Set objConnection = CreateObject("ADODB.Connection") Set objRecordset = CreateObject("ADODB.Recordset") objConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _ "Data Source=" & CSVFilepath & ";" & _ "Extended Properties=""text;HDR=YES;FMT=CSVDelimited"";" objConnection.Open 'Create the SQL command to retrieve information 'The following assumes that the CSV file contains columns with headings of ' "IBAN", "Transaction Date" and "Amount". (Any other columns in the file ' will be ignored.) sqlCommand = "SELECT [IBAN], " & _ " [Transaction Date], " & _ " [Amount] " & _ "FROM [" & CSVFilename & "]" 'Execute the query objRecordset.Open sqlCommand, objConnection, 3, 3, 1 If Not objRecordset.EOF Then ' Check whether any records were created by the query ' Write out the results of the query ' Change "A2" to top left cell of area where you want results written Range("A2").CopyFromRecordset objRecordset End If objRecordset.Close objConnection.Close End Sub