Consulting

Results 1 to 5 of 5

Thread: Solved: cell contents were missing once i reopen a file

  1. #1

    Solved: cell contents were missing once i reopen a file

    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:.


    [vba]
    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

    [/vba]

    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/28bfd5d8...0af73fa6c540f9

    How can I prevent the data from missing?

    I have attached a sample workbook

    Attachment 5076

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    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?

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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:
    [VBA]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[/VBA]
    You will, of course, have to adapt the code to your own needs.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    Thanks macropod for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •