Page 1 of 1
Number to Char like Excel
Posted: Fri Apr 03, 2020 8:52 am
by Marc Vanzegbroeck
Hi,
Is there a function that converts a number to a Character-string like excel columns
Re: Number to Char like Excel
Posted: Fri Apr 03, 2020 10:58 am
by MaxP
Try this
Code: Select all | Expand
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
Re: Number to Char like Excel
Posted: Fri Apr 03, 2020 11:41 am
by Marc Vanzegbroeck
Thank you Massimo, it's working very nice

Re: Number to Char like Excel
Posted: Sat Apr 04, 2020 5:40 pm
by nageswaragunupudi
This is the function used by xbrowse.
Code: Select all | Expand
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