Help counting values using VBA

ola_f

New Member
Joined
Oct 1, 2013
Messages
1
Hi i have xl file that contains a two tables of data. (see below) One table contains the names of some datahubs and the other contains sites that connects to the hubs.
Wat i would like to do is to Count the number of sites each datahub has and store that value so it could be reused for other calculations in the vba as showed below. There will never be more then 15 hubs but it could be like 200 sites connecting to diffrent hubs. I have tried to find some thing that will work for this but with out luck.

// OF

Hub
Hub1
Hub2
Hub3
Sites
Hub
Site 1
Hub 1
Site 2
Hub 1
Site 3
Hub 2
Site 4
Hub 3
Site 5
Hub 2
Site 6
Hub 3

<tbody>
</tbody>

Code:
Dim dDegreeInc As Double
Dim dRad As Double


For a = 0 To UBound(here should the data storing number of sites connected to a specific hub be placed)

dDegreeInc = 360 / a
dRad = (dDegreeInc * a) * PI / 180
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Maybe you can use a Dictionary to store the count for each Hub

Assuming your data in Sheet1 like below (adjust)


A
B
1
Sites​
Hub​
2
Site 1​
Hub 1​
3
Site 2​
Hub 1​
4
Site 3​
Hub 2​
5
Site 4​
Hub 3​
6
Site 5​
Hub 2​
7
Site 6​
Hub 3​
8
Site 7​
Hub 2​
9
Site 8​
Hub 2​
10
Site 9​
Hub 2​
11
Site 10​
Hub 2​

Try this

Code:
Sub CountSites()
    Dim lastRow As Long, rCell As Range
    Dim dict As Object, strHubName
    
    Set dict = CreateObject("Scripting.Dictionary")
    dict.comparemode = vbTextCompare
    
    With Sheets("Sheet1")
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
                
        For Each rCell In .Range("B2:B" & lastRow)
            If dict.exists(rCell.Value) Then
                dict.Item(rCell.Value) = dict.Item(rCell.Value) + 1
            Else
                dict.Add rCell.Value, 1
            End If
        Next rCell
    End With
    
    'Print some values for testing
    strHubName = "Hub 1"
    Debug.Print strHubName & " has " & dict.Item(strHubName) & " sites"
    strHubName = "Hub 2"
    Debug.Print strHubName & " has " & dict.Item(strHubName) & " sites"
    
End Sub

Hope this helps

M.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,370
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top