PDA

View Full Version : Solved: cell contents were missing once i reopen a file



rafi_07max
12-13-2010, 12:14 AM
I have a workbook with many sheets.
What I wanted to do is save a particular sheet by the name of “board” into .txt file

I used the following codes:.


Private Sub CommandButton1_Click()
Dim ws As Worksheet
Set ws = Worksheets("board")
ws.Copy
fileSaveName = Application.GetSaveAsFilename( _
fileFilter:="txt Files (*.txt), *.txt")
If fileSaveName <> "False" Then
ActiveWorkbook.SaveAs fileSaveName
End If
End Sub



So once I saved the file and when I reopen the file using excel some of the contents in a cell were missing.
Take a look at the image below:
http://www.iimmgg.com/image/28bfd5d87093e8f9050af73fa6c540f9

How can I prevent the data from missing?

I have attached a sample workbook
5076

macropod
12-13-2010, 01:48 AM
Hi rafi,

The problem you're having is due to the text format being limited to 255 characters per cell. The same limit applies to CVS and SLK formats also.

What you could do instead, is to create a text file, then write the contents of each cell to that file, with whatever row & column delimiters you require.

rafi_07max
12-13-2010, 02:16 AM
thanks macropod for the reply,
Sorry i didn't understand what you were saying. what do you mean by create a txt file? Isn't the codes i used creating a txt file too?

macropod
12-13-2010, 03:12 AM
Hi Rafi,

Saving as a text file, and opening a text file to write to it are two very different things. To see how different, try this:
Sub Demo()
Dim i As Long, j As Long, ArrVals()
Open ActiveWorkbook.Path & "\Data.txt" For Output As #1
With ActiveSheet.UsedRange
ReDim Preserve ArrVals(.Columns.Count - 1)
For i = 1 To .Rows.Count
For j = 1 To .Columns.Count
ArrVals(j - 1) = .Cells(i, j).Value
Next
Write #1, Join(ArrVals, Chr(34) & vbTab & Chr(34))
Next
End With
Close #1
End Sub
You will, of course, have to adapt the code to your own needs.

rafi_07max
12-14-2010, 07:25 AM
Thanks macropod for your help.