2011-12-15 115 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 
相關問題