我从我们的手机系统中丢弃了一些CDR文件,我需要整理CVS文件中的数据,以便轻松阅读报告。手动完成它几乎需要一整天才能完成,因为我们接到了无数的电话。我在这里找到了这个代码,它可以很好地删除我不需要的所有列。但是,它只是我需要做的一部分。
我想对文件做的其他事情是:
我正在使用Excel 2016。
Sub deleteIrrelevantColumns()
Dim keepColumn As Boolean
Dim currentColumn As Integer
Dim columnHeading As String
currentColumn = 1
While currentColumn <= ActiveSheet.UsedRange.Columns.Count
columnHeading = ActiveSheet.UsedRange.Cells(1, currentColumn).Value
'CHECK WHETHER TO KEEP THE COLUMN
keepColumn = False
If columnHeading = "dateTimeOrigination" Then keepColumn = True
If columnHeading = "callingPartyNumber" Then keepColumn = True
If columnHeading = "originalCalledPartyNumber" Then keepColumn = True
If columnHeading = "finalCalledPartyNumber" Then keepColumn = True
If columnHeading = "dateTimeConnect" Then keepColumn = True
If columnHeading = "dateTimeDisconnect" Then keepColumn = True
If columnHeading = "lastRedirectDn" Then keepColumn = True
If columnHeading = "duration" Then keepColumn = True
If keepColumn Then
'IF YES THEN SKIP TO THE NEXT COLUMN,
currentColumn = currentColumn + 1
Else
'IF NO DELETE THE COLUMN
ActiveSheet.Columns(currentColumn).Delete
End If
'LASTLY AN ESCAPE IN CASE THE SHEET HAS NO COLUMNS LEFT
If (ActiveSheet.UsedRange.Address = "$A$1") And
(ActiveSheet.Range("$A$1").Text = "") Then Exit Sub
Wend
End Sub
答案 0 :(得分:0)
查看下面的重构代码并将其粘贴到VBA项目的ThisWokbook
模块中,此子项由workbook_open
事件触发,并且每次打开此特定文档时都应触发。
如果要自动将此应用于新文件,则必须在文件中创建VBA项目,该文件将打开新文件,处理它们并保存它们。它可能有点复杂,但如果你做一些研究,你应该能够做到。
Private Sub Workbook_Open()
Dim blnColDeleted As Boolean, blnEpoch As Boolean
Dim columnHeading As String, strFormat As String
Dim i As Long, j As Long
Dim arrTMP As Variant
Dim lngTMP As Long
With ThisWorkbook.Worksheets("Sheet1") 'Change Sheet1 to your actual sheetname
For i = .UsedRange.Columns.Count To 1 Step -1 'Create a for loop which steps backwards to compensate for deleted columns
blnColDeleted = False 'Reset some variables for the next column
blnEpoch = False
columnHeading = emptyy
strFormat = Empty
Select Case .Cells(1, i)
Case "dateTimeOrigination", "Date Time"
columnHeading = "Date Time" 'Change to what you actually want the columnheading to be, goes for all cases
strFormat = "dd/mm/yy hh:mm:ss;@" 'Change the string to change the format to suit your needs
blnEpoch = True 'If this column contains epoch timestamps, set to true to convert them
Case "callingPartyNumber", "Calling Number"
columnHeading = "Calling Number"
strFormat = Empty 'Change to desired formatting, leave empty if no formatting required"
blnEpoch = False
Case "originalCalledPartyNumber", "Original Calld Party Number"
columnHeading = "Original Calld Party Number"
strFormat = Empty
blnEpoch = False
Case "finalCalledPartyNumber", "Final Called Party Number"
columnHeading = "Final Called Party Number"
strFormat = Empty
blnEpoch = False
Case "dateTimeConnect", "Date Time Connect"
columnHeading = "Date Time Connect"
strFormat = "dd/mm/yy hh:mm:ss;@"
blnEpoch = True
Case "dateTimeDisconnect", "Date Time Disconnect"
columnHeading = "Date Time Disconnect"
strFormat = "dd/mm/yy hh:mm:ss;@"
blnEpoch = True
Case "lastRedirectDn", "Last Redirect Dn"
columnHeading = "Last Redirect Dn"
strFormat = Empty
blnEpoch = False
Case "duration", "Duration"
columnHeading = "Duration"
strFormat = "[m]:ss;@" 'The square brackets around "mm" show elapsed minutes
blnEpoch = False
Case Else
blnColDeleted = True
.Columns(i).Delete 'If the column header does not match any of the cases, then it is deleted
End Select
If Not blnColDeleted Then
.Cells(1, i) = columnHeading
If blnEpoch And Not .Cells(2, i) < Now() Then
arrTMP = .Range(.Cells(2, i), .Cells(2, i).End(xlDown)) 'Introduced arrays to quickly convert large batches instead of cell by cell. Continous read write operations directly interacting with the sheet will be very resource heavy
For j = LBound(arrTMP, 1) To UBound(arrTMP, 1)
lngTMP = arrTMP(j, 1)
arrTMP(j, 1) = timeCorrected(lngTMP)
Next j
With .Range(.Cells(1, i).Offset(1, 0), .Cells(1, i).Offset(UBound(arrTMP, 1), 0))
.Value = arrTMP
.NumberFormat = strFormat
End With
ElseIf Not strFormat = Empty Then
.Range(.Cells(2, i), .Cells(2, i).End(xlDown)).NumberFormat = strFormat
End If
.Columns(i).AutoFit
End If
Next i
End With
End Sub
将以下功能添加到模块中,以便任何程序都可以引用它。此函数以Excel日期时间格式(自0开始计算的天数)返回当年3月的第2个星期日和该年11月的第1个星期日之间的UTC-6(CST)和UTC-5(CST与DST)的纪元时间/ 1/1900,以及小数符号后面的时间,例如24/05/2017 12:00:00 = 42879.5)。我已编辑上面的代码以合并此功能。我希望这会有所帮助,你可以从这里开始。
Function timeCorrected(myEpoch As Long) As Double
Dim lngYear As Long, lngMonth As Long, lngDay As Long
Dim dblTime As Double, dblStartDST As Double, dblEndDST As Double
Dim i As Long
dblTime = DateAdd("s", myEpoch - 21600, "01/01/1970") 'Time is converted to CST without DST correction (UTC - 6)
lngYear = Year(dblTime)
lngMonth = Month(dlbtime)
lngDay = Day(dblTime)
'DST starts second Sunday in March
For i = DateSerial(lngYear, 3, 8) To DateSerial(lngYear, 3, 14)
If Application.WorksheetFunction.Weekday(i, 2) = 7 Then
dblStartDST = i + TimeSerial(2, 0, 0)
Exit For
End If
Next
'DST ends first sunday in november
For i = DateSerial(lngYear, 11, 1) To DateSerial(lngYear, 11, 7)
If Application.WorksheetFunction.Weekday(i, 2) = 7 Then
dblEndDST = i + TimeSerial(2, 0, 0)
Exit For
End If
Next
If dblTime >= dblStartDST And dblTime < dblEndDST Then
timeCorrected = dblTime + TimeSerial(1, 0, 0)
Else
timeCorrected = dblTime
End If
End Function
至于删除遇到的所有列,我在lastRedirectDN
中发现了1个拼写错误导致该列的错误删除。
但更可能发生的事情是你已经两次运行这个宏了。在第一次运行此宏之后,所有列标题都将不同,这将导致所有列都被删除,因为所有列都将无法检查是否要保留它们。
这可以通过将新列标题添加到下面的案例中来修复,但请确保无论您将列标题设置为什么,都与Case
Case "dateTimeOrigination", "Date Time"
columnHeading = "Date Time"
strFormat = "dd/mm/yy hh:mm:ss"
Case "callingPartyNumber", "Calling Number"
columnHeading = "Calling Number"
strformat = Empty
至于纪元转换,我完全错过了,我已编辑上面的原始代码以允许纪元转换。