循环访问列的过滤标准并对可见数据执行分析以复制到新工作表 – VBAmacros

实际上,我试图完成的是按A列中给出的名称过滤数据库(全部在表1中),然后对现在过滤的可见数据执行各种数据分析,然后将该数据复制到新工作表在一个给定的细胞。

例如,在A列中按名称“Smith”过滤数据,然后让我们将列B中的所有可见数据相加,并将其打印到Sheet 2中的单元格C3。更高级的数据分析我相信我可以自己解决,只是想在这里滚动,我绝对是新的VBAmacros编码。 我已经使用Python创build了所有这些数据库。

最后一部分,将能够遍历列A中的所有过滤标准(我不会事先知道,并且可能在10-20个名字的任何地方)。

这里是我正在使用的代码(这里也可能有一些语法错误):

Option Explicit Sub Data() Dim playername As String Dim team As String Dim numFilters As Integer Dim hits As Integer Dim src As Worksheet Dim tgt As Worksheet Dim filterRange As Range Dim copyRange As Range Dim lastRow As Long Dim i As Integer team = ThisWorkbook.Sheets(1).Name numFilters = ActiveSheet.AutoFilter.Filters.Count ' I want this to capture the num of filter criteria for column A For i = 1 To numFilters playername = Sheets(team).Filter(i) ' This would be the filter criteria for the given iteration ActiveSheet.Range("$A$1:$AN$5000").AutoFilter field:=1, Criteria1:=playername ' Create new sheet with name of person Sheets.Add After:=ActiveSheet ActiveSheet.Select ActiveSheet.Name = playername Set tgt = ThisWorkbook.Sheets(i + 1) ' Perform data analysis (eg sum column B of filtered data) src.AutoFilterMode = False ' Find the last row with data in column A lastRow = src.Range("A" & src.Rows.Count).End(xlUp).Row ' The range that we are auto-filtering (all columns) Set filterRange = src.Range("A1:AN" & lastRow) ' Set the range to start in row 2 to prevent copying the header Set copyRange = src.Range("B2:B" & lastRow) ' Copy the sum of column B to our target cell on the sheet corresponding to this iteration Application.WorksheetFunction.Sum(copyRange.SpecialCells(xlCellTypeVisible)).Copy tgt.Range("A1") Next i End Sub 

这在Application.WorksheetFunction.Sum上当前失败,错误为“Invalid qualifier”。 感谢您的帮助,请让我知道是否需要澄清。