|
楼主 |
发表于 2024-8-22 15:24
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub limonet()
Dim Cn As Object, StrSQL$, Arr As Variant, i%, Brr As Variant, j%, Sht As Range, Crr As Variant
Brr = Worksheets("人员名册").Range("A3:K3")
Set Cn = CreateObject("Adodb.Connection")
Cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.FullName
Crr = Application.Transpose(Application.Transpose(Worksheets("人员名册").Range("F3:J3")))
For Each Sht In Worksheets("人员名册").Range("F3:J3")
Debug.Print Sht.Address(0, 0) & ":" & Left(Sht.Address(0, 0), 1)
StrSQL = StrSQL & " Union All Select * From [人员名册$" & Sht.Address(0, 0) & ":" & Left(Sht.Address(0, 0), 1) & "]"
Next Sht
Arr = Cn.Execute("Select Distinct 类别1 From (" & Mid(StrSQL, 12) & ") Where 类别1<>''").GetRows
For i = 0 To UBound(Arr, 2)
j = Worksheets("成表").Range("B" & Rows.Count).End(xlUp).Row + 2
StrSQL = "Select * From [人员名册$A3:K] Where " & Join(Crr, "='" & Arr(0, i) & "' or ") & "='" & Arr(0, i) & "'"
Worksheets("成表").Range("A" & j).Resize(1, 11) = Brr
Worksheets("成表").Range("A" & j + 1).CopyFromRecordset Cn.Execute(StrSQL)
Next i
Worksheets("成表").Rows("1:2").Delete
End Sub
老师,我把sheet4,也改了。这样改对吗?
|
|