Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

excel to text 1

Status
Not open for further replies.

into

Technical User
Dec 16, 2002
17
i want each row col a and b with data to export to textfile.

smaple txt file
A1
B1
A2
B2

for as long as there is data in A column

could someone help me get started.

this code is nice but not enough becouse it relyes on a selection.

Code:
Option Explicit
 
 Sub Export()

   Dim fsoObject As Object
   
   Dim fsoFile As Object

   Dim rnCell As Range

   Dim vaFilename As Variant

   Dim lnRows As Long

    Dim fsoObj As Object

    Set fsoObject = CreateObject("Scripting.FilesystemObject")


    vaFilname = Application.GetSaveAsFilename("c:\xl.txt", "Text File (*.txt),*.txt,ASCII File (*.asc),*.asc", 1, "export")
                      

   If vaFilnamn = False Then

      MsgBox "No filename", vbInformation

      Exit Sub

   End If

 

   'Set fsoObject = New Scripting.FileSystemObject

   Set fsoFil = fsoObject.CreateTextFile(vaFilnamn, True)

 

   lnRows = 0

 
   For Each rnCell In Selection.Cells

      If rnCell.Row <> lnRows Then
                
              If lnRows <> 0 Then
                
            
            fsoFil.write Chr(13) & Chr(10)

         End If

        lnRows = rnCell.Row
        
         
      End If

      If Left(rnCell.Address, 2) = "$A" Then
      
        fsoFile.write "11"

        fsoFile.write rnCell.Value
        
        fsoFile.write Chr(13) & Chr(10)
    
      ElseIf Left(rnCell.Address, 2) = "$B" Then
        
        fsoFile.write "#12343;"

        fsoFile.write rnCell.Value
        
        fsoFile.write Chr(13) & Chr(10)
    
      End If

   Next rnCell

 

   fsoFile.Close

   Set fsoObject = Nothing

End Sub
 
change selection to usedrange ?

_________________
Bob Rashkin
 
Code:
Sub Export()
   Dim path_name As String, file_name As String, i As Long, last_row As Long
   
   path_name = "C:\"
   file_name = "xl.txt"
   
   last_row = ActiveSheet.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row

   Open path_name & file_name For Output As #1
      For i = 1 To last_row
         Print #1, Trim(Cells(i, 1))
         Print #1, Trim(Cells(i, 2))
      Next i
   Close #1
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top