Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Converting Numeric Columns to Excel Alpha Notation

Hi gurus!

I need to use column numbers in a loop and convert them to the column
letters for populating cells.

I initially wrote a conversion function but found it failing in transition
from Z to AA and AZ to BA. So I scoured the net and got a couple of
conversion functions...all of which failed including this one from Microsoft
Knowledge base...

Private Sub column()
Dim col As Integer
For col = 1 To 55
Debug.Print Str(col) & " " & ConvertToLetter(col)
Next col
End Sub

Function ConvertToLetter(iCol As Integer) As String
' FROM MICROSOFT
Dim iAlpha As Integer
Dim iRemainder As Integer
iAlpha = Int(iCol / 27)
iRemainder = iCol - (iAlpha * 26)
If iAlpha 0 Then
ConvertToLetter = Chr(iAlpha + 64)
End If
If iRemainder 0 Then
ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
End If
End Function

It correctly converts but fails around the transition points:-

52 AZ
53 A[
54 BB
..
78 BZ
79 B[
80 B\
81 CC

As you can see it corrects itself as 54 is indeed BB and 81 is CC and as can
be seen it progressively gets worse...
Anyone any ideas on a foolproof conversion? I have to count up to 256 (IV)
cheers
Jeff


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Converting Numeric Columns to Excel Alpha Notation


Change one line in your function iAlpha = Int(iCol / 27) to
iAlpha=Int((iCol-1)/26)

Veerkar


jeff Wrote:
Hi gurus!

I need to use column numbers in a loop and convert them to the column
letters for populating cells.

I initially wrote a conversion function but found it failing in
transition
from Z to AA and AZ to BA. So I scoured the net and got a couple of
conversion functions...all of which failed including this one from
Microsoft
Knowledge base...

Private Sub column()
Dim col As Integer
For col = 1 To 55
Debug.Print Str(col) & " " & ConvertToLetter(col)
Next col
End Sub

Function ConvertToLetter(iCol As Integer) As String
' FROM MICROSOFT
Dim iAlpha As Integer
Dim iRemainder As Integer
iAlpha = Int(iCol / 27)
iRemainder = iCol - (iAlpha * 26)
If iAlpha 0 Then
ConvertToLetter = Chr(iAlpha + 64)
End If
If iRemainder 0 Then
ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
End If
End Function

It correctly converts but fails around the transition points:-

52 AZ
53 A[
54 BB
..
78 BZ
79 B[
80 B\
81 CC

As you can see it corrects itself as 54 is indeed BB and 81 is CC and
as can
be seen it progressively gets worse...
Anyone any ideas on a foolproof conversion? I have to count up to 256
(IV)
cheers
Jeff



--
avveerkar
------------------------------------------------------------------------
avveerkar's Profile: http://www.excelforum.com/member.php...o&userid=30338
View this thread: http://www.excelforum.com/showthread...hreadid=527894

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Converting Numeric Columns to Excel Alpha Notation

Excel is pretty flexible. You can use numbers instead of letter in expressions
like:

activesheet.cells(1,"A").value = "hi there"
or
activesheet.cells(1,1).value = "hi there"

So you may not need to do the conversion at all.

But if you must, this worked ok for me:

Option Explicit
Private Sub column()
Dim col As Integer
For col = 1 To 55
Debug.Print Str(col) & " " & ConvertToLetter(col)
Next col
End Sub
Function ConvertToLetter(iCol As Integer) As String
Dim myStr As String
myStr = Worksheets(1).Cells(1, iCol).Address(False, False)
myStr = Application.Substitute(myStr, "1", "")

ConvertToLetter = myStr
End Function


jeff wrote:

Hi gurus!

I need to use column numbers in a loop and convert them to the column
letters for populating cells.

I initially wrote a conversion function but found it failing in transition
from Z to AA and AZ to BA. So I scoured the net and got a couple of
conversion functions...all of which failed including this one from Microsoft
Knowledge base...

Private Sub column()
Dim col As Integer
For col = 1 To 55
Debug.Print Str(col) & " " & ConvertToLetter(col)
Next col
End Sub

Function ConvertToLetter(iCol As Integer) As String
' FROM MICROSOFT
Dim iAlpha As Integer
Dim iRemainder As Integer
iAlpha = Int(iCol / 27)
iRemainder = iCol - (iAlpha * 26)
If iAlpha 0 Then
ConvertToLetter = Chr(iAlpha + 64)
End If
If iRemainder 0 Then
ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
End If
End Function

It correctly converts but fails around the transition points:-

52 AZ
53 A[
54 BB
.
78 BZ
79 B[
80 B\
81 CC

As you can see it corrects itself as 54 is indeed BB and 81 is CC and as can
be seen it progressively gets worse...
Anyone any ideas on a foolproof conversion? I have to count up to 256 (IV)
cheers
Jeff


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Converting Numeric Columns to Excel Alpha Notation

Thanks Dave

But I needed to let you know where I was using it, see following code
snippet...

'set previous and current columns and calculate Excel Column
Alphas
lintPrevCol = lintCurCol
lintCurCol = lintCurCol + (Kount - lintConCol - 1)
lstrStartCol = ConvertToLetter(lintPrevCol + 1)
lstrEndCol = ConvertToLetter(lintCurCol)

'output Connection Headings
lstrStartCell = lstrStartCol & "1"
lstrEndCell = lstrEndCol & "1"
oSheet.Range(lstrStartCell, lstrEndCell) = lstraFields

'output Connection Data
lstrStartCell = lstrStartCol & lstrRow
lstrEndCell = lstrEndCol & lstrRow
oSheet.Range(lstrStartCell, lstrEndCell) = lstraData

cheers
jeff

"Dave Peterson" wrote in message
...
Excel is pretty flexible. You can use numbers instead of letter in
expressions
like:

activesheet.cells(1,"A").value = "hi there"
or
activesheet.cells(1,1).value = "hi there"

So you may not need to do the conversion at all.

But if you must, this worked ok for me:

Option Explicit
Private Sub column()
Dim col As Integer
For col = 1 To 55
Debug.Print Str(col) & " " & ConvertToLetter(col)
Next col
End Sub
Function ConvertToLetter(iCol As Integer) As String
Dim myStr As String
myStr = Worksheets(1).Cells(1, iCol).Address(False, False)
myStr = Application.Substitute(myStr, "1", "")

ConvertToLetter = myStr
End Function


jeff wrote:

Hi gurus!

I need to use column numbers in a loop and convert them to the column
letters for populating cells.

I initially wrote a conversion function but found it failing in
transition
from Z to AA and AZ to BA. So I scoured the net and got a couple of
conversion functions...all of which failed including this one from
Microsoft
Knowledge base...

Private Sub column()
Dim col As Integer
For col = 1 To 55
Debug.Print Str(col) & " " & ConvertToLetter(col)
Next col
End Sub

Function ConvertToLetter(iCol As Integer) As String
' FROM MICROSOFT
Dim iAlpha As Integer
Dim iRemainder As Integer
iAlpha = Int(iCol / 27)
iRemainder = iCol - (iAlpha * 26)
If iAlpha 0 Then
ConvertToLetter = Chr(iAlpha + 64)
End If
If iRemainder 0 Then
ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
End If
End Function

It correctly converts but fails around the transition points:-

52 AZ
53 A[
54 BB
.
78 BZ
79 B[
80 B\
81 CC

As you can see it corrects itself as 54 is indeed BB and 81 is CC and as
can
be seen it progressively gets worse...
Anyone any ideas on a foolproof conversion? I have to count up to 256
(IV)
cheers
Jeff


--

Dave Peterson



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Converting Numeric Columns to Excel Alpha Notation

Thanks a heap....cant always trust microsoft code can we....or mine for that
matter...;)
cheers
Jeff


"avveerkar" wrote
in message ...

Change one line in your function iAlpha = Int(iCol / 27) to
iAlpha=Int((iCol-1)/26)

Veerkar


jeff Wrote:
Hi gurus!

I need to use column numbers in a loop and convert them to the column
letters for populating cells.

I initially wrote a conversion function but found it failing in
transition
from Z to AA and AZ to BA. So I scoured the net and got a couple of
conversion functions...all of which failed including this one from
Microsoft
Knowledge base...

Private Sub column()
Dim col As Integer
For col = 1 To 55
Debug.Print Str(col) & " " & ConvertToLetter(col)
Next col
End Sub

Function ConvertToLetter(iCol As Integer) As String
' FROM MICROSOFT
Dim iAlpha As Integer
Dim iRemainder As Integer
iAlpha = Int(iCol / 27)
iRemainder = iCol - (iAlpha * 26)
If iAlpha 0 Then
ConvertToLetter = Chr(iAlpha + 64)
End If
If iRemainder 0 Then
ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
End If
End Function

It correctly converts but fails around the transition points:-

52 AZ
53 A[
54 BB
..
78 BZ
79 B[
80 B\
81 CC

As you can see it corrects itself as 54 is indeed BB and 81 is CC and
as can
be seen it progressively gets worse...
Anyone any ideas on a foolproof conversion? I have to count up to 256
(IV)
cheers
Jeff



--
avveerkar
------------------------------------------------------------------------
avveerkar's Profile:
http://www.excelforum.com/member.php...o&userid=30338
View this thread: http://www.excelforum.com/showthread...hreadid=527894





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Converting Numeric Columns to Excel Alpha Notation

Instead of this:

oSheet.Range(lstrStartCell, lstrEndCell) = lstraData

You could use:

with oSheet
.range(.cells(1, lintPrevCol + 1), .cells(1,lintCurCol)) =lstraData
end with

(check to see if I got the right assignments!)

jeff wrote:

Thanks Dave

But I needed to let you know where I was using it, see following code
snippet...

'set previous and current columns and calculate Excel Column
Alphas
lintPrevCol = lintCurCol
lintCurCol = lintCurCol + (Kount - lintConCol - 1)
lstrStartCol = ConvertToLetter(lintPrevCol + 1)
lstrEndCol = ConvertToLetter(lintCurCol)

'output Connection Headings
lstrStartCell = lstrStartCol & "1"
lstrEndCell = lstrEndCol & "1"
oSheet.Range(lstrStartCell, lstrEndCell) = lstraFields

'output Connection Data
lstrStartCell = lstrStartCol & lstrRow
lstrEndCell = lstrEndCol & lstrRow
oSheet.Range(lstrStartCell, lstrEndCell) = lstraData

cheers
jeff

"Dave Peterson" wrote in message
...
Excel is pretty flexible. You can use numbers instead of letter in
expressions
like:

activesheet.cells(1,"A").value = "hi there"
or
activesheet.cells(1,1).value = "hi there"

So you may not need to do the conversion at all.

But if you must, this worked ok for me:

Option Explicit
Private Sub column()
Dim col As Integer
For col = 1 To 55
Debug.Print Str(col) & " " & ConvertToLetter(col)
Next col
End Sub
Function ConvertToLetter(iCol As Integer) As String
Dim myStr As String
myStr = Worksheets(1).Cells(1, iCol).Address(False, False)
myStr = Application.Substitute(myStr, "1", "")

ConvertToLetter = myStr
End Function


jeff wrote:

Hi gurus!

I need to use column numbers in a loop and convert them to the column
letters for populating cells.

I initially wrote a conversion function but found it failing in
transition
from Z to AA and AZ to BA. So I scoured the net and got a couple of
conversion functions...all of which failed including this one from
Microsoft
Knowledge base...

Private Sub column()
Dim col As Integer
For col = 1 To 55
Debug.Print Str(col) & " " & ConvertToLetter(col)
Next col
End Sub

Function ConvertToLetter(iCol As Integer) As String
' FROM MICROSOFT
Dim iAlpha As Integer
Dim iRemainder As Integer
iAlpha = Int(iCol / 27)
iRemainder = iCol - (iAlpha * 26)
If iAlpha 0 Then
ConvertToLetter = Chr(iAlpha + 64)
End If
If iRemainder 0 Then
ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
End If
End Function

It correctly converts but fails around the transition points:-

52 AZ
53 A[
54 BB
.
78 BZ
79 B[
80 B\
81 CC

As you can see it corrects itself as 54 is indeed BB and 81 is CC and as
can
be seen it progressively gets worse...
Anyone any ideas on a foolproof conversion? I have to count up to 256
(IV)
cheers
Jeff


--

Dave Peterson


--

Dave Peterson
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
Why are my spreadsheet columns numeric instead of alpha? gretchen Excel Discussion (Misc queries) 4 June 1st 07 06:02 PM
both columns and rows are numeric.want the columns to be alpha Amy Setting up and Configuration of Excel 2 September 26th 06 09:16 PM
Need to indirectly reference columns 26 using A1 notation based on numeric column number- how to? KR Excel Worksheet Functions 5 October 26th 05 07:08 PM
HOW DO I CHANGE COLUMNS FROM NUMERIC TO ALPHA HEADINGS? Barry Excel Discussion (Misc queries) 1 August 17th 05 05:09 PM
Columns are now numeric, not alpha. how to change back? samriepe Excel Discussion (Misc queries) 1 June 9th 05 03:17 PM


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