有10,000多行包含供应商和供应商付款信息。 如果根据A,C,D,F,G,H列中的字段存在重复项,那么我想对K和L列中的金额求和,并为其保留1个条目/行。
结果是底部图片。
我想尽可能使用字典脚本,但我不知道如何使用它。它似乎有效,如果不是,不同的方法也很好。 谁能帮忙?????我非常感谢!!
答案 0 :(得分:1)
这个很有趣,所以我把它写出来了。本质上,此脚本会将您的wsFROM视为数据库表,然后它会将SQL语句应用于它以执行您需要的操作并将结果返回给wsTO。它应该运行得非常快,即使它是20000条记录。另一种方法是循环遍历工作表中的每一行,然后查看是否有其他人喜欢它并添加结果。这将是20分钟运行的噩梦VBA例程之一,这应该在几十秒内运行。
确保将SET wsFROM...
和Set wsTO...
行更改为您的工作表。除此之外,我认为它应该按原样使用您的数据。
Sub excelSQL()
'Variables
Dim objConn As Object
Dim rs As Object
Dim strFileName As String, strSQL As String, strConn As String
Dim wsFrom As Worksheet, wsTo As Worksheet
'set the worksheet with the data and the worksheet to dump the results
Set wsFrom = Worksheets("Sheet1")
Set wsTo = Worksheets("Sheet2")
'Make a new file with _tmp appended to it in same folder
strFileName = ThisWorkbook.Path & "/" & ThisWorkbook.Name
strFileName = Replace(strFileName, ".xls", "_tmp.xls")
ThisWorkbook.SaveCopyAs strFileName
'Fancy ADODB stuff.. essentially open up that copy we just saves as if it were a database
Set objConn = CreateObject("ADODB.Connection")
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFileName & ";Extended Properties=""Excel 12.0 Xml;HDR=YES;"""
objConn.Open strConn
'This is the sql string we will use to query the excel/database. The field names are the names in Row 1 of the sheet
strSQL = "SELECT [Number],[NAME],[Up/Down],[FYE],[GL_ACCT_GRP],[GL_SEG_CD],[ACCT#],[CODE],[REGION],Sum(Amount1), Sum(Amount2) FROM [" & wsFrom.Name & "$] GROUP BY [Number],[NAME],[Up/Down],[FYE],[GL_ACCT_GRP],[GL_SEG_CD],[ACCT#],[CODE],[REGION];"
'Fancy ADODB stuff to apply that SQL statement to the excel/database
Set rs = CreateObject("ADODB.Recordset")
rs.Open strSQL, objConn
'get headers
Dim header as object
Dim headerCol as integer
headerCol = 1
'Loop through fields in the recordset
For each header in rs.fields
'Stick them in Row 1
Cells(1, headerCol).value = header.name
'Next Column
headerCol = headerCol + 1
next header
'Copy the results of the sql statement, stored in the recordset, into the To sheet
wsTo.Range("A2").CopyFromRecordset rs
'close it up
rs.Close
objConn.Close
'Remove that tmp workbook
Kill strFileName
End Sub
答案 1 :(得分:1)
数据透视表会让你非常接近。使用旧式布局(“以表格形式显示”),您可以将A,C,D,F,G,H设置为行,然后将K和L相加。
稍后会复制粘贴值,并且您已经获得了骨架:您需要将值复制到它们下方的空白处以填充它(这可能是手动疼痛,或者在VBA中更容易提升至少)。
答案 2 :(得分:0)
从A& C& D& F& G& H创建一个密钥,对其进行排序并小计(总和)K& L表示密钥中的每次更改。