将Excel电子表格中的记录与DB中的记录进行比较

时间:2015-10-18 14:18:47

标签: excel vba excel-vba

我原本以为我遇到了记录集问题,看起来现在已经做了一些修修补补,当我将null值传递给我的SQL查询字符串并且获得执行时它不会识别字符串NULL在SQL

中为NULL

例如 rst.OPEN“SELECT * FROM XXX WHERE Y = int AND Z = NULL”

不起作用

rst.OPEN“SELECT * FROM XXX WHERE Y = int AND Z = datetime”

工作正常吗?

        Sub CompareExcel()

        ' Macro1 Macro
        ' Macro recorded Aking


        Dim TargetDB As String
        Dim TargetDBUser As String
        Dim TargetDBPassword As String

        ThisWorkbook.Sheets("INPUT").Select

        TargetDB = Cells(2, 14)
        TargetDBUser = Cells(3, 14)
        TargetDBPassword = Cells(4, 14)

        Dim cnn As ADODB.Connection
        Dim rst As ADODB.Recordset
        Set cnn = New ADODB.Connection
        Set rst = New ADODB.Recordset
        cnn.Open TargetDB, TargetDBUser, TargetDBPassword

            ThisWorkbook.Sheets("Summary").Select
            ActiveWorkbook.Sheets("Summary").Range("A7:A20000").Delete

            ThisWorkbook.Sheets("INPUT").Select
            ActiveWorkbook.Sheets("INPUT").Select
            RowCounter = Application.WorksheetFunction.CountA(Range("A2:A20"))

            CountAllExcellRecords = 0
            CountSuccessRecords = 0
            RowInSourceSheet = 1

                Do While RowInSourceSheet <= RowCounter

                    SourceSheet = ThisWorkbook.Sheets("INPUT").Range("B1").Offset(RowInSourceSheet, 0)
                    Worksheets(SourceSheet).Activate
                    RowCountSourceSheet = Application.WorksheetFunction.CountA(Range("A:A"))
                    CriteriaBuilder = "('"
                    ColumnCountSourceSheet = ThisWorkbook.Sheets(SourceSheet).UsedRange.Rows(1).Columns.Count
                    SourceSheetRow = 1

                        Do While SourceSheetRow < RowCountSourceSheet

                            QvAriable = " WHERE"
                            QtAble = ""
                            SourceSheetColumn = 0

                                Do While SourceSheetColumn < ColumnCountSourceSheet


                                    qvAriablen = ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn)
                                    qvAriablem = ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(SourceSheetRow, SourceSheetColumn)

                                    Worksheets(SourceSheet).Activate
                                    Cells.Range("A1").Offset(SourceSheetRow, SourceSheetColumn).Activate

                                        Application.Volatile


                                            Select Case True
                                            Case IsEmpty(ActiveCell): CellType = "Blank"

                                                If qvAriablem = "" Then toSQL = " Null "
                                                qvAriablem = toSQL

                                                QvAriable = QvAriable & " " & qvAriablen & " = " & qvAriablem & ""
                                                QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn)

                                            Case Application.IsText(ActiveCell): DataType = "Text"

                                                QvAriable = QvAriable & " " & qvAriablen & " = '" & qvAriablem & "'"
                                                QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn)

                                            Case Application.IsLogical(ActiveCell): CellType = "Logical"

                                                QvAriable = QvAriable & " " & qvAriablen & " = " & qvAriablem & " "
                                                QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn)

                                            Case Application.IsErr(ActiveCell): CellType = "Error"

                                                QvAriable = QvAriable & " " & qvAriablen & " = " & qvAriablem & " "
                                                QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn)

                                            Case IsDate(ActiveCell): DataType = "Date"

                                                qvAriablem = Format(qvAriablem, "MM/DD/YYYY")
                                                QvAriable = QvAriable & " " & qvAriablen & " = '" & Format(qvAriablem, "MM/DD/YYYY") & "'"
                                                QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn)


                                            Case InStr(1, ActiveCell.Text, ":") <> 0: CellType = "Time"

                                                qvAriablem = Format(qvAriablem, "MM/DD/YYYY")
                                                QvAriable = QvAriable & " " & qvAriablen & " = '" & Format(qvAriablem, "MM/DD/YYYY") & "'"
                                                QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn)


                                            Case IsNumeric(ActiveCell): CellType = "Value"

                                                Worksheets("INPUT").Activate
                                                RowCountExceptions = Application.WorksheetFunction.CountA(Range("I:I"))
                                                Exceptions = 2
                                                eXceptionCount = 0

                                                    Do While Exceptions <= RowCountExceptions

                                                        If qvAriablen = Cells(Exceptions, 9) Then

                                                        eXceptionCount = eXceptionCount + 1
                                                        QvAriable = QvAriable & " " & qvAriablen & " = '" & qvAriablem & "'"
                                                        QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn)

                                                        End If

                                                        Exceptions = Exceptions + 1
                                                     Loop

                                                        If eXceptionCount = 0 Then

                                                        QvAriable = QvAriable & " " & qvAriablen & " = " & qvAriablem & " "
                                                        QtAble = QtAble & " " & ThisWorkbook.Sheets(SourceSheet).Range("A1").Offset(0, SourceSheetColumn)


                                                        End If


                                                End Select


                                            If SourceSheetColumn < ColumnCountSourceSheet - 1 Then
                                            QvAriable = QvAriable & " AND "
                                            QtAble = QtAble & ", "
                                            End If

                                        SourceSheetColumn = SourceSheetColumn + 1

                                    Loop

                                rst.Open "SELECT * FROM " & SourceSheet & QvAriable, cnn
                                'rst.Open "SELECT * FROM UNIT_FUND WHERE CURRENCY_CODE = 'GBP' AND  UNIT_FUND_CODE = '150' AND FUND_NAME = 'MetLife Fidelity Cash Fund' AND  EFFECTIVE_DATE = '10/21/2009' AND CEASE_DATE =" & "NULL"
                                Sheets("Summary").Range("G2").CopyFromRecordset rst


                                CountAllExcellRecords = CountAllExcellRecords + 1

                                    If rst.RecordCount > 0 Then

                                        CountSuccessRecords = CountSuccessRecords + 1

                                    Else

                                        ThisWorkbook.Sheets("Summary").Select
                                        ActiveWorkbook.Sheets("Summary").Select
                                        a = "SELECT * FROM " & SourceSheet & QvAriable
                                        NextRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1
                                        Cells(NextRow, 1) = a

                                    End If

                                rst.Close

                             SourceSheetRow = SourceSheetRow + 1

                        Loop

                    RowInSourceSheet = RowInSourceSheet + 1

                Loop

            ThisWorkbook.Sheets("Summary").Select

            Cells(3, 3) = CountAllExcellRecords
            Cells(4, 3) = CountSuccessRecords
            Cells(5, 3) = CountSuccessRecords - CountAllExcellRecords


        End Sub

1 个答案:

答案 0 :(得分:0)

RecordCount属性取决于游标位置和类型。您似乎正在检查是否有任何记录,所以您可以使用:

If Not rst.EOF Then

而不是:

If rst.RecordCount <> 0 Then