将数据从* .csv复制到excel

时间:2016-02-11 14:58:34

标签: excel excel-vba csv vba

我有一个csv文件,里面装有温度传感器的输出,它有日期,温度控制器地址,温度传感器1值,温度传感器2值,热功率,冷却功率和限制和报警状态,这里是一个示例,在记事本中打开时添加了空格以便于阅读:

2/10/2016 14:52:26.2, 1, 73.9039, 74.89208, 14.63515, 0, F, None, None, None,
2/10/2016 14:52:36.594, 1, 73.75067, 74.86765, 25.21247, 0, F, None, None, None,
2/10/2016 14:52:47.165, 1, 73.66284, 74.83871, 35.95927, 0, F, None, None, None,
2/10/2016 14:52:57.788, 1, 73.59991, 74.79031, 47.17537, 0, F, None, None, None,
2/10/2016 14:53:8.381, 1, 73.54018, 74.75883, 58.62064, 0, F, None, None, None,

但是,如果我在excel中打开它,我会得到以下格式:

52:26.2, 1, 73.9039, 74.89208, 14.63515, 0, F, None, None, None
52:36.6, 1, 73.75067, 74.86765, 25.21247, 0, F, None, None, None
52:47.2, 1, 73.66284, 74.83871, 35.95927, 0, F, None, None, None
52:57.8, 1, 73.59991, 74.79031, 47.17537, 0, F, None, None, None
53:08.4, 1, 73.54018, 74.75883, 58.62064, 0, F, None, None, None

我正在尝试在excel中使用vba从此csv文件中提取数据或 多个csv文件基于用户选择,通过文件选择器对话框进入工作簿进行绘图。我遇到的问题是日期和时间列没有正确显示。我认为它与我用于数据的格式有关,但我不确定。我使用的格式是

m/d/yyyy h:mm:ss.000

出于某种原因,这是excel文件中显示的数据:

1/0/1900 0:52:26.200, 1, 73.90390, 74.89208, 14.64, 0, F, None, None, None
1/0/1900 0:52:36.600, 1, 73.75067, 74.86765, 25.21, 0, F, None, None, None
1/0/1900 0:52:47.200, 1, 73.66284, 74.83871, 35.96, 0, F, None, None, None
1/0/1900 0:52:57.800, 1, 73.59991, 74.79031, 47.18, 0, F, None, None, None
1/0/1900 0:53:08.400, 1, 73.54018, 74.75883, 58.62, 0, F, None, None, None

我尝试将第一列格式化为格式为

的数字
0.0000000000

然后移动数据,但该列只是空白,因为我必须使用范围数据类型才能更改格式。这是我用来显示文件选择器和移动数据的代码:

    Sub fileDialogStart()

    Dim fd As FileDialog
    Dim vrtSelectedItem As Variant

    Set fd = Application.FileDialog(msoFileDialogFilePicker)

    With fd
        If .Show = -1 Then
            For Each vrtSelectedItem In .SelectedItems
                ' Finds the last row in the current workbook
                With ThisWorkbook.Sheets(1)
                    lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
                End With
                ' Imports the data from the selected file
                Call importData(vrtSelectedItem)
            Next vrtSelectedItem
        Else
        End If
    End With

    Set fd = Nothing
End Sub

Sub importData(ByVal filePath As String)

    Dim targetWorkbook As Workbook, sourceWorkbook As Workbook
    Dim finalRow As Integer
    'Dim targetData as Range

    Set sourceWorkbook = Workbooks.Open(filePath)

    With sourceWorkbook.Sheets(1)
        ' Finds the number of rows in the file selected from the file picker
        finalRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        ' Stores the first column
        targetData = .Range(.Cells(1, 1), .Cells(finalRow, 1))
    End With

    ' Sets the number format
    'targetData.NumberFormat = "0.0000000000"

    With ThisWorkbook.Sheets(1)
        ' Puts the new data into the current workbook after the last row of data
        ' in case there is already data in the workbook from a previous import
        .Range(.Cells(lastRow, 1), .Cells(lastRow + finalRow - 1, 1)) = targetData
    End With

    With sourceWorkbook.Sheets(1)
        ' Gets the rest of the data from the file selected
        sourceData = .Range(.Cells(1, 2), .Cells(finalRow, 10))
    End With

    With ThisWorkbook.Sheets(1)
        ' Puts the rest of the data into the current workbook
        .Range(.Cells(lastRow, 2), .Cells(lastRow + finalRow - 1, 10)) = sourceData
    End With

    sourceWorkbook.Save
    ThisWorkbook.Save
    sourceWorkbook.Close False
End Sub

在最初使用的子图中调用fileDialogStart()子,并在导入数据后将整个第一列的格式设置回m/d/yyy h:mm:ss.000。也许这个问题与我将csv文件作为工作簿打开的事实有关?我不确定。任何帮助将不胜感激,谢谢!

2 个答案:

答案 0 :(得分:2)

Workbooks.OpenText method允许您指定每个字段的TextFileColumnDataTypes property。这相当于Range.TextToColumns method。为第一个字段指定 xlMDYFormat 格式足以正确显示日期时间(包括它们的秒数)。

Sub openCSV()
    Dim fp As String, fn As String, wbCSV As Workbook

    fp = Environ("TMP")
    fn = "datetemp.csv"
    Debug.Print fp & Chr(92) & fn
    Workbooks.OpenText Filename:=fp & Chr(92) & fn, DataType:=xlDelimited, _
                       Tab:=False, Semicolon:=False, Space:=False, _
                       Other:=False, Comma:=True, FieldInfo:=Array(1, xlMDYFormat), _
                       Local:=True
    With ActiveWorkbook
        With Worksheets(1)
            With .Columns(1)
                .NumberFormat = "mm/dd/yyyy  hh:mm:ss.000"
                .AutoFit
            End With
        End With
    End With

End Sub

根据您自己的区域设置,可能不需要 Local:= True 。它适用于我的EN-US默认设置。

答案 1 :(得分:0)

Excel正在做一些奇怪的事情;但是,当我运行时:

Sub WTF()
   Dim FilesToOpen
   Dim wf As WorksheetFunction
   Set wf = Application.WorksheetFunction
   Dim ary, bry, DQ As String
   DQ = Chr(34)

   FilesToOpen = Application.GetOpenFilename _
      (FileFilter:="Text Files (*.csv), *.csv", Title:="Text Files to Open")

   Close #1
   Open FilesToOpen For Input As #1

   j = 1
   Do While Not EOF(1)
      Line Input #1, TextLine
      ary = Split(TextLine, ",")
      bry = Split(ary(0), " ")
         Cells(j, 1).Formula = "=datevalue(" & DQ & bry(0) & DQ & ")+timevalue(" & DQ & bry(1) & DQ & ")"
         Cells(j, 1).NumberFormat = "mm/dd/yyyy hh:mm:ss.000"
         For k = 2 To 10
            Cells(j, k) = ary(k - 1)
         Next k
      j = j + 1
   Loop

   Close #1
End Sub

在您发布的数据上,我得到:

http://localhost:8000/qconsole/

我无法解释的是,为什么Excel在直接打开文件方面表现不佳。

也许其他人可以解释这一点。