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
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
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:
Upvote 0
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).
 
Upvote 0
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
 
Upvote 0
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:
Upvote 0

Forum statistics

Threads
1,213,515
Messages
6,114,080
Members
448,548
Latest member
harryls

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
Back
Top