Number to Char like Excel

Post Reply
Marc Vanzegbroeck
Posts: 1102
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium
Contact:

Number to Char like Excel

Post by Marc Vanzegbroeck »

Hi,

Is there a function that converts a number to a Character-string like excel columns

Code: Select all

1 = A
2 = C
...
26 = Z
27 = AA
28 = AB
..
52 = AZ
53 = BA
..
Regards,
Marc

FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
User avatar
MaxP
Posts: 85
Joined: Thu Jul 12, 2007 2:02 pm
Contact:

Re: Number to Char like Excel

Post by MaxP »

Try this

Code: Select all

FUNCTION cGExcCol( nCol )
        LOCAL   cCol := "", nTmp

        IF nCol > 26
                nTmp := INT( nCol / 26 )
                IF nTmp > 26
                        RETURN 0
                ENDIF
                cCol += CHR( ASC( "@" ) + nTmp )
        ENDIF
        
        nTmp := nCol % 26
        IF nTmp > 0
                cCol += CHR( ASC( "@" ) + ( nCol % 26 ) )       
        ELSE
                cCol += "Z" 
        ENDIF        
RETURN cCol

/*****************************************************************************/

FUNCTION nGExcCol( cCol )
        LOCAL   nPos := 0

        cCol := ALLTRIM( cCol )
        IF .NOT. EMPTY( cCol )
                IF LEN( cCol ) > 1
                        nPos := 26 * (ASC( LEFT( cCol, 1 ) ) - ASC( "@" ))
                        cCol := SUBSTR( cCol, 2 )
                ENDIF
                nPos += (ASC( LEFT( cCol, 1 ) ) - ASC( "@" ))
                IF nPos > 230
                        nPos := 0
                ENDIF
        ENDIF
RETURN nPos
Regards,
Massimo
Marc Vanzegbroeck
Posts: 1102
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium
Contact:

Re: Number to Char like Excel

Post by Marc Vanzegbroeck »

Thank you Massimo, it's working very nice :D
Regards,
Marc

FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Number to Char like Excel

Post by nageswaragunupudi »

This is the function used by xbrowse.

Code: Select all

static Function MakeColAlphabet( nCol )

   local cCol  := ""
   local nDigit

   do while nCol > 0
      nDigit   := nCol % 26
      if nDigit == 0
         nDigit   := 26
         nCol     -= 26
      endif
      cCol     := Chr( nDigit + 64 ) + cCol
      nCol     := Int( nCol / 26 )
   enddo

return cCol
 
Regards

G. N. Rao.
Hyderabad, India
Post Reply