VBA copies only first row to text file

41 views Asked by At

I am looking to copy non-blank column data into a text file, while the copy is successful, the paste part of the vba copies only first row to the text file. Below are the issues

1.Though copy of the entire column is successful, the copy also has some blank cells (but has formulae), excel copies those cells too. I would like to ignore cells having only formulae.
2.Paste part of the code pastes pastes only first row of the copied column

Sub Addtotext() 'Adds stocks to stockslist for python
Dim strPath As String
Dim strName As String
Dim FSO As Object
Dim oFile As Object
Dim c As Range

With Range("J2", Range("J" & Rows.Count).End(xlUp)).Copy
End With

strName = "a.txt"
strPath = "C:\Volume\PythonProjects"

Set FSO = CreateObject("Scripting.FileSystemObject")


Set oFile = FSO.CreateTextFile(strPath & strName)

For Each c In Selection
    oFile.Write c.Value & " "
Next c

oFile.Close
End Sub
0

There are 0 answers