count the number of columns before the active column

jgarland

Board Regular
Joined
Nov 6, 2005
Messages
50
Hi all.

I'm trying to find the correct excel VBA code to count the number of columns in a named range, from the start of the range up to and including the current active cell location and return that value to a specific cell.

Example:
"mynamedrange" is columns AA thru AZ and the current active cell is in column AF. The returned number should be 6.

The returned number will be variable as there will be future columns inserted, and the active cell location will vary. I've scoured the web and have not found anything usable.

Ideas & suggestions?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Something like this?

Code:
Set rng = Intersect(Range("mynamedrange"), ActiveCell)
If Not rng Is Nothing Then
    myVal = ActiveCell.Column - Range("mynamedrange").Column + 1
    MsgBox myVal
End If
 
Last edited:
Upvote 0
Solution
Works perfect! Thanks stf.

Can the code be altered to return the value no matter what row the active cell is in?

For instance:
"mynamedrange" is row 5 in columns AA thru AZ, and the current active cell is in AF10, or AF11, ect.
 
Upvote 0

Forum statistics

Threads
1,223,703
Messages
6,173,941
Members
452,539
Latest member
delvey

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