|
- Option Explicit
- Sub Test()
- Dim strShName As String, strFieldsName As String, strSplit() As String
- Dim SH As Worksheet, rgAdd As Range, rgResult As Range
- Dim lngRows_Old As Long, lngRows_New As Long
- Dim arrOld As Variant, arrAdd As Variant
- Dim lngRow As Long, lngCol As Long
- Dim lngIndex As Long
-
- Dim Conn As Object, Rst As Object, strPath As String
- Dim strConn As String, strSQL As String
-
- strShName = "Sheet1"
- Set SH = Sheets(strShName)
- lngRows_Old = SH.Range("A" & Rows.Count).End(xlUp).Row
- If lngRows_Old < 2 Then Exit Sub
-
- lngRows_New = (lngRows_Old - 1) * 2
-
- arrOld = SH.Range("A2:H" & lngRows_Old)
- ReDim arrAdd(1 To lngRows_New, 1 To 4)
- lngIndex = 1
-
- For lngRow = LBound(arrOld) To UBound(arrOld)
- For lngCol = 5 To 8 Step 2
- If arrOld(lngRow, lngCol) <> "" Then
- arrAdd(lngIndex, 1) = arrOld(lngRow, 1)
- arrAdd(lngIndex, 2) = arrOld(lngRow, 2)
- arrAdd(lngIndex, 3) = arrOld(lngRow, lngCol)
- arrAdd(lngIndex, 4) = arrOld(lngRow, lngCol + 1)
- lngIndex = lngIndex + 1
- End If
- Next
- Next
-
-
- Set rgAdd = SH.Range("A" & lngRows_Old + 1).Resize(lngRows_New, 4)
- rgAdd = arrAdd
-
- Set rgResult = SH.Range("K1")
-
- Set Conn = CreateObject("ADODB.Connection")
- Set Rst = CreateObject("ADODB.Recordset")
- strPath = ThisWorkbook.FullName
- Select Case Application.Version * 1
- Case Is <= 11
- strConn = "Provider=Microsoft.Jet.Oledb.4.0;Extended Properties=excel 8.0;Data source=" & strPath
- Case Is >= 12
- strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strPath & ";Extended Properties=""Excel 12.0;HDR=YES"";"""
- End Select
- Conn.Open strConn
- '''''''''''''''''''''''''''''''''
- strSQL = "TRANSFORM Count(一作业类型) AS 计数 " & _
- "SELECT Format([一录入时间],'yyyy/mm/dd') AS 日期, 飞机配件型号 " & _
- "FROM [" & strShName & "$B:D] " & _
- "GROUP BY Format([一录入时间],'yyyy/mm/dd'), 飞机配件型号 " & _
- "ORDER BY Format([一录入时间],'yyyy/mm/dd') " & _
- "PIVOT 一作业类型;"
- Rst.Open strSQL, Conn, 3, 1
-
- For lngCol = 0 To Rst.Fields.Count - 1
- strFieldsName = strFieldsName & "|" & Rst.Fields(lngCol).Name
- Next
-
- strSplit = Split(Mid(strFieldsName, 2), "|")
-
- SH.Range("K:W").ClearContents
-
- '标题
- rgResult.Resize(1, UBound(strSplit) + 1) = strSplit
- '内容
- rgResult.Offset(1, 0).CopyFromRecordset Rst
- Set Rst = Nothing
- Set Conn = Nothing
-
- rgAdd.ClearContents
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|