Saving cell values to different text files without spaces or CR

Wingger

New Member
Joined
Dec 9, 2021
Messages
1
Office Version
  1. 2011
Platform
  1. Windows
Good morning. I'm trying to update several .txt files via Excel and I keep getting additional spaces and carriage returns in the text files. I've tried changing the cell format to general, number (in certain cells) and text with no change. My code is shown below:

Any help would be appreciated.

Thanks
 

Attachments

  • VBA Code.jpg
    VBA Code.jpg
    224 KB · Views: 14

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
2,827
Office Version
  1. 2013
Platform
  1. Windows
Hi & welcome to MrExcel.

In addition to @severynm's suggestions to eliminate the characters you might not be aware of, I would like to note that the output always differs from your input since you're using the Print statement.
Print expects the output to be a single text line and always adds two bytes and the end of each output: Carriage Return (13 / &hex 0D) and LineFeed (10 / &hex 0A).

If you don't want that to happen, try using the Put statement and open your file for binary access. That way you have full control.
Note that's best to get the cell's content by using the Text property. That way you get the data exactly as it is on the screen, including currency characters or percent signs.
The use of a buffer is needed as well, otherwise there are so called descriptors added and that way you will not get plain text files.

As a first side note, it's not necessary to select a cell to obtain its contents.
As a second side note, the worksheet ranges aren't qualified in your code (like Worksheet("MySheet").Range("B5")) which may lead to unexpected results.

Finally, to avoid repetitive code you might consider using a separate procedure to write data to a text file. This could look something like this:

VBA Code:
Sub Wingger()

    ' === usage example ====

    Dim FileName As String, DataBuffer As String
  
    FileName = "C:\Users\public\Desktop\Weber Labeler Files\Data Text Files\Sold To.txt"
    DataBuffer = Range("B5").Text

    WriteTextFile FileName, DataBuffer
End Sub


Public Sub WriteTextFile(ByVal argFullFileName As String, ByRef argText As String)
    Dim FileHandle As Long
    FileHandle = FreeFile
    Open argFullFileName For Binary Access Read Write Lock Read Write As #FileHandle
    Put #FileHandle, , argText
    Close #FileHandle
End Sub
 

Forum statistics

Threads
1,176,500
Messages
5,903,415
Members
435,028
Latest member
excelvbanoob420

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top