CountIF和AverageIF列

嗨,所有的Excel / VBA专家,

需要你的帮助,使一个macros从一个列计算一个城市的平均数。 在下面我有一个macros,可以从给定的数组中计算一个城市的数量。 需要把名字旁边的城市的平均数。 感谢您的帮助。

Public Sub CountA() Dim wb As Workbook Dim ws As Worksheet Dim lastCell As String Dim countRange As Range Set wb = ThisWorkbook Set ws = wb.ActiveSheet 'Change as appropriate Set countRange = ws.Range(Cells(2, "V"), Cells(ws.Range("V2").End(xlDown).Row, "V")) Debug.Print countRange.Address Dim Cities() Cities = Array("Auckland", "Brisbane", "Melbourne", "Seoul", "Tokyo", "Sydney", "Bratislava", "Bangalore", "Chennai", "Gurgaon", "Hyderabad", "Kolkata", "New Delhi", "Noida", "Mumbai", "London", "Munich", "Unterfohring", "Aachen", "Abidjan", "Abington", "Alpharetta", "Amstelveen", "Amsterdam", "Anaheim", "Aquascalientes", "Arlon", "Ashland", "Atlanta", "Aurora", "Austin", "Barcelona", "Basel", "Batavia", "Bay Village", "Belton", "Berkshire", "Berlin", "Birmingham", "Bogota", "Boise", "Boston", "Bramley", "Brandon", "Brecksville", "Brentwood", "Bridgetown", "Brussels", "Budapest", "Buffalo Grove", "Bury", "Cairo", "Callahan", "Calumet City", "Cape Town", "Capitola", "Cardiff", "Carmel", "Centennial", "Chanhassen", "Charlotte", "Cheltenham", "Cincinnati", "Clearwater", "Clemson", "Cleveland", "Cohoes", "Columbia", "Columbus", "Conifer", "Cookeville", "Copenhagen", "Coral Gables", "Croydon", "Culver City", "Cumming", "Cutchogue", "Dallas", "Dallas Park", "Darmstadt", "Double Oak", "Dublin") Dim city As Long Dim counter As Long Dim startRange As Range Set startRange = ws.Cells(ws.Range("V2").End(xlDown).Row, "V").Offset(2, 0) counter = 2 For city = LBound(Cities) To UBound(Cities) Debug.Print Cities(x) If Application.WorksheetFunction.CountIf(countRange, Cities(city)) > 0 Then startRange.Offset(counter, 0) = Application.WorksheetFunction.CountIf(countRange, Cities(city)) startRange.Offset(counter, 1) = Cities(city) counter = counter + 1 End If Next city End Sub 

试过这个:

 For city = LBound(Cities) To UBound(Cities) Debug.Print Cities(x) If Application.WorksheetFunction.AverageIf(countRange, Cities(city)) > 0 Then startRange.Offset(counter, 0) = Application.WorksheetFunction.AverageIf(countRange, Cities(city)) startRange.Offset(counter, 1) = Cities(city) 

目前,我的代码可以计数如果城市在蓝色突出显示,并显示下面的结果它突出显示红色和突出显示黄色。 我的目标是添加另一个数据,这是绿色城市的百分比。 我可以通过重做示例= COUNTIF(V2:V25,“Bratislava”)/ COUNTA(V2:V5)手动执行此操作。 但正如你可以看到我的arrays上,我需要每个城市手动input一切。 感谢您的专家帮助。

在这里输入图像说明

你需要添加几行。

获取城市总数:

 Dim citiesCount As Long citiesCount = countRange.Rows.Count 

写出每个城市总城市的比例:

 startRange.Offset(counter, -1) = Application.WorksheetFunction.CountIf(countRange, Cities(city)) / citiesCount 

我强烈build议你从工作表中使用Tim的build议来阅读城市,而不是全部input。

如果V栏中没有任何内容,我也会推荐error handling。

随着额外的线路,你会得到:

 Option Explicit Public Sub CountA() Dim wb As Workbook Dim ws As Worksheet Dim lastCell As String Dim countRange As Range Set wb = ThisWorkbook Set ws = wb.ActiveSheet 'Change as appropriate Set countRange = ws.Range(Cells(2, "V"), Cells(ws.Range("V2").End(xlDown).Row, "V")) Dim Cities() Cities = Array("Auckland", "Brisbane", "Melbourne", "Seoul", "Tokyo", "Sydney", "Bratislava", "Bangalore", "Chennai", "Gurgaon", "Hyderabad", "Kolkata", "New Delhi", "Noida", "Mumbai", "London", "Munich", "Unterfohring", "Aachen", "Abidjan", "Abington", "Alpharetta", "Amstelveen", "Amsterdam", "Anaheim", "Aquascalientes", "Arlon", "Ashland", "Atlanta", "Aurora", "Austin", "Barcelona", "Basel", "Batavia", "Bay Village", "Belton", "Berkshire", "Berlin", "Birmingham", "Bogota", "Boise", "Boston", "Bramley", "Brandon", "Brecksville", "Brentwood", "Bridgetown", "Brussels", "Budapest", "Buffalo Grove", "Bury", "Cairo", "Callahan", "Calumet City", "Cape Town", "Capitola", "Cardiff", "Carmel", "Centennial", "Chanhassen", "Charlotte", "Cheltenham", "Cincinnati", "Clearwater", "Clemson", "Cleveland", "Cohoes", "Columbia", "Columbus", "Conifer", "Cookeville", "Copenhagen", "Coral Gables", "Croydon", "Culver City", "Cumming", "Cutchogue", "Dallas", "Dallas Park", "Darmstadt", "Double Oak", "Dublin") Dim city As Long Dim counter As Long Dim startRange As Range Set startRange = ws.Cells(ws.Range("V2").End(xlDown).Row, "V").Offset(2, 0) counter = 2 Dim citiesCount As Long citiesCount = countRange.Rows.Count 'new line to hold total number of cities For city = LBound(Cities) To UBound(Cities) If Application.WorksheetFunction.CountIf(countRange, Cities(city)) > 0 Then startRange.Offset(counter, -1) = Application.WorksheetFunction.CountIf(countRange, Cities(city)) / citiesCount 'new line to calculate proportion of total startRange.Offset(counter, 0) = Application.WorksheetFunction.CountIf(countRange, Cities(city)) startRange.Offset(counter, 1) = Cities(city) counter = counter + 1 End If Next city End Sub 

这里是一个版本2,从名为CitiesList的工作表中读取城市列表,确保您在正确的工作表和一些空计数范围的error handling。

 Option Explicit Public Sub CountA() Dim wb As Workbook Dim ws As Worksheet ' Dim lastCell As String ''not used Dim countRange As Range Set wb = ThisWorkbook Set ws = wb.Worksheets("Sheet1") 'Change as appropriate Set countRange = ws.Range(ws.Cells(2, "V"), ws.Cells(ws.Range("V2").End(xlDown).Row, "V")) Dim Cities() Cities = GetCities 'Call function to populate array with cities from worksheet Dim city As Long Dim counter As Long Dim startRange As Range On Error Resume Next 'Error handling for range being empty. Might not be the best error handling. Set startRange = ws.Cells(ws.Range("V2").End(xlDown).Row, "V").Offset(2, 0) On Error GoTo 0 If startRange Is Nothing Then Exit Sub Else Resume End If counter = 2 Dim citiesCount As Long citiesCount = countRange.Rows.Count With ws 'make sure in right sheet For city = LBound(Cities, 1) To UBound(Cities, 1) If Application.WorksheetFunction.CountIf(countRange, Cities(city, 1)) > 0 Then startRange.Offset(counter, -1) = Application.WorksheetFunction.CountIf(countRange, Cities(city, 1)) / citiesCount startRange.Offset(counter, 0) = Application.WorksheetFunction.CountIf(countRange, Cities(city, 1)) startRange.Offset(counter, 1) = Cities(city, 1) counter = counter + 1 End If Next city End With End Sub Public Function GetCities() As Variant Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets("CitiesList") GetCities = ws.Range("B2", ws.Range("B2").End(xlDown)) ' Amend as appropriate End Function 

编译但未经testing:

 Public Sub CountA() Dim wb As Workbook Dim ws As Worksheet Dim lastCell As String Dim countRange As Range Set wb = ThisWorkbook Set ws = wb.ActiveSheet 'Change as appropriate Set countRange = ws.Range(Cells(2, "V"), Cells(ws.Range("V2").End(xlDown).Row, "V")) Debug.Print countRange.Address Dim Cities() '<TW> you should really load these from a worksheet.... Cities = Array("Auckland", "Brisbane", "Melbourne", "Seoul", "Tokyo", "Sydney", _ "Bratislava", "Bangalore", "Chennai", "Gurgaon", "Hyderabad") Dim city As Long Dim counter As Long Dim startRange As Range Dim r As Variant Set startRange = ws.Cells(ws.Range("V2").End(xlDown).Row, "V").Offset(2, 0) counter = 2 For city = LBound(Cities) To UBound(Cities) Debug.Print Cities(city) 'assuming the values to be averaged are in the column to the right of the city names ' adjust as required... r = Application.AverageIf(countRange, Cities(city), countRange.Offset(0, 1)) startRange.Offset(counter, 0).Resize(1, 2).Value = Array(r, Cities(city)) counter = counter + 1 Next city End Sub