Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Copy From One Column to Text File

Hi I have a problem with Excel that I would like to use a Macro to do.
I am unfamilar with the syntax used in Excel so this task is proving
to be somewhat tricky.

I would like to copy all entries from one colum into a text file.
Additionally, I would like to copy these files in such a way that I
can use those files as directory names. For example, if the value in
one of the column cells was "example", I would like to copy that value
as "/value".

I would also like the macro to automatically detect how long the
column I am copying from is, so that instead of having to specify a
range, the macro can be used in any file and it will automatically
detect from what range the columns must be copied from. The only thing
I would specify would be the column (which I would change int eh
macro).

How would I go about writng a Macro to perform such a task?

Thanks

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Copy From One Column to Text File

Hi I have a problem with Excel that I would like to use a Macro to do.
I am unfamilar with the syntax used in Excel so this task is proving
to be somewhat tricky.

I would like to copy all entries from one colum into a text file.
Additionally, I would like to copy these files in such a way that I
can use those files as directory names. For example, if the value in
one of the column cells was "example", I would like to copy that value
as "/value".

I would also like the macro to automatically detect how long the
column I am copying from is, so that instead of having to specify a
range, the macro can be used in any file and it will automatically
detect from what range the columns must be copied from. The only thing
I would specify would be the column (which I would change int eh
macro).

How would I go about writng a Macro to perform such a task?


Some questions first... How do you want the cell values placed in the
file... each on their own line or all on one line with comma, tab, something
else delimited? Where does your data start in its row... row 1 (no header),
2 (with header), or some other row? Could there be any blank cells within a
column (other than at the end of the data) and, if so, how did you want them
treated? Did you really mean a forward slash in your example (you said
directory, which would be a back slash)? I'm guessing you said the value was
"example" so you meant to write "\example" and not "/value", right?

Rick

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Copy From One Column to Text File


Thanks for your response. Let be more specific.

- I would like to display the data as a series of new lines e.g.
\example1
\example2
\example3

and so forth. So far I can only display as comma sperated values, butI
require new lines.

- Blank cells should be ignored
- The data should start from Row 2 (IE miss the header).

Finally, one other piece of functionality I wold like to incoporate
(if possible) is to then use that list of directory names and create
seperate sub directories in the folder that the text file has been
saved in (but also retaining the saved text file).

Anil



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Copy From One Column to Text File

Incidentally, an example of the Macro I am tryign to wruite so far is
as follows:

Sub ExportToPRN()
Dim FName As String
Dim WholeLine As String
Dim FNum As Integer
Dim myRange As Range
Dim myCell As Range
Dim myName As String

myName = ActiveWorkbook.FullName

FName = Application.GetSaveAsFilename( _
Replace(myName, ".xls", ".prn"))

On Error GoTo EndMacro:
FNum = FreeFile

Open FName For Output Access Write As #FNum
Set myRange = Range("C1:C99")
WholeLine = ""
For Each myCell In myRange
WholeLine = WholeLine & myCell.Text & ", "
Next myCell

WholeLine = Left(WholeLine, Len(WholeLine) - 1)

Print #FNum, Trim(WholeLine)

EndMacro:
On Error GoTo 0
Close #FNum
End Sub

I would like to add line breaks instead of commas at the line "
WholeLine = WholeLine & myCell.Text & ", " ", but I am unsure of the
syntax to use. "\r\n" and "\n" do not appear to work.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Copy From One Column to Text File

I would like to add line breaks instead of commas at the line "
WholeLine = WholeLine & myCell.Text & ", " ", but I am unsure of the
syntax to use. "\r\n" and "\n" do not appear to work.


Use the built-in newline character sequence constant... vbNewLine.

WholeLine = WholeLine & myCell.Text & vbNewLine

Rick


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Copy From One Column to Text File

Finally, one other piece of functionality I wold like to incoporate
(if possible) is to then use that list of directory names and create
seperate sub directories in the folder that the text file has been
saved in (but also retaining the saved text file).


You can use the MkDir Statement for that.

MkDir PathForTextFile & "\" & CellValue

Do the above within your For Each loop (posted in your other message).

Rick
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Copy From One Column to Text File

Thank you very much Rick. Using that line and working out the rest, I
have a partial solution:

Sub ExportToPRN()
Dim FName As String
Dim WholeLine As String
Dim FNum As Integer
Dim myRange As Range
Dim myCell As Range
Dim myName As String

myName = ActiveWorkbook.FullName

FName = Application.GetSaveAsFilename( _
Replace(myName, ".xls", ".prn"))

On Error GoTo EndMacro:
FNum = FreeFile

Open FName For Output Access Write As #FNum
Set myRange = Range("C1:C99")
WholeLine = ""
For Each myCell In myRange
WholeLine = WholeLine & "\" & myCell.Text & vbNewLine
Next myCell

Is it now possible with the generated text file to create directories
from that file? I can always use a PHP script or similar to do it, but
it would be nice if it could be done automatically by the macro.


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Copy From One Column to Text File

Thanks Rick, you're a super star.


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Copy From One Column to Text File

One final problem when trying to use the make directory command. This
macro doesn't seem to generate anything. Any noticable problems with
this macro?

Sub ExportToPRN()
Dim FName As String
Dim WholeLine As String
Dim FNum As Integer
Dim myRange As Range
Dim myCell As Range
Dim myName As String

myName = ActiveWorkbook.FullName

FName = Application.GetSaveAsFilename( _
Replace(myName, ".xls", ".prn"))

On Error GoTo EndMacro:
FNum = FreeFile

Open FName For Output Access Write As #FNum
Set myRange = Range("C1:C99")
WholeLine = ""
For Each myCell In myRange
WholeLine = WholeLine & "\" & myCell.Text & vbNewLine
MkDir PathForTextFile & "\" & myCell.Text
Next myCell

WholeLine = Left(WholeLine, Len(WholeLine) - 1)

Print #FNum, Trim(WholeLine)

EndMacro:
On Error GoTo 0
Close #FNum
End Sub

I have tried removing the "WholeLine = WholeLine & "\" & myCell.Text &
vbNewLine " line, but without success.


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Copy From One Column to Text File

One final problem when trying to use the make directory command. This
macro creates all of the sub directories in the C:\ folder. I would
like the directories to be made in the same directory as the saved
text file.

Also, the text file now no longer contains anything in it. Is it
possible to make the directories AND save the text file?

Sub ExportToPRN()
Dim FName As String
Dim WholeLine As String
Dim FNum As Integer
Dim myRange As Range
Dim myCell As Range
Dim myName As String

myName = ActiveWorkbook.FullName

FName = Application.GetSaveAsFilename( _
Replace(myName, ".xls", ".prn"))

On Error GoTo EndMacro:
FNum = FreeFile

Open FName For Output Access Write As #FNum
Set myRange = Range("C1:C99")
WholeLine = ""
For Each myCell In myRange
MkDir PathForTextFile & "\" & CellValue
WholeLine = WholeLine & "\" & myCell.Text & vbNewLine
Next myCell

WholeLine = Left(WholeLine, Len(WholeLine) - 1)

Print #FNum, Trim(WholeLine)

EndMacro:
On Error GoTo 0
Close #FNum
End Sub

I have tried removing the "WholeLine = WholeLine & "\" & myCell.Text &
vbNewLine " line, but without success.



  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Copy From One Column to Text File

Can anyone else share some help?


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
copy column to another file climate Excel Worksheet Functions 0 December 21st 09 09:46 AM
Search text string for ssn and copy to new column mary Excel Discussion (Misc queries) 8 December 28th 07 03:13 AM
FIND LAST COPY OF A TEXT IN A COLUMN nastech Excel Discussion (Misc queries) 10 October 6th 07 03:11 AM
To copy values in a column relevant to text in an adjacent column? Guy Keon Excel Worksheet Functions 2 November 15th 05 08:10 PM
VBA Macro to copy an column from one Excel file into another file Sam Excel Programming 4 July 3rd 05 03:36 AM


All times are GMT +1. The time now is 02:34 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"