VBA Excel:从CSV文件中以特定格式提取数据

我有不同的CSV文件,这些文件有一些原始数据Computer Name,"Computer Serial","User name","Employee Number","Software name"后面跟着下面的数据。

从链接的文件中添加这个:

 Comp;uter;"Name ";Computer;Seria;l"" User";"name"" Employee";"Number"" Software";"name""" DK4408XP0016,108081520001,"GAILLARD Alain",11014,"LiveUpdate 3.3 (Symantec Corporation)";;;;;;;; DK4408XP0016,108081520001,"GAILLARD Alain",11014,"Adobe SVG Viewer 3.0";;;;;;;; DK4408XP0016,108081520001,"GAILLARD Alain",11014,"Adobe Download Manager 2.0 (Supprimer uniquement)";;;;;;;; DK4408XP0016,108081520001,"GAILLARD Alain",11014,"ATI - Utilitaire de désinstallation du logiciel";;;;;;;; DK4408XP0016,108081520001,"GAILLARD Alain",11014,"ATI Display Driver";;;;;;;; DK4408XP0016,108081520001,"GAILLARD Alain",11014,"IBM iSeries Access for Windows";;;;;;;; DK4408XP0016,108081520001,"GAILLARD Alain",11014,DomusDraw;;;;;;;; DK4408XP0016,108081520001,"GAILLARD Alain",11014,"NeXspan SoftPhone i2052 R3.1 D03";;;;;;;; DK4408XP0016,108081520001,"GAILLARD Alain",11014,"Désinstallation du logiciel d''imprimante IBM";;;;;;;; DK4408XP0016,108081520001,"GAILLARD Alain",11014,"Désinstallation du logiciel IBM";;;;;;;; DK4408XP0016,108081520001,"GAILLARD Alain",11014,"CA 01 - le Offline MALL de Siemens Automation and Drives";;;;;;;; DK4408XP0016,108081520001,"GAILLARD Alain",11014,"Java Web Start";;;;;;;; DK4408XP0016,108081520001,"GAILLARD Alain",11014,"Correctif Windows XP - KB873339";;;;;;;; DK4408XP0016,108081520001,"GAILLARD Alain",11014,"Correctif Windows XP - KB885250";;;;;;;; DK4408XP0016,108081520001,"GAILLARD Alain",11014,"Correctif Windows XP - KB885835";;;;;;;; DK4408XP0016,108081520001,"GAILLARD Alain",11014,"Correctif Windows XP - KB885836";;;;;;;; DK4408XP0016,108081520001,"GAILLARD Alain",11014,"Correctif Windows XP - KB886185";;;;;;;; 

我从来没有使用过Excel VBA,这是我第一次在这方面工作。 我开始研究一些例子来在Excel中创build和运行VBA代码。

有人请帮我解决这个问题,我想创buildVBA代码来提取原始数据,并把它放到下面的格式中。

 CompName ComputerSerial UserName EmpNo SoftwareName DK4408XP0016 1108081520001 GAILLARD Alain 11014 LiveUpdate 3.3 (Symantec Corporation) DK4408XP0016 1108081520001 GAILLARD Alain 11014 Adobe SVG Viewer 3.0 

我检查了这个链接代码循环通过指定的文件夹中的所有Excel文件,并从特定的单元格中提取数据,它有一个关于“通过文件夹中的文件的Excel循环”的信息,但这不是我正在寻找。

我想,我需要在这里做的,删除特殊字符,如, "" ; 从文件和格式。 我根本不知道如何继续。

是否有任何工具从.CSV文件中提取数据? 我需要一些build议,想法或一些很好的例子来解决我的问题,这对我来说真的很有帮助。

我在这里分享我的一个文件: http : //uploadmb.com/freeuploadservice.php?uploadmbID=1323960039&srv=www&filename=4408_NANTES_softwares.csv

这适用于您的示例文件:

 ' Open the csv file as a text file Workbooks.OpenText Filename:="C:\4408_NANTES softwares.csv" 

Excel有时会自动分析CSV文件,但有时不会; 我无法弄清楚这种模式。 因此,您可以添加以下内容以确保正确parsing:

 ' Parse it using comma and semicolon as delimiters Range(Range("A1"), Range("A1").End(xlDown)).TextToColumns _ DataType:=xlDelimited, _ TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _ Semicolon:=True, Comma:=True, Space:=False, Other:=False, _ FieldInfo:= _ Array(Array(1, 2), Array(2, 2), Array(3, 2), Array(4, 1), Array(5, 2)) 

FieldInfo位可能看起来有点神秘,但它唯一的作用是指定您的字段被视为文本(主要是为了避免您的序列号108081520001被科学记数法形成)。

你有很多的select来实现这一点。

如果您的操作是例外(仅适用于今天),则可以使用导入和转换Excel的CSV文件的function。

打开你的Excel,在工具栏,只需点击数据/转换。

如果你想放置一个像macros一样的任务,你可以通过这个脚本举例:

 Public Sub IsValid() Dim i As Long Dim valueTemp As String 'Chaine de caractere Dim wsTemp As Worksheet 'Feuille Dim rTemp As Range 'Range 'Variable initialise a 1 i = 1 Set wsTemp = ThisWorkbook.Worksheets(NameFileResult) While (Ws_Result.Cells(i, 1) <> "") valueTemp = Ws_Result.Cells(i, 1) With ThisWorkbook 'ton fichier dans lequel tu fais ta recherche Set rTemp = wsTemp.Range("A:D").Find(valueTemp, , xlValues, xlWhole, , , False) If Not rTemp Is Nothing Then wsTemp.Rows(rTemp.Row).Interior.ColorIndex = 4 'Vert si la donnees est disponible wsTemp.Rows(rTemp.Row).Copy (Ws_Index.Rows(15)) wsTemp.Rows(1).Copy (Ws_Index.Rows(14)) Else Ws_Index.Rows(15).Clear Ws_Index.Rows(14).Clear Ws_Index.Cells(15, 5).Value = NameMsgBoxNoFind Ws_Index.Rows(15).Interior.ColorIndex = 3 End If End With i = i + 1 Wend 

结束小组

我知道这是一个老话题,但我也必须这样做,我想我的解决scheme可能会帮助未来的用户。 如果有一个零长度的元素(例如elem1,elem2,elem4),那么数组值将=“”。 它为我工作得很好。

 Function ParseLineEntry(LineEntry As String) As Variant 'This function takes a .CSV line entry as argument and parses it into an array of each element. Dim NumFields As Integer, LastFieldStart As Integer Dim LineFieldArray() As Long Dim i As Long, j As Long 'Determine how many delimitations there are. There will always be at least one field NumFields = 1 For I = 1 To Len(LineEntry) If Mid(LineEntry, i, 1) = "," Then NumFields = NumFields + 1 Next I ReDim LineFieldArray(1 To NumFields) 'Parse out each element from the string and assign it into the appropriate array value LastFieldStart = 1 For i = 1 To NumFields For j = LastFieldStart To Len(LineEntry) If Mid(LineEntry, j, 1) = "," Then LineFieldArray(i) = Mid(LineEntry, LastFieldStart, j - LastFieldStart) LastFieldStart = j + 1 Exit For End If Next j Next i ParseLineEntry = LineFieldArray End Function