2011-12-15 163 views
0

我有不同的CSV文件,这些文件有一些原始数据 Computer Name,"Computer Serial","User name","Employee Number","Software name"后面是以下数据。VBA Excel:从CSV文件中以特定格式提取数据

加入这个从链接文件:

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中创建运行VBA代码&。

有人请帮我解决这个问题,我想创建VBA代码来提取原始数据&放入下面的格式。

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 

我检查这个环节Code for looping through all excel files in a specified folder, and pulling data from specific cells &有关于“通过文件夹的文件擅长循环”的信息,但是这不是我要找的。

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

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

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

+0

您示例文件的格式看起来并不像您在这里发布的内容:“Comp; uter;”Name,“”“; Computer; Seria;”l“”,“User”;“name “”,“”雇员“,”号码“,”“软件”,“名称”“” “DK4408XP0016,108081520001”,“GAILLARD Alain”“,11014,”“LiveUpdate 3.3(赛门铁克公司) ;;;;;;`那是哪一个呢? – 2011-12-15 16:49:03

+0

@ Jean-FrançoisCorbett:你说得对。我有很多这样的文件。现在我已将示例添加到问题中。 – linguini 2011-12-15 17:33:47

回答

1

这适用于您的示例文件:

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

Excel中有时会自动分析CSV文件,但有时不会;我无法弄清楚这种模式。所以,你可以添加以下,以确保它得到正确分析:

' 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)。

1

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

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

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

如果你想要把像宏任务,你可以通过这个脚本采取例如:

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 

末次

+0

感谢您的支持。我可以在Excel中打开所有文件,但没有任何问题,但数据未分类,所以我想运行一个宏。当我尝试使用数据/转换时,什么都没有发生。我在这里共享一个文件[URL = http://uploadmb.com/freeuploadservice.php?uploadmbID = 1323959598&SRV = www&文件名= 4408_NANTES_softwares.csv] 4408_NANTES_softwares.csv [/ URL] – linguini 2011-12-15 14:36:08

0

我知道这是一个古老的线程,但我也必须这样做,我认为我的解决方案可能会帮助未来的用户。如果存在零长度元素(例如,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 
相关问题