|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub test1() '加一方法
- Dim ar, br, Conn As Object
- Dim SQL As String, strConn As String, tb As String, i As Long
-
- If Application.Version < 12 Then
- strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source="
- Else
- strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source="
- End If
- Set Conn = CreateObject("ADODB.Connection")
- Conn.Open strConn & ThisWorkbook.FullName
-
- With Worksheets(2).Range("A1").CurrentRegion
- ar = Application.Rept(.Rows(1), 1)
- tb = .Parent.Name & "$" & .Address(0, 0)
- End With
- For i = 2 To UBound(ar)
- SQL = SQL & " UNION ALL SELECT " & ar(1) & ",'" & ar(i) & "' AS 科目," & ar(i) & " AS 占比 FROM [" & tb & "]"
- Next
-
- With Worksheets(1).Range("A1").CurrentRegion
- br = WorksheetFunction.Transpose(Intersect(.Columns(1), .Columns(1).Offset(1)))
- tb = .Parent.Name & "$" & .Address(0, 0)
- End With
- SQL = "SELECT b.班级,a.科目,b.总成绩*a.占比 AS 成绩 " & _
- "FROM (" & Mid(SQL, 12) & ") a RIGHT JOIN [" & tb & "] b " & _
- "ON a.班级=b.班级 " & _
- "ORDER BY INSTR('" & Join(br, ",") & "',b.班级),INSTR('" & Join(ar, ",") & "',a.科目)"
-
- With Worksheets(3).Range("A1")
- .CurrentRegion.ClearContents
- .Resize(, 3) = Split("班级 科目 成绩")
- .Offset(1).CopyFromRecordset Conn.Execute(SQL)
- .CurrentRegion.Columns(2).Replace "占比", "", xlPart
- End With
-
- Conn.Close
- Set Conn = Nothing
- Beep
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|