LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 80
Default SaveAs call changes text to #####

I have this piece of VB code in Excel that takes in various Excel files and
translates them into tab delimited text files. The code to go through the
files in the directory one by one and do the export does not work in Excel
2007 so I have modified it. I now have it exporting the files. However,
some of them are not exporting correctly. If the data in the cell is too
many characters, it exports the field as ########### instead of the values.

This is the code to do the export of the individual files:

Function ExportCurWorkbook() As Boolean
Dim cell As Object, i As Integer
Dim wrkbk As Workbook, flPath As String

On Error GoTo ExportFail

i = 2

'Select cell A2, *first line of data*.
Range("A" + Format(i) + ":D" + Format(i)).Select
' Set Do loop to stop when an empty cell is reached.
Do Until IsEmpty(ActiveCell)
Selection.NumberFormat = "@"
For Each cell In Selection
'Apply formatting that the user has selected
'if the cell is empty, insert a - replace commas
'with / to avoid quotes in text file
If IsNull(cell.Value) Or cell.Value = "" Or cell.Value = " " Then
If INSERT_DASH Then cell.Value = "-"
Else
If CONVERT_TEXT Then
cell.Value = " " & cell.Value
cell.Value = Right(cell.Value, Len(cell.Value) - 1)
End If

If REPLACE_COMMA Then
cell.Value = Replace(cell.Value, ",", "/")
End If
End If
'A2=LEFT(A1,CEILING(LEN(A1)/2,1))
'A3 = Right(A1, Floor(Len(A1) / 2, 1))

Next
' Step down 1 row from present location.
i = i + 1
Range("A" + Format(i) + ":D" + Format(i)).Select
Loop

'Save the workbook as a text file, then close the spreadsheet
Set wrkbk = ActiveWorkbook

If EXPORT_FILE Then
If txtpath = "" Then txtpath = "C:"
Else
txtpath = "Y:\QuickLinks\MSAccess2007 test folder jkn\test
specialsearch\data\exchange_route\"
End If

' Remove the .xls in the filename and tack on .txt
flPath = txtpath & "\" & Left(wrkbk.name, Len(wrkbk.name) - 4) & ".txt"

Application.DisplayAlerts = False
wrkbk.SaveAs flPath, xlTextWindows

wrkbk.Close SAVE_FILE

ExportCurWorkbook = True

ExportCurWorkbook_exit:
Exit Function

ExportFail:
ExportCurWorkbook = False
Resume ExportCurWorkbook_exit

End Function

I have narrowed the problem down to this line: wrkbk.SaveAs flPath,
xlTextWindows - when I step through the Do Until loops, the cell.Value of the
cell in question is what is supposed to be but when the SaveAs call is made,
it converts the cell to #############.

I am at a loss. As a note, this is not the function I was working on - this
same call (wrkbk.SaveAs) is used in Excel 2003 and the cells are exported
correctly. There is no other difference in the .xls files that are being
changed to text format. I have also verified it is not the data itself - but
rather the length of the data.

Thank you.
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
SaveAs Text Dan Excel Programming 1 October 11th 07 03:02 PM
runtime error 1004 saveas text file Janis Excel Programming 2 September 1st 06 11:11 PM
SaveAs API Call Eric Excel Programming 4 March 1st 05 10:02 PM
SaveAs text file from VBA Getting Error JeepNC Excel Programming 1 January 28th 05 10:20 PM
saveas fixed text for unix jeffP Excel Programming 10 September 8th 04 01:11 PM


All times are GMT +1. The time now is 12:45 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"