Number to Char like Excel

Number to Char like Excel

Postby Marc Vanzegbroeck » Fri Apr 03, 2020 8:52 am

Hi,

Is there a function that converts a number to a Character-string like excel columns
Code: Select all  Expand view
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
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium

Re: Number to Char like Excel

Postby MaxP » Fri Apr 03, 2020 10:58 am

Try this

Code: Select all  Expand view
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
User avatar
MaxP
 
Posts: 85
Joined: Thu Jul 12, 2007 2:02 pm

Re: Number to Char like Excel

Postby Marc Vanzegbroeck » Fri Apr 03, 2020 11:41 am

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

FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium

Re: Number to Char like Excel

Postby nageswaragunupudi » Sat Apr 04, 2020 5:40 pm

This is the function used by xbrowse.

Code: Select all  Expand view
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
User avatar
nageswaragunupudi
 
Posts: 10631
Joined: Sun Nov 19, 2006 5:22 am
Location: India


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: wilsongamboa and 45 guests