VBA Counting instances of journeys

Ronanm

Board Regular
Joined
Nov 13, 2010
Messages
107
Hi

I need to use VBA to count how many times a journey occurs in each Quarter. I have a table which shows the Journeys to be counted (in either direction) D:E, along with the various journeys per Quarter in column A.

So in the example below. the journey London to Cardiff (either direction) appears 2 times in Column A (Qtr2 and Qtr4), Cardiff to Devon (either direction) appears 3 times etc...

tinypic.com
[/URL][/IMG]

Thanks
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hello,

This gives the results you have provided.

Code:
Sub COUNT_JOURNEY()
    For MY_JOURNEY = 2 To Range("D" & Rows.Count).End(xlUp).Row
        MY_A = Range("D" & MY_JOURNEY).Value
        MY_B = Range("E" & MY_JOURNEY).Value
        For MY_DATA = 2 To Range("A" & Rows.Count).End(xlUp).Row
            For MY_NEXT_DATA = MY_DATA + 1 To Range("A" & Rows.Count).End(xlUp).Row
                If Left(Range("A" & MY_NEXT_DATA).Value, 3) = "Qtr" Then GoTo MY_CONT
                If Range("A" & MY_DATA).Value = MY_A Or Range("A" & MY_DATA).Value = MY_B Then
                    If Range("A" & MY_NEXT_DATA).Value = MY_A Or Range("A" & MY_NEXT_DATA).Value = MY_B Then
                        MY_COUNT = MY_COUNT + 1
                    End If
                End If
            Next MY_NEXT_DATA
MY_CONT:
        Next MY_DATA
            Range("F" & MY_JOURNEY).Value = MY_COUNT
            MY_COUNT = 0
    Next MY_JOURNEY
End Sub

Does it work for all journeys?
 
Upvote 0
That works a treat (Nearly got caught out by "my" mis-spelling of one of the "Cardiff"s!), thank you so much :)
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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