VBA for saving values in one column to text file

noslenwerd

New Member
Joined
Nov 12, 2019
Messages
49
Hello,

New to the forum. I've learned a lot just reading.

Wondering if someone can help me with the VBA code below. It DOES save the whole worksheet to a text file, but I would like it to also
  • Only save content in Column K
  • Skip over blank cells in Column K

Code:
Sub saveText()
    ActiveWorkbook.SaveAs FileName:= _
        ThisWorkbook.Path & "\textfile-" & Format(Now, "ddmmyy-hhmmss") & ".txt", FileFormat:=xlText, _
        CreateBackup:=False
End Sub


Sub saveText2()
    Dim FileName As String, lineText As String
    Dim myrng As Range, i, j
    
    FileName = ThisWorkbook.Path & "\isell-notes-" & Range("B6") & Format(Now, "mmddyy-hhmmss") & ".txt"
    
    Open FileName For Output As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
    
    Set myrng = Range("data")
    
    For i = 1 To myrng.Rows.Count
        
        For j = 1 To myrng.Columns.Count
            lineText = IIf(j = 1, "", lineText & ",") & myrng.Cells(i, j)
        Next j
        Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , lineText
    Next i
    
    Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
End Sub
 

Some videos you may like

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

rpaulson

Well-known Member
Joined
Oct 4, 2007
Messages
1,100
this will output Column K to a text file

Rich (BB code):
Sub Create_Txt_File()

myfile = ThisWorkbook.Path & "\isell-notes-" & Range("B6") & Format(Now, "mmddyy-hhmmss") & ".txt"

If Dir(myfile) <> "" Then Kill myfile ' deletes file if it exists
Data = ""

For r = 1 To Cells(Rows.Count, "K").End(xlUp).Row
   If Cells(r, "K") <> "" Then Data = Data & Cells(r, "K") & vbCr
Next r


Open myfile For Output As #1 
Print #1 , Data
Close #1 

End Sub
hth,

Ross
 
Last edited:

noslenwerd

New Member
Joined
Nov 12, 2019
Messages
49
this will output Column K to a text file

Rich (BB code):
Sub Create_Txt_File()

myfile = ThisWorkbook.Path & "\isell-notes-" & Range("B6") & Format(Now, "mmddyy-hhmmss") & ".txt"

If Dir(myfile) <> "" Then Kill myfile ' deletes file if it exists
Data = ""

For r = 1 To Cells(Rows.Count, "K").End(xlUp).Row
   If Cells(r, "K") <> "" Then Data = Data & Cells(r, "K") & vbCr
Next r


Open myfile For Output As #1 
Print #1 , Data
Close #1 

End Sub
hth,

Ross
And that did the trick! Thank you so much.
 

Watch MrExcel Video

Forum statistics

Threads
1,099,020
Messages
5,466,091
Members
406,464
Latest member
buks1232000

This Week's Hot Topics

Top