Hi All Excel/VBA experts,
Need your help on making a macro that counts the average count of a city from a column. Right below I have a macro that can count the number of a city from the given array. Need to put the average count of the city next to the name. Thank you for the help.
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
Tried this:
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)
Currently my code can CountIf the City highlighted in BLUE and shows the result below it highlighted in RED and Highlighted in Yellow. My objective is to add another data which is the percentage of the city highlighted in Green. I can do that manually by doing fo rexample =COUNTIF(V2:V25,"Bratislava")/COUNTA(V2:V5) . But as you could see on my arrays I need to type everything manually per city. Thank you for the expert help.
Application.WorksheetFunction.AverageIf(countRange, Cities(city), countRange.Offset(0,1))
if the values you want to average are in the column to the right of the City names. – Tim Williams