代码之家  ›  专栏  ›  技术社区  ›  chefsmart

用于检查数据一致性的Excel字符串操作

  •  1
  • chefsmart  · 技术社区  · 16 年前

    背景信息:有近7000个人,有关于他们在一次、两次或三次测试中表现的数据。

    每个人都参加了第一次考试(我们称之为 试验M )一些参加过M测试的人也参加过 测试I 还有一些参加过考试的人我也参加过 测试B .

    对于前两个测试(M和I),学生可以得分 I、II或III级 . 取决于他们的成绩 一级得3分,二级得2分,三级得1分 .

    最后一个测试B只是一个没有分数的及格或不及格的结果。通过此项测试者得1分,不得分。(好吧,事实上,成绩是有奖的,但是所有的成绩都有一个共同的1分)。

    一位业余爱好者将代表这些学生及其成绩的数据输入到Excel文件中。问题是,这个人做了最糟糕的事情——他开发了自己的符号,把所有的测试信息输入到一个单元里——让我的生活陷入地狱。

    文件最初有两个文本列,一个用于个人的ID,另一个用于测试信息(如果可以调用的话)。

    alt text http://i48.tinypic.com/5tv0bl.png 我知道这很可怕,而且我很痛苦。在图片中,如果看到“M-II-2 I-III-1”,则表示该人在M测试中获得2分的二级成绩,在I测试中获得1分的三级成绩。有的只做了一次测试,有的只做了两次,有的只做了三次。

    当这个文件来我这里处理和分析学生的表现时,我把它连同说明一起发回来,插入3个附加的列,其中只有三个测试的成绩。文件现在看起来如下。C列和D列分别用1、2和3表示等级I、II和III。C列用于测试M,D列用于测试I。E列表示BA(B已实现!)如果个人通过了测试B。

    alt text http://i50.tinypic.com/16c0yvr.png

    既然您已经掌握了上述信息,那么让我们来讨论一下这个问题。我不相信这一点,我想检查B列中的数据是否与C、D和E列中的数据匹配。

    也就是说,我想检查B列中的字符串,并找出C、D和E列中的数字是否正确。

    所有的帮助都非常感谢。

    P.S.-我已经通过ODBC将其导出到了MySQL,这就是为什么您看到这些空值的原因。我也尝试在MySQL中这样做,并且真的会接受MySQL或Excel解决方案,我没有偏好。

    Edit : - See file with sample data

    1 回复  |  直到 15 年前
        1
  •  0
  •   Fionnuala    16 年前

    要从原始数据创建平面文件,请执行以下操作:

    Sub GetData()
        Dim cn As Object
        Dim rs As Object
        Dim strFile As String
        Dim strCon As String
        Dim strSQL As String
        Dim s As String, t As Variant, x As Variant
        Dim i As Integer, j As Integer, k As Integer
    
        ''This is not the best way to refer to the workbook
        ''you want, but it is very conveient for notes
        ''It is probably best to use the name of the workbook.
    
        strFile = ActiveWorkbook.FullName
    
        ''Note that if HDR=No, F1,F2 etc are used for column names,
        ''if HDR=Yes, the names in the first row of the range
        ''can be used.
        ''This is the Jet 4 connection string, you can get more
        ''here : http://www.connectionstrings.com/excel
    
        strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
            & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
    
        ''Late binding, so no reference is needed
    
        Set cn = CreateObject("ADODB.Connection")
        Set rs = CreateObject("ADODB.Recordset")
    
    
        cn.Open strCon
    
        strSQL = "SELECT * " _
               & "FROM [Sheet1$] "
    
        ''Open the recordset for more processing
        ''Cursor Type: 3, adOpenStatic
        ''Lock Type: 3, adLockOptimistic
        ''Not everything can be done with every cursor type and
        ''lock type. See http://www.w3schools.com/ado/met_rs_open.asp
    
        rs.Open strSQL, cn, 3, 3
    
        ''Pick a suitable empty worksheet for the results
    
        With Worksheets("Sheet2")
    
            ''Fill headers into the first row of the worksheet
    
    
            .Cells(1, 1) = "ID"
            .Cells(1, 2) = "Exam"
            .Cells(1, 3) = "Grade"
            .Cells(1, 4) = "Points"
    
            ''Working with the recordset ...
    
            ''Counter for Fields/Columns in Recordset and worksheet
            ''Row one is used with titles, so ...
            i = 1
    
            Do While Not rs.EOF
    
    
                ''Store the ID to a string (if it is a long,
                ''change the type) ...
    
                s = rs!ID
    
                t = Split(rs!testinfo, " ")
    
                For j = 0 To UBound(t)
                    ''(Counter)
                    i = i + 1
    
                    .Cells(i, 1) = s
    
                   x = Split(t(j), "-")
    
                    For k = 0 To UBound(x)
                        If t(j) = "BA-1" Then
                            .Cells(i, 2) = "B"
                            .Cells(i, 3) = "A"
                            .Cells(i, 4) = 1
                        Else
                            .Cells(i, k + 2) = x(k)
                        End If
                    Next
                Next
    
    
                ''Keep going 
                rs.MoveNext
    
            Loop
    
       ''Finished with the sheet
       End With
    
       ''Tidy up
       rs.Close
       Set rs = Nothing
       cn.Close
       Set cn = Nothing
    End Sub
    

    要检查额外的列:

    Sub CheckData()
        Dim cn As Object
        Dim rs As Object
        Dim strFile As String
        Dim strCon As String
        Dim strSQL As String
        Dim s As String, t As Variant, x As Variant
        Dim i As Integer, j As Integer, k As Integer
        Dim BAErr, MErr, IErr
    
        strFile = ActiveWorkbook.FullName
    
        strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
            & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
    
        Set cn = CreateObject("ADODB.Connection")
        Set rs = CreateObject("ADODB.Recordset")
    
        cn.Open strCon
    
        strSQL = "SELECT * " _
               & "FROM [Sheet1$] "
    
        rs.Open strSQL, cn, 3, 3
    
        Do While Not rs.EOF
    
            t = Split(rs!testinfo, " ")
    
            For j = 0 To UBound(t)
               x = Split(t(j), "-")
    
               Select Case x(0)
                    Case "BA"
                        If rs![test b] <> "BA" Then
                            BAErr = BAErr & "," & rs!ID
                        End If
                    Case "M"
                        If String(rs![test m], "I") <> x(1) Then
                            MErr = MErr & "," & rs!ID
                        End If
                    Case "I"
                        If String(rs![test i], "I") <> x(1) Then
                            IErr = IErr & "," & rs!ID
                        End If
               End Select
            Next
    
            rs.MoveNext
    
        Loop
    
    
       ''Tidy up
       rs.Close
       Set rs = Nothing
       cn.Close
       Set cn = Nothing
    
       If BAErr <> "" Then
          MsgBox Mid(BAErr, 2), , "B Errors"
       End If
    
       If MErr <> "" Then
          MsgBox Mid(MErr, 2), , "M Errors"
       End If
    
       If IErr <> "" Then
          MsgBox Mid(IErr, 2), , "I Errors"
       End If
    
    End Sub