我有很多.csv文件,其数据格式为:
Time data1 data2 data3
0:00:00 1.22 1.23 432
0:01:00 1.52 1.13 432
0:02:00 1.12 1.15 431
但是某些csv文件跳过了以下时隙:
Time data1 data2 data3
0:00:00 1.22 1.23 432
0:03:00 1.71 1.43 431
0:04:00 1.79 1.54 432
数据来自机器监视系统,该系统定期收集数据。关闭或出现问题时,它将跳过某些时隙。我想从所有csv文件中提取特定行,说 data2 到xlsx文件中,同时保留被跳过为空单元格的时隙,例如:
Time data2 data2
0:00:00 1.23 1.23
0:01:00 1.13
0:02:00 1.15
0:03:00 1.22 1.43
0:04:00 1.71 1.54
我对excel宏/ VBA并不熟悉,但是希望获得有关如何解决此问题的任何建议,谢谢!
答案 0 :(得分:0)
您可以像这样从CSV读取特定内容。
Sub sbADO()
Dim sSQLQry As String
Dim ReturnArray
Dim Conn As New ADODB.Connection
Dim mrs As New ADODB.Recordset
Dim DBPath As String, sconnect As String
DBPath = "C:\Users\Excel\Desktop\"
sconnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBPath & ";Extended Properties='text;HDR=YES;FMT=Delimited'"
Conn.Open sconnect
sSQLSting = "SELECT * From CSV1.csv WHERE ID = 2"
mrs.Open sSQLSting, Conn
ActiveSheet.Range("A2").CopyFromRecordset mrs
'Close Recordset
mrs.Close
Conn.Close
End Sub
或者,合并所有CSV文件中的所有日期,然后删除不需要/不需要的内容。
Sub AnalysisMerger2()
Dim WSA As Worksheet
Dim bookList As Workbook
Dim SelectedFiles As Variant
Dim NFile As Long
Dim FileName As String
Dim Ws As Worksheet, vDB As Variant, rngT As Range
Dim vFn, myFn As String
Application.ScreenUpdating = False
SelectedFiles = Application.GetOpenFilename(filefilter:="Excel Files (*.csv*), *.csv*", MultiSelect:=True)
If IsEmpty(SelectedFiles) Then Exit Sub
For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
FileName = SelectedFiles(NFile)
vFn = Split(FileName, "\")
myFn = vFn(UBound(vFn))
myFn = Replace(myFn, ".csv", "")
Set bookList = Workbooks.Open(FileName, Format:=2)
Set WSA = bookList.Sheets(1)
vDB = WSA.UsedRange
bookList.Close (0)
Set Ws = Sheets.Add(after:=Sheets(Sheets.Count))
ActiveSheet.Name = myFn
Ws.Range("a1").Resize(UBound(vDB, 1), UBound(vDB, 2)) = vDB
Next
Application.ScreenUpdating = True
End Sub