查找重叠计数

时间:2019-05-23 19:45:08

标签: excel

嗨,我有2个专栏(城市和会员),其中有5个独特的城市和100,000个会员。一些成员可能被分配到多个城市。我想要一个具有6列和6行城市的图形(带有附加的“未分配城市”列)。表中的值将是成员计数。所以我基本上是在计算重叠。我该怎么做?

我希望这就像将我的城市字段拖到数据透视表中的列和行中一样简单,但是我不能。

我希望它看起来像这样:

2 个答案:

答案 0 :(得分:0)

假设您需要条件格式:

选择要应用格式的完整范围。然后应用一个规则(公式选项),使=B$1=$A2(假设您已从B2选择到某个端点)。在列或行上使用固定(“ $”)条件有助于确定该条件分别适用于所选范围内的每个单元格。

enter image description here

答案 1 :(得分:0)

这是VBA解决方案。设置一个工作数组,其中每个不同的成员一行,每一个不同的城市一行,并在成员和城市重合的位置填充1。然后转移到输出数组,其中工作数组中的成对列已设置为1:

Option Explicit

Sub MultResponse()
Dim LastRow, LastColumn As Long
Dim sht1, sht2 As Worksheet
Dim workArray() As Integer
Dim cityDict As New Scripting.Dictionary
Dim memberDict As New Scripting.Dictionary
Dim city, member As String
Dim item As Integer
Dim i, j As Long
Dim memberNo As Long
Dim cityNo As Integer
Dim outputArray() As Integer
Dim outrow, outcol, rowTotal As Integer

Set sht1 = ThisWorkbook.Worksheets("Sheet1")
Set sht2 = ThisWorkbook.Worksheets("Sheet2")

LastRow = sht1.Cells(sht1.Rows.Count, "A").End(xlUp).Row



' Make list of distinct cities

j = 0
For i = 2 To LastRow
    city = sht1.Cells(i, 2)

    If city <> "" And Not cityDict.Exists(city) Then
        j = j + 1
        cityDict.Add Key:=city, item:=j
    End If

Next i


' Make list of distinct members

j = 0
For i = 2 To LastRow
    member = sht1.Cells(i, 1)
    If member <> "" And Not memberDict.Exists(member) Then
        j = j + 1
        memberDict.Add Key:=member, item:=j
    End If
Next i


' Set up and fill array with one row for each distinct member, one column for each distinct city


ReDim workArray(1 To memberDict.Count, 0 To cityDict.Count)


For i = 2 To LastRow
    member = sht1.Cells(i, 1)
    city = sht1.Cells(i, 2)

    If city <> "" And member <> "" Then
        memberNo = memberDict(member)
        cityNo = cityDict(city)
        workArray(memberNo, cityNo) = 1
    End If
Next i



' Fill output array where pairs of columns in work array are set to 1
' outputArray(0,0) is used for members with missing city


ReDim outputArray(0 To cityDict.Count, 0 To cityDict.Count)

'First do ones with no affiliation

For i = 1 To memberDict.Count
    rowTotal = 0
    For j = 1 To cityDict.Count
        rowTotal = rowTotal + workArray(i, j)
    Next j
    If rowTotal = 0 Then outputArray(0, 0) = outputArray(0, 0) + 1
Next i

' Then do ones with affiliation

For outrow = 1 To cityDict.Count
    For outcol = 1 To cityDict.Count
        For i = 1 To memberDict.Count
            If workArray(i, outrow) = 1 And workArray(i, outcol) = 1 _
                Then outputArray(outrow, outcol) = outputArray(outrow, outcol) + 1
        Next i
    Next outcol
Next outrow

' Transfer output array into sheet

For i = 0 To cityDict.Count
    For j = 0 To cityDict.Count
        sht2.Cells(i + 2, j + 2) = outputArray(i, j)
    Next j
Next i

'Insert row and column headers

sht2.Cells(1, 2) = "N/C"
sht2.Cells(2, 1) = "N/C"

For i = 0 To cityDict.Count - 1
    sht2.Cells(i + 3, 1) = cityDict.Keys(i)
Next i

For j = 0 To cityDict.Count - 1
    sht2.Cells(1, j + 3) = cityDict.Keys(j)
Next j


End Sub

测试数据

Firestore REST API documentation

结果

enter image description here