Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Increase letter by one

I have a string variable Col = "A"
I want to be able to use Col +1 as "B"
What's the correct code?

This must be simple??

Thanks,
--
Lee

www.raising-cain.com


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Increase letter by one

chr(Asc(col)+1)

demo'd from the immediate window:

col = "A"
? chr(Asc(col)+1)
B


or
a bit more robust:

col = "A"
? left(columns(col).Offset(0,1).Address(0,0),2 + (columns(col).column < 27))
B
col = "AA"
? left(columns(col).Offset(0,1).Address(0,0),2 + (columns(col).column < 27))
AB

--
Regards,
Tom Ogilvy


"Lee Cain" wrote in message
...
I have a string variable Col = "A"
I want to be able to use Col +1 as "B"
What's the correct code?

This must be simple??

Thanks,
--
Lee

www.raising-cain.com




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Increase letter by one

Whoops - slight error in the second formula - fails at Z.

Here is the adjustment:

col = "Z"
? left(columns(col).Offset(0,1).Address(0,0),2 + (columns(col).column+1 <
27))
AA


--
Regards,
Tom Ogilvy


"Tom Ogilvy" wrote in message
...
chr(Asc(col)+1)

demo'd from the immediate window:

col = "A"
? chr(Asc(col)+1)
B


or
a bit more robust:

col = "A"
? left(columns(col).Offset(0,1).Address(0,0),2 + (columns(col).column <

27))
B
col = "AA"
? left(columns(col).Offset(0,1).Address(0,0),2 + (columns(col).column <

27))
AB

--
Regards,
Tom Ogilvy


"Lee Cain" wrote in message
...
I have a string variable Col = "A"
I want to be able to use Col +1 as "B"
What's the correct code?

This must be simple??

Thanks,
--
Lee

www.raising-cain.com






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 620
Default Increase letter by one

Lee,

Try,

Col = Chr(Asc(Col)+1)

Won't work beyond Z of course. For that I suggest holding the letter index
in a long variable and incrementing this, generating the letter from it

If icol 26 Then
col = Chr(icol \ 26 + 64) & Chr(icol Mod 26 + 64)
Else
col = Chr(icol + 64)
End If


--

HTH

Bob Phillips

"Lee Cain" wrote in message
...
I have a string variable Col = "A"
I want to be able to use Col +1 as "B"
What's the correct code?

This must be simple??

Thanks,
--
Lee

www.raising-cain.com






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Increase letter by one

Here is Bob's excellent solution setup as a function:

Public Function Add1Col(sStr)
icol = Columns(sStr).Column + 1
If icol 26 Then
col = Chr(icol \ 26 + 64) & Chr(icol Mod 26 + 64)
Else
col = Chr(icol + 64)
End If
Add1Col = col
End Function


demo'd in the immediate window:

? add1col("AA")
AB
? add1col("Z")
AA

? add1col("B")
C


--
Regards,
Tom Ogilvy

"Bob Phillips" wrote in message
...
Lee,

Try,

Col = Chr(Asc(Col)+1)

Won't work beyond Z of course. For that I suggest holding the letter index
in a long variable and incrementing this, generating the letter from it

If icol 26 Then
col = Chr(icol \ 26 + 64) & Chr(icol Mod 26 + 64)
Else
col = Chr(icol + 64)
End If


--

HTH

Bob Phillips

"Lee Cain" wrote in message
...
I have a string variable Col = "A"
I want to be able to use Col +1 as "B"
What's the correct code?

This must be simple??

Thanks,
--
Lee

www.raising-cain.com










  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 620
Default Increase letter by one

Well that's funny, 'cos I liked yours, the
left(columns(col).Offset(0,1).Address(0,0),2 + (columns(col).column+1 <27))
version that is, using the worksheet to work through the letter series,
very neat.

--

HTH

Bob Phillips

"Tom Ogilvy" wrote in message
...
Here is Bob's excellent solution setup as a function:

Public Function Add1Col(sStr)
icol = Columns(sStr).Column + 1
If icol 26 Then
col = Chr(icol \ 26 + 64) & Chr(icol Mod 26 + 64)
Else
col = Chr(icol + 64)
End If
Add1Col = col
End Function


demo'd in the immediate window:

? add1col("AA")
AB
? add1col("Z")
AA

? add1col("B")
C


--
Regards,
Tom Ogilvy

"Bob Phillips" wrote in message
...
Lee,

Try,

Col = Chr(Asc(Col)+1)

Won't work beyond Z of course. For that I suggest holding the letter

index
in a long variable and incrementing this, generating the letter from it

If icol 26 Then
col = Chr(icol \ 26 + 64) & Chr(icol Mod 26 + 64)
Else
col = Chr(icol + 64)
End If


--

HTH

Bob Phillips

"Lee Cain" wrote in message
...
I have a string variable Col = "A"
I want to be able to use Col +1 as "B"
What's the correct code?

This must be simple??

Thanks,
--
Lee

www.raising-cain.com










  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Increase letter by one

Fabulous!
All I need is 1 & 2 beyond what ever my source letter is!
No fear of Z here.

Thanks!
--
Lee

www.raising-cain.com
"Bob Phillips" wrote in message
...
Lee,

Try,

Col = Chr(Asc(Col)+1)

Won't work beyond Z of course. For that I suggest holding the letter index
in a long variable and incrementing this, generating the letter from it

If icol 26 Then
col = Chr(icol \ 26 + 64) & Chr(icol Mod 26 + 64)
Else
col = Chr(icol + 64)
End If


--

HTH

Bob Phillips

"Lee Cain" wrote in message
...
I have a string variable Col = "A"
I want to be able to use Col +1 as "B"
What's the correct code?

This must be simple??

Thanks,
--
Lee

www.raising-cain.com








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
Need formula to increase Alpha character to next letter pwinters Excel Discussion (Misc queries) 5 April 2nd 23 07:07 PM
Number in cell increase with increase in font size. Value increases with increase in font.[_2_] Excel Discussion (Misc queries) 2 August 9th 07 01:58 PM
Fill Down In Rows But Want to Increase By Letter Karl Excel Discussion (Misc queries) 3 December 18th 06 07:01 AM
New Validation option to format 1st letter as Capital letter Jeff Excel Discussion (Misc queries) 5 July 13th 06 05:11 AM
column header changed from letter to number, how return to letter Ron Excel Discussion (Misc queries) 2 May 9th 05 08:34 PM


All times are GMT +1. The time now is 04:33 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"