我想运行一个循环来按特定条件对文本进行分组并累加总数,但我需要能够抓取两位数据。
Connecticut 624
Georgia 818
Washington 10
Arkansas 60
New Jersey 118
Ohio 2,797
选择将是这样的,我希望用户能够突出显示两列,我将按位置运行并按州分组并添加总数
编辑 -
到目前为止,我所能做的就是抓住用户选择的范围:
Sub Short()
Dim rngMyRange As Range
If TypeName(Selection) = "Range" Then
Set rngMyRange = Selection
Else
Exit Sub 'Non-range type selection e.g. a chart
End If
MsgBox "The address of rngMyRange is " & rngMyRange.Address(False, False)
End Sub
我在另一个工作表中有一个公式,用于检查其组的状态,但它一次只有一个单元格
=IF( AND(D9="", B9="USA"),"",IF(B9="USA",IF(COUNTIF(Legends!$B$4:$B$13,D9)>0,"US Group 1",IF(COUNTIF(Legends!$F$4:$F$15,D9)>0,"US Group 3","US Group 2")),IF(COUNTIF(Legends!$H$4:$H$6,B9)>0,"Int'l Tier 1",IF(COUNTIF(Legends!$J$4:$J$15,B9)>0,"Int'l Tier 2","Int'l Tier 3"))))
答案 0 :(得分:1)
使用下面的for循环将遍历您范围内的每个单元格(选择 - 确保您只选择其中包含名称的列)
Sub Short()
Dim rngMyRange As Range
Dim ttl as integer
Dim c
If TypeName(Selection) = "Range" Then
Set rngMyRange = Selection
Else
Exit Sub 'Non-range type selection e.g. a chart
End If
for each c in rngMyRange
ttl = ttl + c.offset(0,1)
next c
End Sub
您需要使用您的条件更改for循环。通过在数据范围上使用数据透视表,可以更轻松地完成此操作。