I am a beginner in excel macro, and hope to find an answer to my question in this great forum.
I want to create APRIL.txt file from the data in my spreadsheet.
The sample Sheet is attached here with.
I am a beginner in excel macro, and hope to find an answer to my question in this great forum.
I want to create APRIL.txt file from the data in my spreadsheet.
The sample Sheet is attached here with.
My English is very poor, so please be patient >_<"
Thanks & Regards.
hkbhansali
If I may ask, what is the complication about this? I mean, if it's me I would just copy that and paste into a notepad and save.
多么想要告诉你 我好喜欢你
This question's been asked a few times recently - so I've written a quick post to cover it:
http://excel.solutions/2014/04/using...-to-text-file/
let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source
If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE
Walking the tightrope between genius and eejit...
Here's another..
Change file path to suit..
![]()
Private Sub CommandButton1_Click() Dim Rng As Range With CreateObject("Scripting.FileSystemObject").CreateTextFile("c:\April.txt", True) Set Rng = Cells(1, 1).Resize(Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row, 1) .writeline Join(Application.Transpose(Rng), vbCrLf) End With End Sub
yaa, it s work,
Thanks a lot all above my welwisher,
Hi apo,
will u add code if any row in a column A is mark "X" can skip in above code?
You can easily adapt my code to do that - see highlights in red:
![]()
Sub WriteTextFile() Dim rng As Range, lRow As Long Dim stOutput As String, stNextLine As String, stSeparator As String Dim stFilename As String, stEncoding As String Dim fso As Object '------------------------------------------------------------------------------------- 'CHANGE THESE PARAMETERS TO SUIT Set rng = ActiveSheet.UsedRange 'this is the range which will be written to text file stFilename = "C:\Temp\TextOutput.txt" 'this is the text file path / name stSeparator = vbTab 'e.g. for comma seperated value, change this to "," stEncoding = "UTF-8" 'e.g. "UTF-8", "ASCII" '------------------------------------------------------------------------------------- For lRow = 1 To rng.Rows.Count If rng.Cells(lRow, 1).Value <> "X" Then If rng.Columns.Count = 1 Then stNextLine = rng.Rows(lRow).Value Else stNextLine = Join$(Application.Transpose(Application.Transpose(rng.Rows(lRow).Value)), stSeparator) End If If stOutput = "" Then stOutput = stNextLine Else stOutput = stOutput & vbCrLf & stNextLine End If End If Next lRow Set fso = CreateObject("ADODB.Stream") With fso .Type = 2 .Charset = stEncoding .Open .WriteText stOutput .SaveToFile stFilename, 2 End With Set fso = Nothing End Sub
Hi olly,
Thanks for prompt reply,
its work fine,
hi olly,
your code is perfect but its generate all column while i want only column "B" data only
Come on, you need to start understanding and tweaking the code to suit your workbook.
Change the line highlighted in blue, to the range you actually want this to apply to:
So in your case, you may change this to something like:![]()
Sub WriteTextFile() Dim rng As Range, lRow As Long Dim stOutput As String, stNextLine As String, stSeparator As String Dim stFilename As String, stEncoding As String Dim fso As Object '------------------------------------------------------------------------------------- 'CHANGE THESE PARAMETERS TO SUIT Set rng = ActiveSheet.UsedRange 'this is the range which will be written to text file stFilename = "C:\Temp\TextOutput.txt" 'this is the text file path / name stSeparator = vbTab 'e.g. for comma seperated value, change this to "," stEncoding = "UTF-8" 'e.g. "UTF-8", "ASCII" '------------------------------------------------------------------------------------- For lRow = 1 To rng.Rows.Count If rng.Cells(lRow, 1).Value <> "X" Then If rng.Columns.Count = 1 Then stNextLine = rng.Rows(lRow).Value Else stNextLine = Join$(Application.Transpose(Application.Transpose(rng.Rows(lRow).Value)), stSeparator) End If If stOutput = "" Then stOutput = stNextLine Else stOutput = stOutput & vbCrLf & stNextLine End If End If Next lRow Set fso = CreateObject("ADODB.Stream") With fso .Type = 2 .Charset = stEncoding .Open .WriteText stOutput .SaveToFile stFilename, 2 End With Set fso = Nothing End Sub
![]()
Set rng = ActiveSheet.Range ("B1:B" & Cells(Rows.Count, "B").End(xlUp).Row)
Hi,olly, & apo
THANKS,
my problem solved, thanks for kind help.
Hi,olly, & apo
THANKS,
my problem solved, thanks for kind help.
Hi..
Given that all your data is in 1 column.. this should do..
![]()
Private Sub CommandButton1_Click() Dim Rng As Range With CreateObject("Scripting.FileSystemObject").CreateTextFile("c:\April.txt", True) For i = 1 To Range("A" & Rows.Count).End(xlUp).Row Set Rng = Cells(i, 1) If Not Split(Cells(i, 1).Value, ",")(0) Like "*x*" Then .writeline Rng Next i End With End Sub
Hi, apo
u r right my data is in 1 column only and its "B" column,
above code is not working.
Olly is right.. you should step through any code given to you in order to understand what it does.. and at least attempt to reverse engineer it..
Minor changes that were needed in my code are shown in red..
![]()
Private Sub CommandButton1_Click() Dim Rng As Range With CreateObject("Scripting.FileSystemObject").CreateTextFile("c:\April.txt", True) For i = 1 To Range("B" & Rows.Count).End(xlUp).Row Set Rng = Cells(i, 2) If Not Split(Cells(i, 2).Value, ",")(0) Like "*x*" Then .writeline Rng Next i End With End Sub
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks