VBA: Return Row number of a time range based on value in another cell

RobOttman

New Member
Joined
Feb 15, 2017
Messages
1
I have been searching all day for answers and have made zero progress. I hope that you can help me!

I have one worksheet ("Schedule") and in column L there is the StartTime for all of our classes. On another worksheet("Times") I have a list of the standard start times for class periods. What I want to do is have a VBA macro start at the top of column L in Schedules, then find the row number (or, if it's easier, return a value from a neighboring column, which would just be the row number anyway) for the time time range where the "active cell" of column L falls. I would then have VBA put this value in column M.

So "Times" looks like this:
[TABLE="width: 81"]
<colgroup><col></colgroup><tbody>[TR]
[TD="align: right"]8:15 AM[/TD]
[/TR]
[TR]
[TD="align: right"]9:20 AM[/TD]
[/TR]
[TR]
[TD="align: right"]10:25 AM[/TD]
[/TR]
[TR]
[TD="align: right"]11:30 AM[/TD]
[/TR]
[TR]
[TD="align: right"]12:35 PM[/TD]
[/TR]
[TR]
[TD="align: right"]1:40 PM[/TD]
[/TR]
[TR]
[TD="align: right"]2:45 PM[/TD]
[/TR]
[TR]
[TD="align: right"]3:50 PM[/TD]
[/TR]
[TR]
[TD="align: right"]4:00 PM[/TD]
[/TR]
[TR]
[TD="align: right"]7:00 PM[/TD]
[/TR]
</tbody>[/TABLE]

Column L of "Schedule" contains times like 8:15 AM and 9:20 AM so that when I run the macro, column M would display "1" and "2" respectively (only 1 time per row! I'm not trying to get two periods out of one start time!). Similarly, if the start time is, say, 5:20 PM, the macro would return "9", since "5:20 PM" is between 4:00 PM and 7:00 PM.

I have tried using function, such as Match and VLookup, but those have not worked. I can't figure out how to use variables in VLookup, and for some reason Excel is unable to "MATCH" the times as exported from Access. (That is another problem entirely! If I export the query, the two times appear 100% identical, but will never match. If I copy the column from Access and paste it into Excel, they'll match as you would expect. I have no idea why...)

So any light you could shed on how to compare the value of one cell to a range of values in another worksheet via VBA would be greatly appreciated!
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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