VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > vb >
  • excel vba教程之ADODBRecordset中取不重复值

1.  ADODBRecordset中取不重复值

Public Sub RunMacro()

    Dim lcConnectString, lcCommandText As String
    Dim loADODBConnection As ADODB.Connection
    Dim loADODBRecordset As ADODB.Recordset
    ActiveWorkbook.Save
    lcConnectionString = "Driver={Microsoft Excel Driver (*.xls)}; " & _
                            "DBQ=" + ActiveWorkbook.FullName + ";" & _
                            "ReadOnly=True"
    lcCommandText = "select distinct 学生,到过的国家 from [sheet1$]"
    Set loADODBConnection = CreateObject("ADODB.Connection")
    Set loADODBRecordset = CreateObject("ADODB.Recordset")
    loADODBConnection.Open lcConnectionString
    loADODBRecordset.Open lcCommandText, loADODBConnection, 3, 1, 1
    Sheets.Add
    Dim r, f As Integer
    r = 1
    For f = 0 To loADODBRecordset.Fields.Count - 1
        Sheets(1).Cells(r, f + 1) = loADODBRecordset.Fields(f).Name
    Next
    While Not loADODBRecordset.EOF
        r = r + 1
        For f = 0 To loADODBRecordset.Fields.Count - 1
            Sheets(1).Cells(r, f + 1) = loADODBRecordset.Fields(f).Value
        Next
        loADODBRecordset.MoveNext
    Wend
    loADODBConnection.Close
End Sub

相关教程