Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 59
Default Return Column Number

I've made a (not so) small UDF to return the column number, no matter how
the input is given, ie.:


Function RCN(ColumnAddres as variant) as long

If doesn't matter, if I give en column as...

RCN(3)
RCN("C")
RCN("C7")
RCN("C:C")
RCN("$C$11")

....in all cases it returns the number 3, which is the column number of the
address given.

But, I kind of think my UDF is somewhat long and takes too many lines for
such a relative simple task.

Could anyone guide me to a simple solution, for a small UDF to solve this
little "problem"?


TIA,



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Return Column Number

I would probably do something like this...

Public Function ColumnNumber(ByVal ColAddress As Variant) As Long
Dim rng As Range

On Error Resume Next
Set rng = Range(ColAddress)
Set rng = Columns(ColAddress)
ColumnNumber = rng(1).Column
End Function

--
HTH...

Jim Thomlinson


"Charlotte E" wrote:

I've made a (not so) small UDF to return the column number, no matter how
the input is given, ie.:


Function RCN(ColumnAddres as variant) as long

If doesn't matter, if I give en column as...

RCN(3)
RCN("C")
RCN("C7")
RCN("C:C")
RCN("$C$11")

....in all cases it returns the number 3, which is the column number of the
address given.

But, I kind of think my UDF is somewhat long and takes too many lines for
such a relative simple task.

Could anyone guide me to a simple solution, for a small UDF to solve this
little "problem"?


TIA,




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 59
Default Return Column Number

Wauw!

That one was just about 7 times as effective as mine: 5 lines to 36 lines
:-)

Thanks :-)))



Jim Thomlinson wrote:
I would probably do something like this...

Public Function ColumnNumber(ByVal ColAddress As Variant) As Long
Dim rng As Range

On Error Resume Next
Set rng = Range(ColAddress)
Set rng = Columns(ColAddress)
ColumnNumber = rng(1).Column
End Function


I've made a (not so) small UDF to return the column number, no
matter how the input is given, ie.:


Function RCN(ColumnAddres as variant) as long

If doesn't matter, if I give en column as...

RCN(3)
RCN("C")
RCN("C7")
RCN("C:C")
RCN("$C$11")

....in all cases it returns the number 3, which is the column number
of the address given.

But, I kind of think my UDF is somewhat long and takes too many
lines for such a relative simple task.

Could anyone guide me to a simple solution, for a small UDF to solve
this little "problem"?


TIA,



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 59
Default Return Column Number


Always post your code for comments and suggestions. ALWAYS!!



??? Why ?!?

As stated, I knew my code was not effective, so no need for posting it for
comments and telling me that...

All I asked for was hints to help me in the right direction - and Jim didn't
need to see my code to actually give me the full solution!

Now, that's what I call helping a fellow Excel nerd :-)

Thanks to Jim :-)))




I've made a (not so) small UDF to return the column number, no
matter how the input is given, ie.:


Function RCN(ColumnAddres as variant) as long

If doesn't matter, if I give en column as...

RCN(3)
RCN("C")
RCN("C7")
RCN("C:C")
RCN("$C$11")

...in all cases it returns the number 3, which is the column number
of the address given.

But, I kind of think my UDF is somewhat long and takes too many
lines for such a relative simple task.

Could anyone guide me to a simple solution, for a small UDF to solve
this little "problem"?


TIA,





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default Return Column Number

simpler

ColumnNumber = Range(ColAddress).Column


"Jim Thomlinson" wrote:

I would probably do something like this...

Public Function ColumnNumber(ByVal ColAddress As Variant) As Long
Dim rng As Range

On Error Resume Next
Set rng = Range(ColAddress)
Set rng = Columns(ColAddress)
ColumnNumber = rng(1).Column
End Function

--
HTH...

Jim Thomlinson


"Charlotte E" wrote:

I've made a (not so) small UDF to return the column number, no matter how
the input is given, ie.:


Function RCN(ColumnAddres as variant) as long

If doesn't matter, if I give en column as...

RCN(3)
RCN("C")
RCN("C7")
RCN("C:C")
RCN("$C$11")

....in all cases it returns the number 3, which is the column number of the
address given.

But, I kind of think my UDF is somewhat long and takes too many lines for
such a relative simple task.

Could anyone guide me to a simple solution, for a small UDF to solve this
little "problem"?


TIA,




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Return Column Number

It can actually be much shorter:

Public Function ColumnNumber(ByVal ColAddress As Variant) As Integer
ColumnNumber = Range(ColAddress).Cells(1, 1).Column
End Function

HTH,
Bernie
MS Excel MVP


"Charlotte E" <@ wrote in message ...
Wauw!

That one was just about 7 times as effective as mine: 5 lines to 36 lines :-)

Thanks :-)))



Jim Thomlinson wrote:
I would probably do something like this...

Public Function ColumnNumber(ByVal ColAddress As Variant) As Long
Dim rng As Range

On Error Resume Next
Set rng = Range(ColAddress)
Set rng = Columns(ColAddress)
ColumnNumber = rng(1).Column
End Function


I've made a (not so) small UDF to return the column number, no
matter how the input is given, ie.:


Function RCN(ColumnAddres as variant) as long

If doesn't matter, if I give en column as...

RCN(3)
RCN("C")
RCN("C7")
RCN("C:C")
RCN("$C$11")

....in all cases it returns the number 3, which is the column number
of the address given.

But, I kind of think my UDF is somewhat long and takes too many
lines for such a relative simple task.

Could anyone guide me to a simple solution, for a small UDF to solve
this little "problem"?


TIA,





  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default Return Column Number

nevermind, I see what I missed before clicking "Post"

"Charlie" wrote:

simpler

ColumnNumber = Range(ColAddress).Column


"Jim Thomlinson" wrote:

I would probably do something like this...

Public Function ColumnNumber(ByVal ColAddress As Variant) As Long
Dim rng As Range

On Error Resume Next
Set rng = Range(ColAddress)
Set rng = Columns(ColAddress)
ColumnNumber = rng(1).Column
End Function

--
HTH...

Jim Thomlinson


"Charlotte E" wrote:

I've made a (not so) small UDF to return the column number, no matter how
the input is given, ie.:


Function RCN(ColumnAddres as variant) as long

If doesn't matter, if I give en column as...

RCN(3)
RCN("C")
RCN("C7")
RCN("C:C")
RCN("$C$11")

....in all cases it returns the number 3, which is the column number of the
address given.

But, I kind of think my UDF is somewhat long and takes too many lines for
such a relative simple task.

Could anyone guide me to a simple solution, for a small UDF to solve this
little "problem"?


TIA,




  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default Return Column Number

I don't think Don was trying to critique your code. It's just that sometimes
people here can give you more ideas for future use! I have tons of notes
saved from years of visiting here.

"Charlotte E" wrote:


Always post your code for comments and suggestions. ALWAYS!!



??? Why ?!?

As stated, I knew my code was not effective, so no need for posting it for
comments and telling me that...

All I asked for was hints to help me in the right direction - and Jim didn't
need to see my code to actually give me the full solution!

Now, that's what I call helping a fellow Excel nerd :-)

Thanks to Jim :-)))




I've made a (not so) small UDF to return the column number, no
matter how the input is given, ie.:


Function RCN(ColumnAddres as variant) as long

If doesn't matter, if I give en column as...

RCN(3)
RCN("C")
RCN("C7")
RCN("C:C")
RCN("$C$11")

...in all cases it returns the number 3, which is the column number
of the address given.

But, I kind of think my UDF is somewhat long and takes too many
lines for such a relative simple task.

Could anyone guide me to a simple solution, for a small UDF to solve
this little "problem"?


TIA,




  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Return Column Number

Charlie,

That will error if the CalAddress has more than one cell...

HTH,
Bernie
MS Excel MVP

simpler

ColumnNumber = Range(ColAddress).Column





  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Return Column Number

And what happens to "C" and 3 when you do that since those are not valid
range addresses?
--
HTH...

Jim Thomlinson


"Charlie" wrote:

simpler

ColumnNumber = Range(ColAddress).Column


"Jim Thomlinson" wrote:

I would probably do something like this...

Public Function ColumnNumber(ByVal ColAddress As Variant) As Long
Dim rng As Range

On Error Resume Next
Set rng = Range(ColAddress)
Set rng = Columns(ColAddress)
ColumnNumber = rng(1).Column
End Function

--
HTH...

Jim Thomlinson


"Charlotte E" wrote:

I've made a (not so) small UDF to return the column number, no matter how
the input is given, ie.:


Function RCN(ColumnAddres as variant) as long

If doesn't matter, if I give en column as...

RCN(3)
RCN("C")
RCN("C7")
RCN("C:C")
RCN("$C$11")

....in all cases it returns the number 3, which is the column number of the
address given.

But, I kind of think my UDF is somewhat long and takes too many lines for
such a relative simple task.

Could anyone guide me to a simple solution, for a small UDF to solve this
little "problem"?


TIA,




  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Return Column Number

What Happens with "C" and 3 which are not valid range addresses? Mine goes
boom when I try that...
--
HTH...

Jim Thomlinson


"Bernie Deitrick" wrote:

It can actually be much shorter:

Public Function ColumnNumber(ByVal ColAddress As Variant) As Integer
ColumnNumber = Range(ColAddress).Cells(1, 1).Column
End Function

HTH,
Bernie
MS Excel MVP


"Charlotte E" <@ wrote in message ...
Wauw!

That one was just about 7 times as effective as mine: 5 lines to 36 lines :-)

Thanks :-)))



Jim Thomlinson wrote:
I would probably do something like this...

Public Function ColumnNumber(ByVal ColAddress As Variant) As Long
Dim rng As Range

On Error Resume Next
Set rng = Range(ColAddress)
Set rng = Columns(ColAddress)
ColumnNumber = rng(1).Column
End Function


I've made a (not so) small UDF to return the column number, no
matter how the input is given, ie.:


Function RCN(ColumnAddres as variant) as long

If doesn't matter, if I give en column as...

RCN(3)
RCN("C")
RCN("C7")
RCN("C:C")
RCN("$C$11")

....in all cases it returns the number 3, which is the column number
of the address given.

But, I kind of think my UDF is somewhat long and takes too many
lines for such a relative simple task.

Could anyone guide me to a simple solution, for a small UDF to solve
this little "problem"?


TIA,






  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Return Column Number

Always post your code for comments and suggestions. ALWAYS!!

??? Why ?!?


Because it allows us to see how you were trying to approach the problem and,
if possible, allows us to make comments and suggestions to help guide you in
your future programming endeavors. As things stand now, you have a solution
to your problem, but you don't know how efficient, usable or applicable your
approach to coding might be for future problems that you might try to write
code for. Now, it may be possible that we have no useful suggestions on your
coding or its style; but then again, we might be able to offer you some
insights that you might find useful. Exactly what did you have to lose by
copy/pasting your attempted code solution instead of your somewhat snippy
(my interpretation) "???Why?!?" response?

--
Rick (MVP - Excel)

  #15   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,049
Default Return Column Number

I agree. I've been using Excel since the very first version (198?) and today
I learnt that SPLIT could return one item !!! wow. We all can learn!

"Charlie" wrote in message
...
I don't think Don was trying to critique your code. It's just that
sometimes
people here can give you more ideas for future use! I have tons of notes
saved from years of visiting here.

"Charlotte E" wrote:


Always post your code for comments and suggestions. ALWAYS!!



??? Why ?!?

As stated, I knew my code was not effective, so no need for posting it
for
comments and telling me that...

All I asked for was hints to help me in the right direction - and Jim
didn't
need to see my code to actually give me the full solution!

Now, that's what I call helping a fellow Excel nerd :-)

Thanks to Jim :-)))




I've made a (not so) small UDF to return the column number, no
matter how the input is given, ie.:


Function RCN(ColumnAddres as variant) as long

If doesn't matter, if I give en column as...

RCN(3)
RCN("C")
RCN("C7")
RCN("C:C")
RCN("$C$11")

...in all cases it returns the number 3, which is the column number
of the address given.

But, I kind of think my UDF is somewhat long and takes too many
lines for such a relative simple task.

Could anyone guide me to a simple solution, for a small UDF to solve
this little "problem"?


TIA,






  #16   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Return Column Number

Another way, with a lot more flexibility than other replies:

Function ColNum(C As Variant) As Long
On Error GoTo ErrA:
If IsNumeric(C) Then
ColNum = Cells(1, CLng(C)).Column
Else
ColNum = Cells(1, C).Column
End If
Exit Function
ErrB:
On Error GoTo ErrC:
ColNum = Range(C, C)(1, 1).Column
Exit Function
ErrA:
Resume ErrB
ErrC:
End Function


It returns the column number of the first cell indicated by C. C can
be a number indicating a column; a string indicating a column number,
column letter, range specification, or defined name; or a Range
object. For example, all of the following will write a debug trace
value of 3:

Debug.Print ColNum(3)
Debug.Print ColNum("3")
Debug.Print ColNum("C")
Debug.Print ColNum("C:C")
Debug.Print ColNum("C1")
Debug.Print ColNum("C1:F10")
Debug.Print ColNum("TheCell")
Debug.Print ColNum(Range("C20"))

If C is not valid, the result is 0. E.g,

Debug.Print ColNum("asdf") ' returns 0


Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


On Wed, 27 May 2009 18:34:03 +0200, "Charlotte E" <@ wrote:

I've made a (not so) small UDF to return the column number, no matter how
the input is given, ie.:


Function RCN(ColumnAddres as variant) as long

If doesn't matter, if I give en column as...

RCN(3)
RCN("C")
RCN("C7")
RCN("C:C")
RCN("$C$11")

...in all cases it returns the number 3, which is the column number of the
address given.

But, I kind of think my UDF is somewhat long and takes too many lines for
such a relative simple task.

Could anyone guide me to a simple solution, for a small UDF to solve this
little "problem"?


TIA,


  #17   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 59
Default Return Column Number

Thanks, chip - another step up the learning curce :-)


Chip Pearson wrote:
Another way, with a lot more flexibility than other replies:

Function ColNum(C As Variant) As Long
On Error GoTo ErrA:
If IsNumeric(C) Then
ColNum = Cells(1, CLng(C)).Column
Else
ColNum = Cells(1, C).Column
End If
Exit Function
ErrB:
On Error GoTo ErrC:
ColNum = Range(C, C)(1, 1).Column
Exit Function
ErrA:
Resume ErrB
ErrC:
End Function


It returns the column number of the first cell indicated by C. C can
be a number indicating a column; a string indicating a column number,
column letter, range specification, or defined name; or a Range
object. For example, all of the following will write a debug trace
value of 3:

Debug.Print ColNum(3)
Debug.Print ColNum("3")
Debug.Print ColNum("C")
Debug.Print ColNum("C:C")
Debug.Print ColNum("C1")
Debug.Print ColNum("C1:F10")
Debug.Print ColNum("TheCell")
Debug.Print ColNum(Range("C20"))

If C is not valid, the result is 0. E.g,

Debug.Print ColNum("asdf") ' returns 0


Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


On Wed, 27 May 2009 18:34:03 +0200, "Charlotte E" <@ wrote:

I've made a (not so) small UDF to return the column number, no
matter how the input is given, ie.:


Function RCN(ColumnAddres as variant) as long

If doesn't matter, if I give en column as...

RCN(3)
RCN("C")
RCN("C7")
RCN("C:C")
RCN("$C$11")

...in all cases it returns the number 3, which is the column number
of the address given.

But, I kind of think my UDF is somewhat long and takes too many
lines for such a relative simple task.

Could anyone guide me to a simple solution, for a small UDF to solve
this little "problem"?


TIA,



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to return a column number? sycsummit Excel Programming 3 July 24th 08 11:35 PM
Return column number from column header text Roger[_3_] Excel Discussion (Misc queries) 4 February 14th 08 09:40 PM
How to return column number moonhk Excel Programming 4 November 7th 07 04:10 PM
To return the column number of a string [email protected] Excel Worksheet Functions 1 June 28th 07 07:28 PM
Excel - Return column alphabet from column number Sathyaish Excel Programming 2 April 7th 04 02:40 PM


All times are GMT +1. The time now is 08:12 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"