Vba 列的CountIF和AverageIF
大家好,Excel/VBA专家 需要您帮助制作一个宏,从一列中计算一个城市的平均数量。下面我有一个宏,可以从给定的数组中计算城市的数量。Vba 列的CountIF和AverageIF,vba,excel,countif,Vba,Excel,Countif,大家好,Excel/VBA专家 需要您帮助制作一个宏,从一列中计算一个城市的平均数量。下面我有一个宏,可以从给定的数组中计算城市的数量。 需要将城市的平均计数放在名称旁边。 谢谢你的帮助 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 'C
需要将城市的平均计数放在名称旁边。 谢谢你的帮助
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)
目前,我的代码可以计算城市是否以蓝色突出显示,下面的结果以红色突出显示,以黄色突出显示。我的目标是添加另一个数据,即以绿色突出显示的城市百分比。我可以通过执行fo-rexample=COUNTIFV2:V25、Bratislava/COUNTAV2:V5手动完成这项操作。但正如你在我的阵列上看到的,我需要按城市手动键入所有内容。感谢您的专家帮助
已编译但未测试:
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
您需要添加几行 获取城市总数:
Dim citiesCount As Long
citiesCount = countRange.Rows.Count
写出每个城市在城市总数中所占的比例:
startRange.Offset(counter, -1) = Application.WorksheetFunction.CountIf(countRange, Cities(city)) / citiesCount
我强烈建议你使用蒂姆的建议,从工作表中阅读城市,而不是把它们全部输入
我还建议在第五列中没有任何内容的情况下进行错误处理
使用您获得的其他行:
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的工作表中读取城市列表,确保您在正确的工作表中,并对空计数范围进行一些错误处理
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
似乎你知道你需要在这里使用AVERAGEIF-你试过了吗?这与您现有的代码非常相似…我确实将countif更改为averageif,但给了我运行时错误1004:如果worksheefFunction类失败,则无法获取averageif属性。通常最好发布失败的代码,并包括错误消息for city=LBoundCities To UBoundCities Debug。如果Application.WorksheetFunction.AverageIfcountRange,Citiescity>0,则打印Citiesx,然后开始传输.Offsetcounter,0=Application.WorksheetFunction.AverageIfcountRange,Citiescity开始传输.Offsetcounter,1=城市这是故障代码,以黄色高亮显示。错误消息是:运行时错误1004。如果工作表函数类无效,则无法获取averageif属性。是否注意到averageif比Countif多接受一个参数?你需要告诉它从哪里取平均值。因此,例如Application.WorksheetFunction.AverageIfcountRange、Citiescity、countRange.Offset0,1,如果要求平均值的值位于城市名称右侧的列中,则代码会运行,但会给出DIV/0!作为城市名称旁边的结果。在哪一列中是要为每个城市平均的值?从V2开始到具有valueCity的最后一个单元格。这与我在上面发布的CountIf宏相同。需要将城市的平均计数放在名称旁边-你到底想在这里做什么?平均多少?我假设有另一列包含需要求平均值的数字数据。如果不是这样的话,那么你是如何得到一个平均值的呢?我已经编辑了上面的问题,以便澄清。谢谢。太好了,它很有效!!非常感谢。我使用的是第1个版本。很抱歉在a$$中遇到麻烦,如果第V列中80行中的1行或2行为空,我将如何将其重命名为字符串?您的意思是,如果一个或两个城市名称在您希望输入的范围内丢失,例如未指定?您是否更改了公式?此行Set countRange=ws.Rangews.Cells2,V,ws.Cellsws.RangeV2.EndxlDown.Row,V表示要在第一个空格处对站点进行计数的范围,以便在所选范围内不会缺少任何城市。如果您以不同的方式定义范围,例如Set countRange=ws.Rangews.Cells2,V,ws.Cellsws.Cellsws.Rows.Count,V.EndxlUp.Row,V,那么您可能会。第二个版本将在第V列中找到最后使用的单元格,并包含空白单元格。您需要更改设置countRange的方式,因为它将在第一个空白处停止。你可以使用我上面建议的替代方案。设置countRange后,添加以下行:countRange.Replace What:=,Replacement:=未指定,LookAt:=xlPart ux,SearchOrder:=xlByRows