简体   繁体   中英

How to return a long string from a VBA function in Excel?

I've the following function defined in Excel:

Function LongString()
    Dim i As Integer

    Do
        LongString = LongString & "X"
        i = i + 1
    Loop Until i > 40000

End Function

This results in an error : #VALUE!

It seems that the maximum string length is limited to 32768 ?

How to get this working ?

--EDIT-- Thanks you all for your support. My solution was to split up my function into several cell which contain less then the 32768 characters.

According to Microsoft the 32767 length limit is in their specification (see here ).

Length of cell contents (text): "32,767 characters. Only 1,024 display in a cell; all 32,767 display in the formula bar."

As such the only way you will get more than that in is to break down strings into multiple cells.

Excel Allows more than 32 k in each cell. I've a strange situation, I have a cell with a string of 34743 bytes, i process the string but no able to return more than 32k. So the problem is in the return of the value, not in the max size of the cell.

Note Excel 2013, in 2003 the limit is 32k. But vba code it's still limited to return 32k even in Excel 2013. M$ rare bugs.

Your LonsString function returns a variant/variable-length string which can contain up to 2^31 (about 2 billion) characters.

However, as mentioned by @Jon cells can only contain up to 32767 characters. (change the data type of i to long to prove the point).

If you expand on what you are trying to achieve with LongString we may be able to offer some alternatives

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM