0

问题

在特定字段中包含许多不正确或不一致输入的日期的大型数据集。在大约 60,000 条记录中输入日期的方式大约有 19 种。有些条目完全无效,必须丢弃,其他条目必须正确格式化。我将范围加载到数组中,然后执行操作。我写出了我认为我需要的操作,但是我需要一些帮助才能真正让它们工作。请参阅下面的代码;评论中的错误。

当前问题

对于像“54 Days”这样的条目,下面的代码应该只取空格左边的字符,然后检查它们是否是数字。如果是,则保留它们,如果不是,则清空该数组元素。在实践中,什么都没有发生,我仍然在数组元素中有完整的条目?

ElseIf delType = "String" And Len(del(i, 1)) < 10 And InStrRev(del(i, 1), " ") Then 'Takes the number from entries like 2194 Days or 23 DPD
    del(i, 1) = Left(del(i, 1), Len(del(i, 1)) - InStrRev(del(i, 1), " "))
    If IsNumeric(del(i, 1)) = False Then 'If the characters to the left of the space are not numbers, discard
        del(i, 1) = Empty
    Else
        del(i, 1) = Format((CLng(Range("E" & i + 1).Value) - Abs(del(i, 1))), "mm/dd/yy") 'Pull order date and subtract days from it for delinquency date
    End If

输入模板

九月。25, 20(没有年份,没有年份!删除。)
SEPT(没有年份,没用,删除。)
N/A(垃圾!删除。)
LONG TIME AG(傻瓜认为这是个好主意,删除。)
6 月 30 日, 200(显然该字段只能保存 12 个字符,删除。)
CHARGED OFF(无用,删除。)
94 DAYS(取空格前的所有字符并从包含订单日期的其他字段中减去以获得拖欠日期。)
94 DPD(DPD in某人聪明的头脑代表我相信过期的日子。同上。)
2008-7-15 12(不确定额外的数字是什么,取空格前的所有字符并转换。)
无效(删除。)
空白(什么都不做。)
12282009(使用嵌套的 LEFT 和 RIGHT 并在其间使用 / 连接。)
9202011(添加前导零,然后同上。)
92410(添加前导零,这将转换为 09/24/10)
41261(自 1899 年 12 月 31 日以来的天数,这将转换为 12/08/12)
1023(自拖欠日期以来的天数,从 ORDER DATE 中减去以获得拖欠日期。)
452(同上。)
12(同上。)
1432.84(货币价值,智商低的走狗误入,删除。)

代码(正在进行中)

'Perform housekeeping on delinquency date
Columns("AH:AH").Select
Selection.NumberFormat = "0"
Selection.Copy
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    True, Transpose:=False
Dim del()
ReDim del(1 To importwsRowCount, 1 To 1)
del = Range("AH1:AH" & importwsRowCount).Value
Dim delChars As Long
Dim delType As String
For i = LBound(del, 1) To UBound(del, 1)
    delChars = Len(del(i, 1)) 'Determine length of entry
    If IsNumeric(del(i, 1)) = True Then 'Determine datatype of entry
        delType = "Numeric"
    Else
        delType = "String"
    End If
    If InStr(del(i, 1), ".") Then 'Removes monetary entries like 142.84
        del(i, 1) = Empty
    ElseIf InStr(del(i, 1), "*") Then 'Removes ***INVALID*** entries
        del(i, 1) = Empty
    ElseIf delChars = 12 Then 'Removes all entries that extend beyond the 12 character limit of the field and get cut off
        del(i, 1) = Empty
    ElseIf delType = "String" And Len(del(i, 1)) < 10 And InStrRev(del(i, 1), " ") Then 'Takes the number from entries like 2194 Days or 23 DPD
        del(i, 1) = Left(del(i, 1), Len(del(i, 1)) - InStrRev(del(i, 1), " "))
        If IsNumeric(del(i, 1)) = False Then 'If the characters to the left of the space are not numbers, discard
            del(i, 1) = Empty
        Else
            del(i, 1) = Format((CLng(Range("E" & i + 1).Value) - Abs(del(i, 1))), "mm/dd/yy") 'Pull order date and subtract days from it for delinquency date
        End If
    ElseIf delType = "Numeric" And Len(del(i, 1)) = 5 Then
        If del(i, 1) > CLng(Date) Then 'Value is greater than todays date, improperly formated date that needs character manipulation and / added
            del(i, 1) = Format(del(i, 1), "000000") 'Add leading zero
            del(i, 1) = DateSerial(Right(del(i, 1), 2), Left(del(i, 1), 2), Right(Left(del(i, 1), 2), 4)) 'Grab year, then month, then day for serialize
        Else
            del(i, 1) = Format(del(i, 1), "mm/dd/yy") 'Properly formated date that just needs format conversion
        End If
    ElseIf delType = "Numeric" And (delChars = 7 Or delChars = 8) Then
        If delChars = 7 Then
            del(i, 1) = Format(del(i, 1), "00000000") 'Add leading zero
        End If
        del(i, 1) = DateSerial(Right(del(i, 1), 4), Left(del(i, 1), 2), Right(Left(del(i, 1), 2), 6)) 'Grab year, then month, then day for serialize
    ElseIf delType = "Numeric" And delChars < 5 Then
        del(i, 1) = Format((CLng(Range("E" & i + 1)) - Abs(del(i, 1))), "mm/dd/yy")
    End If
Next i
Set delRange = Range("AJ1:AJ" & importwsRowCount)
iWS.names.Add Name:="dRange", RefersTo:=delRange
Range("dRange").Value = del 'Write array to worksheet
4

1 回答 1

0

“54 天”包含 7 个字符,所以Len("54 Days")是 7

空格字符出现在位置 3 并InStrRev("54 Days", " ")返回 3

所以,我们有Left("54 Days", 7 - 3)哪个产生"54 D"显然不是数字的

Left(del(i, 1), InStrRev(del(i, 1), " ") - 1)也许试试

于 2012-11-06T22:50:04.253 回答