Xls to txt change required in code

pavanranjan

Board Regular
Joined
Sep 20, 2008
Messages
50
AVAILABLE CODE AS FOLLOWS

Sub pavanranjan()

Dim i As Long
Dim lr As Long
Dim x As Workbook
Dim r As Variant

Set x = ThisWorkbook

lr = Cells(Rows.Count, 1).End(xlUp).Row

For i = lr To 2 Step -1

r = x.Sheets("Sheet1").Range("A" & i).Value

Rows("1:1").Copy

With Workbooks.Add

.ActiveSheet.Paste


.SaveAs Filename:="D:\Common\data\IBMmain" & r & ".xls", _
FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False

x.Sheets("Sheet1").Range("A" & i).EntireRow.Copy

.Sheets("Sheet1").Range("A" & Rows.Count).End(3)(2).Select

ActiveSheet.Paste

End With


Next i


End Sub
 

Some videos you may like

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,974
Office Version
  1. 365
Platform
  1. Windows
So, what's the question?

If it is just what the code would look like to save as a text file instead of an Excel file, you can use the Macro Recorder and record yourself saving as a text file, and you will see the code that you need.
 
Last edited:

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,974
Office Version
  1. 365
Platform
  1. Windows
i need change in above code which changes xl type to TXT file
Did you try my suggestion?
If it is just what the code would look like to save as a text file instead of an Excel file, you can use the Macro Recorder and record yourself saving as a text file, and you will see the code that you need.
Note that there are many different type of text files (fixed width, tab-delimited, etc), so you need to be sure that you select the appropriate file format type when doing a Save As (you didn't mention what type of text file you are trying to create).
 

pavanranjan

Board Regular
Joined
Sep 20, 2008
Messages
50

ADVERTISEMENT

i dont know how to do it,i was looking for a change in above code.

regards
 

veyselemre

Board Regular
Joined
Mar 16, 2006
Messages
76
Office Version
  1. 2010
Platform
  1. Windows
Do you want to make each line a separate text file?
Code:
Sub pavanranjan()

    Dim i As Long
    Dim lr As Long
    Dim x As Workbook
    Dim xS As Worksheet
    Dim r As String
    Dim b As Range


    Set x = ActiveWorkbook
    Set xS = x.Sheets("Sheet1")
    Set b = xS.Range("1:1")


    lr = xS.Cells(Rows.Count, 1).End(xlUp).Row


    For i = lr To 2 Step -1
        b.Copy
        Workbooks.Add
        With ActiveSheet
            .Paste
            r = xS.Range("A" & i).Value
            xS.Range("A" & i).EntireRow.Copy .Cells(2, 1)
            ActiveWorkbook.SaveAs Filename:="D:\Common\data\IBMmain\" & r & ".TXT", FileFormat:=xlText, CreateBackup:=False
        End With
        ActiveWorkbook.Close False
    Next i
    
    Set x = Nothing
    Set xS = Nothing
    Set b = Nothing
End Sub
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,974
Office Version
  1. 365
Platform
  1. Windows
i dont know how to do it

It is very easy, and a very get trick to learn. You can get tons of VBA code this way.
See:
https://www.excel-easy.com/vba/examples/macro-recorder.html

i was looking for a change in above code.
You still haven't addressed what type of text file format you are looking for (see my previous comments on the different types of text files).
If you don't tell us that, we don't know which code to provide you (we could "guess", but we might guess wrong, which wouldn't help you).
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,123,269
Messages
5,600,629
Members
414,396
Latest member
rinianjell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top