PNG32 PNG32 PNG32 PNG32
PNG32
Forum Excel, VBA, VSTO, Exceltips, Excelhj�lp PNG32 drivs av Excelspecialisten    Logga in     English
PNG32
PNG32 PNG32
PNG32

Logga in

PNG32

Du är inte inloggad. Logga in eller registrera dig för att skriva inlägg eller svara på inlägg.

För frågor om forumet, kontakta oss på webmaster@excelforum.se

PNG32 PNG32
PNG32 PNG32
PNG32

Excelforum

PNG32

 
ForumForumDiskussionerDiskussionerVBAVBATa bort tom rad i textfilTa bort tom rad i textfil
Föregående Föregående
 
Nästa Nästa
Nytt inlägg
 2017-01-03 14:36
 
 Ändrad av svindlarn86  på 2017-01-03 15:37:14
I am using a macro to copy a range of cells from an Excel worksheet to a new text file. Each row in the Excel file genereates a row in the textfile.The last row of the text file is empty and I can't remove this. The code looks like this: Dim r As Long Dim arr As Variant Dim file As Variant Set file = CreateObject("Scripting.FileSystemObject").CreateTextFile("c:\temp\returförslag" + " test.exp", True, True) With ActiveSheet arr = .Range(.[A1], .UsedRange.Cells(.UsedRange.Cells.Count)) For r = 1 To UBound(arr) strfile = Join(WorksheetFunction.Index(arr, r), vbTab) strfile = Replace(strfile, Chr(13), "") strfile = Replace(strfile, Chr(10), "") strfile = Replace(strfile, vbcrlf"") 'file.WriteLine Join(WorksheetFunction.Index(arr, r), vbTab) file.WriteLine strfile Next End With I have tried to use the replace function to remove the carriage return but unfortunately with no success. Can you please help me out with this annoying problem?
Nytt inlägg
 2017-01-09 12:16
 

 Hi

You probably need to handle the last loop differently.. So after initiaing the loop:

If r = ubound(arr)

      Exit for

End If

(This little snippet will exit the loop at the last iteration, thus preventing the last line to be generated). If you notice that you are missing last row of data, then you will need file.writeline join(worksheetfunction.indext(arr, r), vbTab) within the IF, before the exit for. 

Best regards

Chris

 

 

Föregående Föregående
 
Nästa Nästa
ForumForumDiskussionerDiskussionerVBAVBATa bort tom rad i textfilTa bort tom rad i textfil

PNG32 PNG32
Excelforum drivs av Excelspecialisten som bedriver utbildning i Excel och VBA, tillhandahåller support och hjälp med Excel, utvecklar program i Excel. Är ni i behov av en konsult inom Excel, VBA eller VSTO, eller söker en excelkurs, kontakta oss.
Copyright 2013 ExcelSpecialisten XLS AB   Användarvillkor  Personliga uppgifter