我想在Excel工作簿中创建一个宏,它将另一个工作簿中的表与DBX中的相应表进行比较

时间:2015-10-15 12:50:52

标签: excel excel-vba vba

我对VBA非常缺乏经验,但我正在尝试创建一个宏和Excel工作簿,它将另一个工作簿中的静态数据与SQL DBx中的相应表进行比较

我可以成功从我的Source Work Book中提取信息。但是,如果数据类型从numberic更改为string,则某些数据不会从源工作簿中复制。我还要复制标题。我已经复制了下面的这个子

我还希望在每个表中创建不同条目的输出?这可能吗

Sub Comparison()

Sub Comp()

'
' Macro1 Macro
' Macro recorded Aking

Dim aCtiveBook As String

x = 1
    Do While x <= 9


            aCtiveBook = ThisWorkbook.Sheets("INPUT").Range("A1").Offset(x, 0)

            Dim cn As ADODB.Connection
            Dim rt As ADODB.Recordset
            Dim cm As ADODB.Command
            Set rt = New ADODB.Recordset
            Set cn = New ADODB.Connection
            With cn
                .Provider = "Microsoft.Jet.OLEDB.4.0"
                .ConnectionString = "Data Source = " & aCtiveBook & ";" & _
            "Extended Properties=Excel 8.0;"
            .Open



                                Sheetq = ThisWorkbook.Sheets("INPUT").Range("B1").Offset(x, 0)
                                Sheetn = ThisWorkbook.Sheets("INPUT").Range("C1").Offset(x, 0)


                                rt.Open "Select * from" & "[" & Sheetq & "$" & "]", cn

                                ThisWorkbook.Sheets(Sheetn).Select
                                ThisWorkbook.Sheets(Sheetn).Range("A1:BA20000").Delete

                        Sheets(Sheetn).Range("A2").CopyFromRecordset rt
                        rt.Close


                        rt.Open "Select * from [" & Sheetq & "$" & "A1:Z1" & "]", cn

                        Sheets(Sheetn).Range("A1").CopyFromRecordset rt
                        rt.Close

                    End With
                x = x + 1

    Loop


End Sub

0 个答案:

没有答案