1

I am stuck with developing a formula that would count total ride number, which start in one neighborhood and end in another. There are 83 neighborhoods in total to have data counted for, but each of them is split into several zones, where each of them has unique public number attached to them. For example, "Neighborhood A" consists of zones 5431, 5432, 5434 and 5435; then "Neighborhood B" consists of zones 2312, 2333, 2385; "Neighborhood C" consists of zones 1678, 2343, 3761 and so on. One important aspect to remember here is that there is no specific number of zones for a neighborhood. The number varies, but it's value belongs to a single range (further I will refer to it as a Start_range or Finish_range).

The output result I require is a table, that has these columns: Table example

The zones for every neighborhood are specified on a separate worksheet in the same workbook. The specification table looks like this: Zone arrangement table example

What is the biggest concern in this whole situation is how can I set Excel understand, that the string value (e.g. "Neighborhood A") that it reads from cells in the first column and in the forth column is referred to the array of zones, and also that the array of zone values means that it needs to count the total number of rides between all the zones of both neighborhoods. To make it absolutely clear, I mean that:

Neighborhood A -> Neighborhood C means count total number from rides

2435 -> 2066

2435 -> 3044

2435 -> 2043

2162 -> 2066

2162 -> 3044

2162 -> 2043

(see picture "Zone arrangement table example" for reference).

All the zones are listed in a separate worksheet in a different workbook. The lookup range is one column and 62579 rows. Each row contains a number of a specific zone. There are 2 columns like this - one for Start zone (Start_zone_range) and another for Finish zone (Finish_zone_range).

There are 2 additional arguments in the statement - transport mode and day. They do not cause any trouble, just need to be noted for understanding the statement. The ranges for them are listed in the same workbook and worksheet as the ranges for zones (Transport_mode_range and Day_range). Both of these ranges only contain string values. Each range is one column and 62579 rows (equal to every other argument).

My current formula statement returns 0, but is set like this:

=COUNTIFS('[Data_workbook]Data_worksheet'!$AS$2:$AS$62579 (Start_zone_range);A2 (1,1 cell in the output table (see "Table example");'[Data_workbook]Data_worksheet'!$AT$2:$AT$62579 (Finish_zone_range);D2 (1,4 cell in the output table (see "Table example");'[Data_workbook]Data_worksheet'!$T$2:$T$62579 (Transport_mode_range);"Auto" (Transport_criteria);'[Data_workbook]Data_worksheet'!$M$2:$M$62579 (Day_range);"Tuesday" (day_criteria).

I tried solving it through VBA. After parsing several similar threads, I found some examples to do, but I really am not experienced in VBA. It didn't work out, but I will explain, which steps have I taken. So I started with trying to establish in public sub that range "Neighborhood A" is equal to Range of values from the worksheet, that has zones specified for each neighborhood ("Legend"). It went like this:

Public Sub define_ranges()

Dim Range("Neighborhood A") As Variant
"Neighborhood A" = Array(Worksheets("Legend").Range("$B$1:$B$2"))

Dim Range("Neighborhood B") As Variant
"Neighborhood B" = Array(Worksheets("Legend").Range("$B$3:$B$17"))

Dim Range("Neighborhood C") As Variant
"Neighborhood C" = Array(Worksheets("Legend").Range("$B$18:$B$34"))

Dim ("Neighborhood D") As Variant
"Neighborhood D" = Array(Worksheets("Legend").Range("$B$35"))

And so on...

End Sub

Then, after defining ranges for all the 83 neighborhoods, I went on to define a countifs() loop cycle, which I spied from another thread here.

It went on like this:

Sub do_countifs_loop()

Dim lastRow As Long
Dim i As Double
Dim WS As Excel.Worksheet
Dim lRow As Long

Dim StartValuesRange As Range
Dim FinishValuesRange As Range
Dim ResultRange As Range
Dim StartCriteriaValue As String
Dim FinishCriteriaValue As String
Dim DayCriteriaValue As String
Dim DayCriteriaValueRange As Range
Dim TransportCriteriaValue As String
Dim TransportCriteriaValueRange As Range


DayCriteriaValue = "Tuesday"
TransportCriteriaValue = "Auto"

Set WS = ActiveWorkbook.Sheets("Auto rides on Tuesday")

lastRow = WS.Cells(WS.Rows.Count, "A").End(xlUp).Row

Set StartValuesRange = 
Range(Workbooks("Data_workbook").Worksheets("Data_worksheet").Range("$AS$2:$AS$62579"))

Set FinishValuesRange = 
Range(Workbooks("Data_workbook").Worksheets("Data_worksheet").Range("$AT$2:$AT$62579"))

Set DayCriteriaValueRange = 
Range(Workbooks("Data_workbook").Worksheets("Data_worksheet").Range("$M$2:$M$62579"))

Set TransportCriteriaValueRange = 
Range(Workbooks("Data_workbook").Worksheets("Data_worksheet").Range("$T$2:$T$62579"))

lRow = 2
WS.Activate

Do While lRow <= lastRow

StartCriteriaValue = WS.Range("A" & lRow)
FinishCriteriaValue = WS.Range("D" & lRow)
i = Application.WorksheetFunction.Countifs(StartValuesRange, StartCriteriaValue, FinishValuesRange, FinishCriteriaValue, DayCriteriaValueRange, DayCriteriaValue, TransportCriteriaValueRange, TransportCriteriaValue1, TransportCriteriaValueRange, TransportCriteriaValue2)
WS.Range("B" & lRow).Value = i

lRow = lRow + 1
Loop

End Sub

But yeah, didn't work out as well, unfortunately. Received both some code issue errors, fixed them somehow, but then received Error 1004 "Application-defined or Object-defined error".

So, here I am, trying to figure out, what is going wrong. I will be highly thankful for any input, tip and piece of advice, as I am having hard time figuring this out for several sessions already.

N.B.1.: I am also capable of little Python work, so if there is a more straightforward way that you know that might be done via Python, please let me know.

N.B.2.: Sorry for a long read. It is my first thread here, so I am yet to figure out whether explaining needs more or less details.

  • `Range(Workbooks("Data_workbook").Worksheets("Data_worksheet").Range("$AS$2:$AS$62579"))` just needs to be `Workbooks("Data_workbook").Worksheets("Data_worksheet").Range("$AS$2:$AS$62579")` I think you are looking for 2 criteria on the same "criteria range" with TransportCriteriaValueRange, not sure, but i think you'll need to split them as i dont think it behaves like an OR – Nathan_Sav Aug 13 '21 at 14:47
  • Welcome to StackOverflow @Mihails! To get an idea of the proper amount of information you can read the help doc about [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – HackSlash Aug 13 '21 at 15:20
  • This is not too complex, but difficult to work on without some sample data. Can you share a workbook with some example output and the neighborhood lookup table? – Tim Williams Aug 14 '21 at 01:03
  • @Nathan_Sav thank you very much, good point. I will try setting up an AND OR structure – Mihails Potihonins Aug 16 '21 at 05:54
  • @TimWilliams I do agree that it is not complex, just the issue is that I struggle to make Excel recognize string value as a reference to data range. I can share the workbook sample, surely, just please tell me how to do it :) – Mihails Potihonins Aug 16 '21 at 05:57
  • You can upload it to dropbox/google docs/onedrive etc and share a link here. – Tim Williams Aug 16 '21 at 06:17
  • That link is not accessible... – Tim Williams Aug 16 '21 at 15:50
  • @TimWilliams My apologies. Seems that something went off in the process, as I doublechecked that I made it accessible to people with the link, but it wasn't set somehow. Now it is. Please use this link: https://drive.google.com/drive/folders/10VLk-10HKBxP6TBKWmq2Fk_6d52D8ME6?usp=sharing – Mihails Potihonins Aug 17 '21 at 06:07
  • Whenever I try to save a local copy of these files Excel ends up corrupting the file. Maybe a smaller example with all of the unneeded columns removed from the dataset andfewer rows in the raw data? – Tim Williams Aug 17 '21 at 06:52
  • @TimWilliams Sorry about that. Sure, let me recompose a file. I will make another copy in the nearest 30-40 minutes – Mihails Potihonins Aug 17 '21 at 09:31
  • @TimWilliams Here is the newer version https://drive.google.com/drive/folders/10VLk-10HKBxP6TBKWmq2Fk_6d52D8ME6?usp=sharing – Mihails Potihonins Aug 17 '21 at 09:45
  • I'm at work but can take a look later ... – Tim Williams Aug 17 '21 at 15:38
  • See if this gives you any ideas - it's all VBA not formulas but it may help you. https://drive.google.com/file/d/1LdjqEwcUIlNhMDyK2RB2uBn06_GHtKb8/view?usp=sharing I would not go with the tabular approach you had of listing the neighborhood combinations - that's too many rows to scroll through... Example file has a more-compact format. – Tim Williams Aug 18 '21 at 05:35
  • @TimWilliams this is *absolutely amazing*, thank you for such a tremendous input. The reason I decided to go for a tabular approach is that I was requested to do it that way, as my colleague will later use the data to apply information for vectors. I will go through your solution step by step to learn the approach. Once again, thank you very much. – Mihails Potihonins Aug 18 '21 at 08:11
  • @TimWilliams seems that I still need your advise on this. The macro works perfectly in the worksheet you have developed, but, unfortunately, if I try to copy the module to another workbook, it doesn't run (no specific error, just doesn't launch the macro), even though I replace all the lookup ranges and worksheet names to match in the workbook. :( – Mihails Potihonins Aug 18 '21 at 13:46
  • I can't see your modified workbook, so it's difficult for me to know why it won't run. Have you tried stepping through it? – Tim Williams Aug 18 '21 at 15:27
  • @TimWilliams Hello again. Sorry for taking a long time to reply. It's all good now, as I forgot that I need to run a Debug.Print for the first time to create the report form. Thank you once again for your time and input, it cannot get any more helpful! – Mihails Potihonins Aug 23 '21 at 07:00
  • Good to hear you figured it out. – Tim Williams Aug 23 '21 at 07:08

0 Answers0