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

时间:2011-12-15 14:05:18

标签: excel vba excel-vba 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中运行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&它有一个关于“excel循环文件夹中的文件”的信息,但这不是我正在寻找的。

我想,我需要在这里做些什么,从文件&中删除, "" ;之类的特殊字符。然后格式化。我根本不知道如何处理这个问题。

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

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

3 个答案:

答案 0 :(得分:1)

你有很多选择来实现这个目标。

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

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

如果你想把像macro这样的任务放在一起,你可以通过这个脚本来举例:

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

End Sub

答案 1 :(得分: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以科学记数法格式化)。

答案 2 :(得分: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