2016-11-10 5 views
0

ここで何か問題があります。条件を使用してVBAの出力レコードセット

FIELD(1から5まで)、DATEHOUR(yyyyddmmhhmmssxxx、年、日、月、時、分、秒、およびミリ秒の形式)の4つの列を持つ2500万行のcsvデータベースを持っています。または北)とCATEGORY(1から10まで)。

私は次のコードを使用してい

Public Sub QueryTextFile() 
    Dim Recordset As ADODB.Recordset 
    Dim ConnectionString As String 
    ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ThisWorkbook.Path & ";" & _ 
     "Extended Properties=Text;" 

    Const SQL As String = "SELECT * FROM Dados.csv WHERE Categoria=3;" 

    Set Recordset = New ADODB.Recordset 
    Call Recordset.Open(SQL, ConnectionString, CursorTypeEnum.adOpenForwardOnly, LockTypeEnum.adLockReadOnly, CommandTypeEnum.adCmdText) 
    Call Sheet1.Range("A1").CopyFromRecordset(Recordset) 
    Recordset.Close 
    Set Recordset = Nothing 

End Sub 

問題は、私はそれをどのように行うことができ、私は、例えば、ONLY取得したいことは2月(月02)のために登録することで、フィールド3?

ありがとう!

+0

'CopyFromRecordset'は最初の65536レコードのみを返します。 'ADODB.Recordset.GetRows'は、完全なRecordsetを配列にロードします。この配列を使用してワークシートを作成できます。 CSVファイルのダウンロードリンクを提供できる場合、私はコードを助けます。 –

答えて

0
SELECT * FROM Dados.csv 
WHERE 
Categoria = 3 AND 
FIELD = 3  AND 
DATEHOUR Like '######02*' 
0

このサイズのファイルで、単純なフィルタ操作が必要な場合は、行単位で読み込んで一致する行を保存することができます。次に例を示します。

入力ファイル

FIELD,DATEHOUR,BOUND,CATEGORY 
1,20030609100744914,south,2 
2,19530310011542750,north,5 
5,19780506121938486,south,6 
3,19201602155516116,south,2 
3,19381909204504683,north,2 
4,19641002092156003,south,10 
1,19142109082009062,south,8 
5,19762306242234798,north,11 
2,19261008163849534,south,2 
1,19093003100715152,south,12 
1,19282102090128629,north,2 
5,19652606190400678,south,3 
3,19162302062356493,south,3 
5,20151605125616260,north,6 

とVBA関数:ここ

Option Explicit 

Sub filterData() 

    Dim fileNum As Integer 
    Dim csvLine As String 

    Dim arrLine() As String  ' fields from the csv line, 0-based 
    Const FIELD = 0: Const DATEHOUR = 1: Const BOUND = 2: Const CATEGORY = 3 

    Dim arrResult() As String ' string array for filtered data 
    Dim idx As Long: idx = 1  ' current index in the array 
    ReDim arrResult(idx To 1000) ' initial size 

    ' read file line-by-line 
    fileNum = FreeFile() 
    Open ThisWorkbook.Path & Application.PathSeparator & "dataFile.csv" _ 
      For Input As #fileNum 

    While Not EOF(fileNum) 
     Line Input #fileNum, csvLine 
     ' 0-FIELD, 1-DATEHOUR, 2-BOUND, 3-CATEGORY 
     arrLine = Split(csvLine, ",") 

     ' check the condition for inclusion 
     If arrLine(FIELD) = "3" And Mid(arrLine(DATEHOUR), 7, 2) = "02" Then 
      If idx > UBound(arrResult) Then 
       ReDim Preserve arrResult(1 To UBound(arrResult) * 2) 
      End If 
      arrResult(idx) = csvLine 
      idx = idx + 1 
     End If 
    Wend 

    Close #fileNum 

    ' trim excess entries 
    ReDim Preserve arrResult(1 to idx - 1) 

    ' display results in the spreadsheet (only up to 1mm) 
    Worksheets("Sheet1").[a1].Resize(UBound(arrResult), 1) = Application.Transpose(arrResult) 

End Sub 

は、あなたが "シート1" で取得します結果である:

enter image description here

PS:または、UNIXを使用することができますgrep ...

P.S .:結果配列が1mmより大きい場合は、Excelで表示することはできません。配列内のエントリ数を制限したり、配列の一部のみを表示したり、複数の列に表示したり、結果配列を別のファイルに書き込んだりするために、コードを変更することができます。

関連する問題