Vba 偏移结果中偏移单元格的总和
我需要将从Vba 偏移结果中偏移单元格的总和,vba,excel,Vba,Excel,我需要将从startRange.Offset(counter,0)=Application.WorksheetFunction.CountIf(countRange,Cities(city)) 代码的结果没有固定的位置,因为C列中列出的城市每天和每张表格中都不同 total的第一个结果来自代码: Public Sub count() Dim lastCell As String Range("C2").Select Selection.End(xlDown).Select lastCell
startRange.Offset(counter,0)=Application.WorksheetFunction.CountIf(countRange,Cities(city))
代码的结果没有固定的位置,因为C列中列出的城市每天和每张表格中都不同
total的第一个结果来自代码:
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
total的第二个结果来自代码:
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
我已经检查过了,但我不清楚。您可能需要添加一个函数来检查数组中是否存在计数范围内的值,如果存在,则对每个匹配,将1添加到您的总城市计数中 使用函数时,您可能会遇到以下情况:
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
您不会使用CitieCount=((countRange.Rows.count)-1)“新行来保存城市总数,因为您可能有两个或多个城市在您不想计数的范围内。我应该使用什么代码来计算以红色突出显示的结果之和?我同意不使用
citiesCount=((countRange.Rows.count)-1)
,因为即使城市不在我的数组列表中,它也会计数。请参阅我的答案,使用较大的城市编号,您可能希望将countRange读入数组,然后将该数组与其他数组(城市)进行比较每场比赛在花旗账户上加一个。会更快。