VBA for saving values in one column to text file

noslenwerd

Board Regular
Joined
Nov 12, 2019
Messages
85
Office Version
  1. 365
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
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
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:
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,893
Members
449,097
Latest member
dbomb1414

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