Count Columns in-between ranges

Modfish

New Member
Joined
Apr 22, 2009
Messages
9
Help.. I know its simple but annoying me

I'm trying to count the number of columns betwen to rangenames,. Each range name refers to a single cell.

e.g.

Sub test()

Application.Goto Reference:="Start_Outline"
Application.Goto Reference:="Start_TaskName"
int_col = 3

End Sub


i.e trying to make the variable int_col refer to the number of columns between Start_Outline Start_TaskName rather than a constant.

Many thanks
Modfish
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
This might be a bit of overkill:

Code:
Function ColDiff(rStartRng As Range, rEndRng As Range) As Integer
Dim iRng1Col As Integer
Dim iRng2Col As Integer
 
On Error GoTo CrashTrap
 
iRng2Col = rEndRng.Areas(1).Column
iRng1Col = rStartRng.Areas(1).Column
 
If iRng2Col = iRng1Col Then
   ColDiff = 0
Else
   If iRng2Col > iRng1Col Then
      ColDiff = iRng2Col - iRng1Col - 1
   Else
      ColDiff = -(iRng1Col - iRng2Col - 1)
   End If
End If
Exit Function
CrashTrap:
ColDiff = xlErrRef
End Function

...but, is that something you can work with?
 
Upvote 0
Each range name refers to a single cell.

Hi Modfish

In this case you can use:

Code:
int_col = Abs(Range("Start_Outline").Column - Range("Start_TaskName").Column)

Notice that a value equal to zero means they are in the same column and a value 1 means they are in contiguous columns.
 
Upvote 0
Nice one thanks for prompt replies.

Understand the long one but used the short version

Works a dream

Cheers
Modfish
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,487
Members
448,967
Latest member
visheshkotha

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