|
ADO法请参考:- Sub Macro1()
- Dim cnn As Object, rs As Object, i&, j&, SQL$, sh As Worksheet
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- For Each sh In Worksheets
- If sh.Name <> "Sheet1" Then sh.Delete
- Next
- Set cnn = CreateObject("ADODB.Connection")
- cnn.Open "Provider = Microsoft.Jet.Oledb.4.0;Extended Properties ='Excel 8.0;imex=1';Data Source =" & ThisWorkbook.FullName
- SQL = "select distinct 姓名 from [Sheet1$]"
- s = "select format(身份证,'000000000000000000') as 身份证," & Join([d1:t1&""], ",") & " from [Sheet1$] where 姓名='"
- arr = cnn.Execute(SQL).GetRows
- For j = 0 To UBound(arr, 2)
- Worksheets.Add(After:=Sheets(Sheets.Count)).Name = arr(0, j)
- SQL = s & arr(0, j) & "'"
- Set rs = cnn.Execute(SQL)
- For i = 1 To rs.Fields.Count
- Cells(1, i) = rs.Fields(i - 1).Name
- Next
- Range("a2").CopyFromRecordset rs
- Next
- rs.Close
- cnn.Close
- Set rs = Nothing
- Set cnn = Nothing
- Application.ScreenUpdating = True
- End Sub
复制代码 |
|