|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub BOB() 'SQL法
- Dim shResult As Worksheet
- Dim arrResult As Variant, arrSource As Variant, lngRow As Long, lngRowID As Long
- Dim objDic As Object, strKey As String
- Dim Conn As Object, Rst As Object, strPath As String
- Dim strConn As String, strSQL As String
- Set shResult = Sheets("B1")
- lngRow = shResult.Range("B" & Rows.Count).End(xlUp).Row
- arrResult = shResult.Range("B1:L" & lngRow)
-
- Set objDic = CreateObject("Scripting.Dictionary")
- For lngRow = 2 To UBound(arrResult)
- strKey = arrResult(lngRow, 1)
- If strKey <> "" Then objDic(strKey) = lngRow
- Next
-
- Set Conn = CreateObject("ADODB.Connection")
- Set Rst = CreateObject("ADODB.Recordset")
- strPath = "F:\Temp\666\BX01.xlsx" '这是BX01所在的路径及文件名!
- 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 = "SELECT 产品编号, Count(产品编号) AS 次数, Sum(数量) AS 到料总数, Sum([投产累计数]) AS 生产累计,Sum([X1累计废品1]) AS 废品1累计, Sum([X1累计废品2]) AS 废品2累计,Sum([X1累计废品3]) AS 废品3累计, Sum([X1累计成品数量]) AS 成品数1累计 FROM [BX1$] WHERE 产品编号 <>'' GROUP BY 产品编号;"
- Rst.Open strSQL, Conn, 3, 1
- arrSource = Rst.getrows
-
- Rst.Close: Set Rst = Nothing
- Conn.Close: Set Conn = Nothing
-
- For lngRow = LBound(arrSource, 2) To UBound(arrSource, 2)
- strKey = arrSource(0, lngRow)
- If objDic.Exists(strKey) Then
- lngRowID = objDic(strKey)
- arrResult(lngRowID, 5) = arrSource(1, lngRow)
- arrResult(lngRowID, 6) = arrSource(2, lngRow)
- arrResult(lngRowID, 7) = arrSource(3, lngRow)
- arrResult(lngRowID, 8) = arrSource(4, lngRow)
- arrResult(lngRowID, 9) = arrSource(5, lngRow)
- arrResult(lngRowID, 10) = arrSource(6, lngRow)
- arrResult(lngRowID, 11) = arrSource(7, lngRow)
- End If
- Next
-
- shResult.Range("B1").Resize(UBound(arrResult), UBound(arrResult, 2)) = arrResult
-
- MsgBox "OK!"
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|