PC Review


Reply
Thread Tools Rate Thread

Concatenate & Write to Text File

 
 
Dan R.
Guest
Posts: n/a
 
      5th Mar 2007
I'm trying to concatenate columns 1, 3, 4 and write the results to a
text file. I have some code from one of J-Walk's books but I can't
seem to manipulate it to do what I want. Here it is if you want
something to start with:

Sub ExportRange()
Dim Filename As String
Dim NumRows As Long, NumCols As Integer
Dim r As Long, c As Integer
Dim Data
Dim ExpRng As Range
Set ExpRng = Selection
NumCols = ExpRng.Columns.Count
NumRows = ExpRng.Rows.Count
Filename = "C:\Windows\Desktop\textfile.txt"
Open Filename For Output As #1
For r = 1 To NumRows
For c = 1 To NumCols
Data = ExpRng.Cells(r, c).Value
If IsNumeric(Data) Then Data = Val(Data)
If IsEmpty(ExpRng.Cells(r, c)) Then Data = ""
If c <> NumCols Then
Write #1, Data;
Else
Write #1, Data
End If
Next c
Next r
Close #1
End Sub

Thank You,
-- Dan

 
Reply With Quote
 
 
 
 
JE McGimpsey
Guest
Posts: n/a
 
      5th Mar 2007
One way:

Public Sub ExportRange()
Const csDELIM As String = ""
Const csFILENAME As String = "C:\Windows\Desktop\textfile.txt"
Dim rCell As Range
Dim nFileNumber As Long
Dim sData As String

If TypeOf Selection Is Range Then
nFileNumber = FreeFile
Open csFILENAME For Output As nFileNumber
For Each rCell In Selection.EntireRow.Columns(1).Cells
With rCell
sData = .Text & csDELIM & .Offset(0, 2).Text & _
csDELIM & .Offset(0, 3).Text
End With
Print #nFileNumber, sData
Next rCell
Close #nFileNumber
End If
End Sub

If you want a delimiter between cell values, change csDELIM to suit.


In article <(E-Mail Removed)>,
"Dan R." <(E-Mail Removed)> wrote:

> I'm trying to concatenate columns 1, 3, 4 and write the results to a
> text file. I have some code from one of J-Walk's books but I can't
> seem to manipulate it to do what I want. Here it is if you want
> something to start with:
>
> Sub ExportRange()
> Dim Filename As String
> Dim NumRows As Long, NumCols As Integer
> Dim r As Long, c As Integer
> Dim Data
> Dim ExpRng As Range
> Set ExpRng = Selection
> NumCols = ExpRng.Columns.Count
> NumRows = ExpRng.Rows.Count
> Filename = "C:\Windows\Desktop\textfile.txt"
> Open Filename For Output As #1
> For r = 1 To NumRows
> For c = 1 To NumCols
> Data = ExpRng.Cells(r, c).Value
> If IsNumeric(Data) Then Data = Val(Data)
> If IsEmpty(ExpRng.Cells(r, c)) Then Data = ""
> If c <> NumCols Then
> Write #1, Data;
> Else
> Write #1, Data
> End If
> Next c
> Next r
> Close #1
> End Sub
>
> Thank You,
> -- Dan

 
Reply With Quote
 
Dan R.
Guest
Posts: n/a
 
      5th Mar 2007
Excellent, works great JE. Quick question if I might, I'm trying to
stay away from using a selected range. Could I just use something like
this:

Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range

Set rng1 = Range("A3", Cells(Rows.Count, 1))
Set rng2 = Range("C3", Cells(Rows.Count, 3))
Set rng3 = Range("D3", Cells(Rows.Count, 4))

And then maybe: sData = rng1 & rng2 & rng3
or something of the like?

Also, our computers are set up like this: C:\Documents and Settings
\username\Desktop, is there a way to have the txtfile output to the
users desktop without having to prompt for the directory?

Thanks again,
-- Dan

 
Reply With Quote
 
JE McGimpsey
Guest
Posts: n/a
 
      6th Mar 2007
Probably better to replace

For Each rCell In Selection.EntireRow.Columns(1).Cells

with

For Each rCell in Range("A3:A" & _
Range("A" & Rows.Count).End(xlUp).Row)

In article <(E-Mail Removed)>,
"Dan R." <(E-Mail Removed)> wrote:

> Excellent, works great JE. Quick question if I might, I'm trying to
> stay away from using a selected range. Could I just use something like
> this:
>
> Dim rng1 As Range
> Dim rng2 As Range
> Dim rng3 As Range
>
> Set rng1 = Range("A3", Cells(Rows.Count, 1))
> Set rng2 = Range("C3", Cells(Rows.Count, 3))
> Set rng3 = Range("D3", Cells(Rows.Count, 4))
>
> And then maybe: sData = rng1 & rng2 & rng3
> or something of the like?
>
> Also, our computers are set up like this: C:\Documents and Settings
> \username\Desktop, is there a way to have the txtfile output to the
> users desktop without having to prompt for the directory?
>
> Thanks again,
> -- Dan

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
how do I write an expression to concatenate text =?Utf-8?B?aGFpbHM=?= Microsoft Access Form Coding 3 19th Nov 2005 12:17 AM
Concatenate to a text file NewGuy100 Microsoft Excel Programming 1 11th Oct 2005 06:45 PM
Is there any way for System.IO.StreamWriter Write method to write out part of the string to file. such as if the machine is shut down half way through? or does the file not actually exist until the entire write is completed successfully. Daniel Microsoft Dot NET 1 7th Sep 2005 03:02 PM
Concatenate Text File RickKennedy Microsoft Excel Programming 2 19th Oct 2004 06:05 PM
Concatenate Text File RickKennedy Microsoft Excel Programming 1 18th Oct 2004 10:31 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:23 AM.