来自偏移结果的偏移单元的和

我需要从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,但是这对我并不清楚。

您可能需要添加一个函数来检查数组中是否存在计数范围内的值,如果是,则每个匹配都会将总数加1。

使用吉米·佩纳的function,你会有如下的东西:

 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