ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Help with loop (https://www.excelbanter.com/excel-programming/320244-help-loop.html)

Steph[_3_]

Help with loop
 
Hello. I was hoping someone could make a monir edit to a small piece of
code I have. Thanks to Dick Kusleika, I have the following code that writes
the data on Sheet1 onto a text file. This works great, with one small
exception - There is an extra blank line at the bottom of the text file.
The text file is being used as an EDI transfer, and apparently the EDI is
very picky about extra blank lines! How can I edit the code so the extra
vbnewline is not added at the bottom?? Thanks so much!!

Sub SaveAsTextFile()

Dim sh As Worksheet
Dim rRow As Range
Dim rCell As Range
Dim sOutput As String
Dim lFnum As Long
Dim sFname As String

Set sh = Worksheets("Sheet1")

For Each rRow In sh.UsedRange.Rows
For Each rCell In rRow.Cells
sOutput = sOutput & rCell.Text & vbTab
Next rCell
sOutput = sOutput & vbNewLine
Next rRow

lFnum = FreeFile
sFname = "C:\Testfile.txt"

Open sFname For Output As lFnum

Print #lFnum, sOutput

Close lFnum

End Sub



Bob Phillips[_7_]

Help with loop
 
Sub SaveAsTextFile()

Dim sh As Worksheet
Dim rRow As Range
Dim rCell As Range
Dim sOutput As String
Dim lFnum As Long
Dim sFname As String

Set sh = Worksheets("Sheet1")

For Each rRow In sh.UsedRange.Rows
For Each rCell In rRow.Cells
sOutput = sOutput & rCell.Text & vbTab
Next rCell
sOutput = sOutput & vbNewLine
Next rRow
sOutput = Left(sOutput, Len(sOutput) - 1)

lFnum = FreeFile
sFname = "C:\Testfile.txt"

Open sFname For Output As lFnum

Print #lFnum, sOutput

Close lFnum

End Sub

--
HTH

-------

Bob Phillips
"Steph" wrote in message
...
Hello. I was hoping someone could make a monir edit to a small piece of
code I have. Thanks to Dick Kusleika, I have the following code that

writes
the data on Sheet1 onto a text file. This works great, with one small
exception - There is an extra blank line at the bottom of the text file.
The text file is being used as an EDI transfer, and apparently the EDI is
very picky about extra blank lines! How can I edit the code so the extra
vbnewline is not added at the bottom?? Thanks so much!!

Sub SaveAsTextFile()

Dim sh As Worksheet
Dim rRow As Range
Dim rCell As Range
Dim sOutput As String
Dim lFnum As Long
Dim sFname As String

Set sh = Worksheets("Sheet1")

For Each rRow In sh.UsedRange.Rows
For Each rCell In rRow.Cells
sOutput = sOutput & rCell.Text & vbTab
Next rCell
sOutput = sOutput & vbNewLine
Next rRow

lFnum = FreeFile
sFname = "C:\Testfile.txt"

Open sFname For Output As lFnum

Print #lFnum, sOutput

Close lFnum

End Sub






All times are GMT +1. The time now is 09:52 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com