Q
總和和組VBA片
0
A
回答
1
SQL是理想的分組和求和數據。在這個例子中,我使用ADODB連接來分組和彙總數據。
Sub CreateConsolidatedTable()
Const adOpenKeyset = 1
Const adLockOptimistic = 3
Const WORKSHEETNAME As String = "Sheet1"
Const TABLENAME As String = "Table1"
Dim conn As Object, rs As Object
Dim tbl As ListObject
Dim Destination As Range
Set Destination = Worksheets.Add.Range("A1")
Set tbl = Worksheets(WORKSHEETNAME).ListObjects(TABLENAME)
Set conn = CreateObject("ADODB.Connection")
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.FullName & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";"
conn.Open
' On Error GoTo CloseConnection
Set rs = CreateObject("ADODB.Recordset")
With rs
.ActiveConnection = conn
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Source = getSQL(tbl)
.Open
With Destination
tbl.HeaderRowRange.Copy .Range("A1")
.Range("A2").CopyFromRecordset rs
.Parent.ListObjects.Add SourceType:=xlSrcRange, Source:=.Range("A1").CurrentRegion, XlListObjectHasHeaders:=xlYes, TableStyleName:=tbl.TableStyle
End With
End With
CloseRecordset:
rs.Close
Set rs = Nothing
CloseConnection:
conn.Close
Set conn = Nothing
End Sub
Function getSQL(tbl As ListObject) As String
Dim SQL As String, SheetName As String, RangeAddress As String
SQL = "SELECT DISTINCTROW [LastName], [FirstName], [Agent ID], Sum([Case Docs]) AS [Sum Of Case Docs], Sum([Call Count]) AS [Sum Of Call Count]" & _
" FROM [SheetName$RangeAddress]" & _
" GROUP BY [LastName], [FirstName], [Agent ID];"
SheetName = tbl.Parent.Name
RangeAddress = tbl.Range.Address(False, False)
SQL = Replace(SQL, "SheetName", SheetName)
SQL = Replace(SQL, "RangeAddress", RangeAddress)
getSQL = SQL
End Function
2
試試這個:
Sub Subroutine()
Dim currentrow As Integer
currentrow = 1
For i = 1 To 500
If Cells(currentrow, 8) = Cells(i, 2) Then
Cells(currentrow, 10) = Cells(currentrow, 10) + Cells(i, 4)
Cells(currentrow, 11) = Cells(currentrow, 11) + Cells(i, 5)
ElseIf IsNull(Cells(i, 2)) Or Cells(i, 2) = "" Then
Exit For
Else
currentrow = currentrow + 1
Cells(currentrow, 7) = Cells(i, 1)
Cells(currentrow, 8) = Cells(i, 2)
Cells(currentrow, 9) = Cells(i, 3)
Cells(currentrow, 10) = Cells(i, 4)
Cells(currentrow, 11) = Cells(i, 5)
End If
Next i
End Sub
你必須要調整單元格的座標,以配合您的細胞的座標。
1
子consolidateData()
昏暗lRow只要 昏暗ItemRow1,ItemRow2作爲字符串 昏暗lengthRow1,lengthRow2作爲字符串
lRow = 3
Do While (Cells(lRow, 1) <> "")
ItemRow1 = Cells(lRow, "A")
ItemRow2 = Cells(lRow + 1, "A")
lengthRow1 = Cells(lRow, "B")
lengthRow2 = Cells(lRow + 1, "B")
If ((ItemRow1 = ItemRow2) And (lengthRow1 = lengthRow2)) Then
Cells(lRow, "D") = Cells(lRow, "D") + Cells(lRow + 1, "D")
Cells(lRow, "E") = Cells(lRow, "E") + Cells(lRow + 1, "E")
Rows(lRow + 1).Delete
Else
lRow = lRow + 1
End If
Loop
結束子
相關問題
- 1. 總和列 - VBA
- 2. mongodb組和總和?
- 3. Excel VBA。總和複製
- 4. Vba總和低於標題
- 5. 總和組行
- 6. 總和跨組
- 7. Python的組和總和
- 8. 如何分組和總和?
- 9. Linq加入,組和總和
- 10. XSLT分組和總和
- 11. 做分組和總和SQL
- 12. PHP陣列組和總和
- 13. DAX分組和總和
- 14. 組和總和列表
- 15. 組和總和字段
- 16. XSLT 2組和總和值
- 17. SQL分組和總和
- 18. Matlab:數組求和總和
- 19. dplyr分組和總和
- 20. 數組和總和Iffs
- 21. 最大雙切片總和
- 22. 總和()由組SQL
- 23. 數組的總和
- 24. 組,分的總和,
- 25. 總和數組值
- 26. 數組列總和
- 27. 分組和彙總
- 28. 總和過濾組
- 29. 數組的總和
- 30. 組通過總和
是否有是VBA?數據透視表可以做你正在問的問題 – TJYen
VBA人可以給另一個人最好的建議:使用Excel宏記錄器。別客氣。 – nicomp