I know this is an old thread, but it was recently referenced in more recent thread...
http://www.mrexcel.com/forum/excel-...ations-code-pasting-notepad-without-tabs.html
I just thought the readers of this thread might like to see a slightly different, slightly more compact macro which will do the same thing that Andrew's code does...
Code:Sub Test() Dim X As Long, FileNo As Long, FileText As String [COLOR=#008000][B]' Change file name to suit[/B][/COLOR] Const FileName As String = "C:\TEMP\MyFile.txt" [COLOR=#008000][B]' Change sheet reference to suit[/B][/COLOR] With Sheets("Sheet1").Range("A1").CurrentRegion For X = 1 To .Rows.Count FileText = FileText & vbCrLf & Join(Application.Index(.Rows(X).Value, 1, 0), "|") Next End With FileNo = FreeFile Open FileName For Output As #FileNo Print #FileNo, Mid(FileText, 3) Close #FileNo End Sub
Sub SaveCopy()
'This savecopy routine will output to a pipe delimited file, good for bulk inserting into an Oracle DB
Dim vFileName As Variant
Dim rngLastCell As Range
Dim lLastRow As Long
Dim nLastCol As Integer
Dim lCurrRow As Long
Dim nCurrCol As Integer
Dim sRowString As String
Dim ArchiveFolder As String
ArchiveFolder = "C:\Temp\"
Application.DisplayAlerts = False
vFileName = ArchiveFolder & "Spins_" & Format(Now(), "YYYYMMDDHHMMSS") & ".txt"
Open vFileName For Output As #1
Set rngLastCell = ActiveSheet.Range("A1").SpecialCells(xlLastCell)
lLastRow = Range("A" & Rows.Count).End(xlUp).Row
nLastCol = Range("XFD1").End(xlToLeft).Column
For lCurrRow = 1 To lLastRow
sRowString = ActiveSheet.Cells(lCurrRow, 1).Formula
For nCurrCol = 2 To nLastCol
sRowString = sRowString & "|" & ActiveSheet.Cells(lCurrRow, nCurrCol).Formula
Next nCurrCol
If Len(sRowString) = nLastCol - 1 Then
Print #1,
Else
Print #1, sRowString
End If
Next lCurrRow
Close #1
ActiveWindow.Close False
Application.DisplayAlerts = True
End Sub
Where were you 5 years ago mate, I could have really done with this lol. I have been using this horrible code I wrote all this time:
........
I am sorry, but my answer in Message #9 has led this thread astray. If all you want to do is concatenate a single column of contiguous cells together to form a pipe delimited text string, you can do that with one line of code...
PipeDelimitedText = Join(Application.Transpose(Sheets("Sheet1").Range("A4:A87").Value), "|")
Yes, something like this...Hi Rick, would concatenation be the answer if I wanted to wrap rows? For instance, if the range was A1:Z20.
For Col = 1 To 26
PipeDelimitedText = PipeDelimitedText & "|" & Join(Application.Transpose(Sheets( _
"Sheet3").Range("A1:A20").Offset(, Col - 1).Value), "|")
Next
Yes, something like this...
Code:For Col = 1 To 26 PipeDelimitedText = PipeDelimitedText & "|" & Join(Application.Transpose(Sheets( _ "Sheet3").Range("A1:A20").Offset(, Col - 1).Value), "|") Next
Open FileName For Output As #FileNo
Dim X As Long, FileNo As Long, FileText As String
' Change file name to suit
Const FileName As String = "[B]Desktop:\Testfile.txt[/B]"
' Change sheet reference to suit
With Sheets("NTFile").Range("A1:EX20")
For X = 1 To .Rows.Count
FileText = FileText & vbCrLf & Join(Application.Index(.Rows(X).Value, 1, 0), "|")
Next
End With
FileNo = FreeFile
Open FileName For Output As #FileNo
Print #FileNo, Mid(FileText, 3)
Close #FileNo
You cannot use functions when assigning values to constants in a Const statement. FileName will have to be Dim'med as a String variable. Once that is done, you can use this to assign the path/filename to it...Use the environment variables:
Const FileName As String = environ("HOMEDRIVE") & Environ("HOMEPATH") & "\Desktop\Testfile.txt"
Dim FileName As String
FileName = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\Testfile.txt"
You cannot use functions when assigning values to constants in a Const statement. FileName will have to be Dim'med as a String variable. Once that is done, you can use this to assign the path/filename to it...
The reason is that VB physically substitutes the constant's value for the constant's name throughout the program prior to compiling it... since the program is not yet compiled at this point, function evaluation cannot occur.I have once again learned something today .
I have never actually used constants to be honest, I always use string variables (I don't know why).