我需要总结从startRange.Offset(counter, 0) = Application.WorksheetFunction.CountIf(countRange, Cities(city))
代码的结果没有固定的位置,因为C列中列出的城市每天和每张工作表都不同。
总计的第一个结果来自代码:
Public Sub count()
Dim lastCell As String
Range("C2").Select
Selection.End(xlDown).Select
lastCell = ActiveCell.Address
ActiveCell.Offset(1, 0).Select
ActiveCell.Value = "=counta(C2:" + lastCell + ")"
End Sub
总计的第二个结果来自代码:
citiesCount = countRange.Rows.Count
。
这段代码的问题在于,如果我的数组中没有列出某个城市,它仍然会计算它。
这是完整的代码。
Public Sub B1_Manual_CountLocations()
Dim wb As Workbook
Dim ws As Worksheet
Dim lastCell As String
Dim countRange As Range
count 'Call the function to count the total of cities
Set wb = ThisWorkbook
Set ws = wb.ActiveSheet 'Change as appropriate
Set countRange = ws.Range(Cells(2, "C"),Cells(ws.Range("C2").End(xlDown).Row, "C"))
Dim Cities()
Cities = Array("Armonk", "Bratislava", "Bangalore", "Hong Kong", "Mumbai", Zurich")
Dim city As Long
Dim counter As Long
Dim startRange As Range
Set startRange = ws.Cells(ws.Range("C2").End(xlDown).Row, "C").Offset(2, 0)
counter = 2
Dim citiesCount As Long
citiesCount = ((countRange.Rows.count) - 1) '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
startRange.Offset(counter + 1, 0) = "Total:" & citiesCount 'count the total number of city
End Sub
我已经检查了SUM of offset cells that contain a value,但这对我来说并不清楚。
答案 0 :(得分:1)
您可能需要添加一个函数来检查数组中是否存在计数范围中的值,如果是,那么每次匹配时都会将总数增加1。
使用Jimmy Pena之类的功能,你会得到以下内容:
Option Explicit
Public Sub B1_Manual_CountLocations()
Dim wb As Workbook
Dim ws As Worksheet
Dim countRange As Range
Dim cell As Range
Set wb = ThisWorkbook
Set ws = wb.ActiveSheet 'Change as appropriate
Set countRange = ws.Range(ws.Cells(2, "C"), ws.Cells(ws.Range("C2").End(xlDown).Row, "C")) 'explicit worksheet reference
Dim Cities()
Cities = Array("Armonk", "Bratislava", "Bangalore", "Hong Kong", "Mumbai", "Zurich")
Dim city As Long
Dim counter As Long
Dim startRange As Range
Dim citiesCount As Long
citiesCount = 0
For Each cell In countRange
If IsInArray(cell.Value, Cities) Then
citiesCount = citiesCount + 1
End If
Next cell
Set startRange = ws.Cells(ws.Range("C2").End(xlDown).Row, "C").Offset(2, 0)
counter = 2
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
startRange.Offset(counter + 1, 0) = "Total:" & citiesCount 'count the total number of city
End Sub
'https://stackoverflow.com/questions/11109832/how-to-find-if-an-array-contains-a-string
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function