Which range is the active cell in?

bhsoundman

Board Regular
Joined
Jul 17, 2010
Messages
56
Office Version
  1. 365
Platform
  1. MacOS
Hi All,

I'm trying to determine which range my active cell is in. I have a working method, however I'm trying to avoid having to name a 100 ranges & have 100 cases in VBA.

For example if the active cell is within columns A:E, then A1 should be selected when the macro runs. If it's within columns F:J, then F1 should be selected. It would follow the same pattern for 100 possible range options. Every range option would be the same number of columns each time. & the cell that gets selected will always be in the same relative location (A1, F1 etc etc)

Do I just have to bite the bullet & start naming ranges & cases?

Thanks in advance for you expertise!
 
It's an interesting thread, for me anyway, but what do you want the result to be? If I remember right, originally it was to select the cell but in some of the follow ups I see that a value is the result.
I don't know if you have tried the two suggestions in Posts 16 and 18 and I don't even know if the results of these is what you actually want because there is no reference saying that it works or not, just some useful info. Anyway, neither of these have any looping.
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
After testing some more, your non-looping version is great! Thanks so much for your advice!
OK, thanks, but please see correction in Post #12.

There was an incorrect "+1" in the formula. To match your results, the code should be:

VBA Code:
Sub Test()

    Dim ColumnStart As Long, N As Long
    Dim currentshow As Variant
  
    ColumnStart = 4
    N = 32
  
    currentshow = Cells(1, ColumnStart + N * Int((ActiveCell.Column - ColumnStart) / N)).Value
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,225,606
Messages
6,185,957
Members
453,333
Latest member
BioCoder84

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