Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Macro to Export Active Cells, Comma Sep to .txt file

Hello, this is my first post on a very old subject.

There are 2 changes I'd like to make to this code.

1. I'd like it not to ask for a filename, and use the Worksheet name
instead.
2. This code save a Tab separated text file, I'd like to use Comma's
instead.

I tried changing vbTab to vbComma, but then I get an error on the line
"CurrTextStr = Left(CurrTextStr, Len(CurrTextStr) - 1)".

Any assistance would be appreciated!

Sub OutputActiveSheetAsTabDelim()
Dim SrcRg As Range
Dim CurrRow As Range
Dim CurrCell As Range
Dim CurrTextStr As String
Dim ListSep As String
Dim FName As Variant
Dim ColCount As Integer
Dim CurrCol As Integer
FName = Application.GetSaveAsFilename("", "Tab Delimited File
(*.txt),*.txt")
If FName < False Then
ListSep = vbTab ''Chg to comma, etc for a different separator
If Selection.Cells.Count 1 Then
Set SrcRg = Selection
Else
Set SrcRg = ActiveSheet.UsedRange
End If
ColCount = SrcRg.Columns.Count
Open FName For Output As #1
For Each CurrRow In SrcRg.Rows
CurrCol = 0
CurrTextStr = ""
For Each CurrCell In CurrRow.Cells
CurrCol = CurrCol + 1
CurrTextStr = CurrTextStr & CurrCell.Value & ListSep
Next
While Right(CurrTextStr, 1) = ListSep
CurrTextStr = Left(CurrTextStr, Len(CurrTextStr) - 1)
Wend
Print #1, CurrTextStr
Next
Close #1
End If
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Macro to Export Active Cells, Comma Sep to .txt file

Sub OutputActiveSheetAsTabDelim()
Dim SrcRg As Range
Dim CurrRow As Range
Dim CurrCell As Range
Dim CurrTextStr As String
Dim ListSep As String
Dim FName As Variant
Dim ColCount As Integer
Dim CurrCol As Integer
sPath = "C:\Myfolder\"
FName = sPath & Activesheet.Name & ".csv"
ListSep = ","
If Selection.Cells.Count 1 Then
Set SrcRg = Selection
Else
Set SrcRg = ActiveSheet.UsedRange
End If
ColCount = SrcRg.Columns.Count
Open FName For Output As #1
For Each CurrRow In SrcRg.Rows
CurrCol = 0
CurrTextStr = ""
For Each CurrCell In CurrRow.Cells
CurrCol = CurrCol + 1
CurrTextStr = CurrTextStr & CurrCell.Value & ListSep
Next
While Right(CurrTextStr, 1) = ListSep
CurrTextStr = Left(CurrTextStr, Len(CurrTextStr) - 1)
Wend
Print #1, CurrTextStr
Next
Close #1
End If
End Sub

--
Regards,
Tom Ogilvy


"Stryves" wrote:

Hello, this is my first post on a very old subject.

There are 2 changes I'd like to make to this code.

1. I'd like it not to ask for a filename, and use the Worksheet name
instead.
2. This code save a Tab separated text file, I'd like to use Comma's
instead.

I tried changing vbTab to vbComma, but then I get an error on the line
"CurrTextStr = Left(CurrTextStr, Len(CurrTextStr) - 1)".

Any assistance would be appreciated!

Sub OutputActiveSheetAsTabDelim()
Dim SrcRg As Range
Dim CurrRow As Range
Dim CurrCell As Range
Dim CurrTextStr As String
Dim ListSep As String
Dim FName As Variant
Dim ColCount As Integer
Dim CurrCol As Integer
FName = Application.GetSaveAsFilename("", "Tab Delimited File
(*.txt),*.txt")
If FName < False Then
ListSep = vbTab ''Chg to comma, etc for a different separator
If Selection.Cells.Count 1 Then
Set SrcRg = Selection
Else
Set SrcRg = ActiveSheet.UsedRange
End If
ColCount = SrcRg.Columns.Count
Open FName For Output As #1
For Each CurrRow In SrcRg.Rows
CurrCol = 0
CurrTextStr = ""
For Each CurrCell In CurrRow.Cells
CurrCol = CurrCol + 1
CurrTextStr = CurrTextStr & CurrCell.Value & ListSep
Next
While Right(CurrTextStr, 1) = ListSep
CurrTextStr = Left(CurrTextStr, Len(CurrTextStr) - 1)
Wend
Print #1, CurrTextStr
Next
Close #1
End If
End Sub


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Macro to Export Active Cells, Comma Sep to .txt file

Thank you for your assistance!

Tom Ogilvy wrote:

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
macro to export a range to a text file? [email protected] Excel Programming 4 July 21st 06 02:25 PM
export to csv file (comma problem) Mohan Excel Programming 1 June 16th 06 06:49 PM
How do I export data from Excel as a series of comma-separated val gbergren New Users to Excel 2 January 20th 06 08:17 PM
Macro to export data from active .doc form to an existing .xls Jidings Excel Programming 0 July 29th 05 07:03 PM
Macro to export a worksheet and save as new file Mike_M Excel Programming 2 May 25th 05 10:37 AM


All times are GMT +1. The time now is 02:19 PM.

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

About Us

"It's about Microsoft Excel"