2017-02-15 11 views
2

問題は、Excelスプレッドシートを解析するためのCSVファイルです。次のようにExcel VBA - 日付の解析エラー、文字列の処理

データの例は次のとおりです。 -

01/02/2015,MXP,0.4,150.00,Producing design document, 64111258 
02/06/2015,IHM,0.8,210.00,"Maximilian dolce, lorem ipsum", 64111258 
02/06/2015,AXSP,0.6,250.00,"Magnificent, thanks very much", 64111258 

現在、これは私がデータを解析するために使用していたコードである: - 次のように

Sub OpenCSV() 

    Dim filePath As String 

    Dim intChoice As Integer 

    Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False 

    intChoice = Application.FileDialog(msoFileDialogOpen).Show 

    rowIndex = 0 

    If intChoice <> 0 Then 

     filePath = Application.FileDialog(_ msoFileDialogOpen).SelectedItems(1) 

     Open filePath For Input As #1 

     Do Until EOF(1) 

      Line Input #1, LineFromFile 

      LineItem = Split(LineFromFile, ",") 

      Sheets("Sheet2").Cells(11, 2).Offset(rowIndex, 0).Value = LineItem(0) ' Date 
      Sheets("Sheet2").Cells(11, 2).Offset(rowIndex, 1).Value = LineItem(1) ' Code 
      Sheets("Sheet2").Cells(11, 2).Offset(rowIndex, 2).Value = LineItem(2) ' Hours 
      Sheets("Sheet2").Cells(11, 2).Offset(rowIndex, 3).Value = LineItem(3) ' Cost 
      Sheets("Sheet2").Cells(11, 2).Offset(rowIndex, 4).Value = LineItem(4) ' Description 

      rowIndex = rowIndex + 1 

     Loop 

     Close #1 

    End If 

End Sub 

問題は、次のとおりです -

  1. Excelセルに解析され、転記された02/06/2015などの日付は、06/02/2015として終了します。これは一貫して起こるのではなく、データセット内のさまざまな日付にランダムに発生します。
  2. CSVデリミタ4は、データ内にあるカンマだけでなく、正しく解析されなくなります。したがって、データは関連するセルに正しく転置されません。

これらのエラーを修正するにはどうすればよいですか?このような

+0

を使用したデータ►テキストオプションから外部データ►を入手だろうVBAはありません)同じ問題がありますか?問題がない場合は、マクロを試して記録し、必要に応じて変更するだけです。それがうまくいかない場合は、おそらく2番目の問題を助けることができます。しかし、なぜそれが起こっているのかを#1で判断するのは本当に分かります。 – Benjamin

答えて

0
Dim arr() As String 
    Dim newDate As Date 

    arr = Split(LineItem(0), "/") 
    newDate = DateSerial(Year:=arr(2), Month:=arr(1), Day:=arr(0)) 

が続い

Sheets("Sheet2").Cells(11, 2).Offset(rowIndex, 0).Value = newDate 
1

何かがあなたのために働く必要があります。

Sub tgr() 

    Dim wb As Workbook 
    Dim wsDest As Worksheet 
    Dim sFilePath As String 
    Dim aData As Variant 

    sFilePath = Application.GetOpenFilename("CSV Files, *.csv", MultiSelect:=False) 
    If sFilePath = "False" Then Exit Sub 'Pressed cancel 

    Set wb = ActiveWorkbook 
    Set wsDest = wb.Sheets("Sheet2") 

    Application.ScreenUpdating = False 
    With Workbooks.Open(sFilePath) 
     aData = .Sheets(1).Range("A1", .Sheets(1).Cells(.Sheets(1).Rows.Count, "E").End(xlUp)).Value 
     .Close False 
    End With 
    Application.ScreenUpdating = True 

    With wsDest.Range("B11").Resize(UBound(aData, 1), UBound(aData, 2)) 
     .Value = aData 
     .Resize(, 1).NumberFormat = "mm/dd/yyyy" 'Can set date format here, change to dd/mm/yyyy if needed 
    End With 

End Sub 
+0

コピーするデータに論理演算子を適用するにはどうすればよいですか?その非線形ならば、私はa1とEの間のすべてのセルではなく、特定のセルをすべての行にコピーしたいだけです。むしろ行単位で繰り返し行おうと思っています。それは可能ですか? – Resurgent

0

ほとんどの場合、問題は、あなたのデータとWindowsの地域設定の日付フォーマットの間のミスマッチです。いくつかの方法は、彼らが

  • 変更*.txtファイルへのファイルの種類に合わせて、この

    • 変更Windowsの地域設定を処理します。次に、日付列のデータ型を指定できるWorkbooks.OpenTextメソッドを使用します。
    • 同じことを可能にするデータ接続を作成します。 QueryTableの作成を続けないようにしてください。テーブルはすでにそこにあります。削除して再作成するか、更新してください。

    次は、QueryTableメソッドを示すコードです。エクセルGUIでは、これはあなたが直接ExcelにCSVを(インポートする場合


    Option Explicit 
    
    Sub OpenCSV() 
    Dim filePath As String 
    Dim intChoice As Integer 
    Dim WS As Worksheet 
    
    Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False 
    intChoice = Application.FileDialog(msoFileDialogOpen).Show 
    
    If intChoice <> 0 Then 
    
        filePath = Application.FileDialog(_ 
         msoFileDialogOpen).SelectedItems(1) 
    
        Set WS = Worksheets("sheet2") 
        With WS.QueryTables 
    
        'If it exists, either delete and re-import or refresh 
        If .Count > 0 Then 
         Range(.Item(1).Destination.Address).CurrentRegion.Delete 
         .Item(1).Delete 
        End If 
        End With 
    ' 
        With WS.QueryTables.Add(Connection:="TEXT;" & filePath, Destination:=WS.Range("$B$11")) 
         .Name = "New Text Document" 
         .FieldNames = True 
         .RowNumbers = False 
         .FillAdjacentFormulas = False 
         .PreserveFormatting = True 
         .RefreshOnFileOpen = False 
         .RefreshStyle = xlInsertDeleteCells 
         .SavePassword = False 
         .SaveData = True 
         .AdjustColumnWidth = True 
         .RefreshPeriod = 0 
         .TextFilePromptOnRefresh = False 
         .TextFilePlatform = 437 
         .TextFileStartRow = 1 
         .TextFileParseType = xlDelimited 
         .TextFileTextQualifier = xlTextQualifierDoubleQuote 
         .TextFileConsecutiveDelimiter = False 
         .TextFileTabDelimiter = True 
         .TextFileSemicolonDelimiter = False 
         .TextFileCommaDelimiter = True 
         .TextFileSpaceDelimiter = False 
    
         'make sure format argument matches format in the csv file 
         .TextFileColumnDataTypes = Array(xlDMYFormat) 
    
         .TextFileTrailingMinusNumbers = True 
         .Refresh BackgroundQuery:=False 
        End With 
    End If 
    End Sub 
    

  • 関連する問題