Excel cell won't hold 32K byte string
I'm trying to store a text file in a single Excel 2013 cell. I load the file into a string and then store it in a cell, and I get an "Out of Memory" exception if the file size is greater than 16K, not 32K as I expected. My code looks like this:
Sub LoadText()
Dim idx As Integer
Dim fname As String
Dim filenum As Long
Dim filetext As String
fname = "C:\test.txt"
If Len(Dir(fname)) > 0 Then
filetext = Space(FileLen(fname))
filenum = FreeFile()
Open fname For Binary Access Read As #filenum
Get #filenum, , filetext
Close #filenum
Else
filetext = "NO FILE FOUND!"
End If
filetext = Trim(filetext)
If LenB(filetext) <= 32768 Then
Cells(1, 1).Value = filetext ' Code fails with "out of memory" if LenB(filetext) > 16384
Else
Cells(1, 1).Value = "TOO BIG!"
End If
End Sub
The test case failed with LenB(filetext) at 28K, Len(filetext) at 14K. LenB() reports string sizes twice as large as reported by Len(), which means I'm storing one character in two bytes, which I don't understand either. I'm running 64bit Windows 8.1 on a Dell 8700 purchased in the US, and everything I've read says that should mean either ANSI or UTF-8, one byte per character. I've also coded this routine to read the text file a line at a time until EOF, with the same results. Anyone have any suggestions?
Thanks for looking.
Recent comments
5 years 41 weeks ago
6 years 27 weeks ago
6 years 39 weeks ago
6 years 42 weeks ago
6 years 43 weeks ago
6 years 48 weeks ago
7 years 4 weeks ago
7 years 5 weeks ago
7 years 5 weeks ago
7 years 5 weeks ago