• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Create Text File and Folder and move the text file to the folder

Amazon777

New Member
I was studying this code from Chandoo. My current project is to take some Excel ranges and put them in a text file. The post below is perfect.
I would like to expand this by moving the text file to a folder that is created by the macro.

I successfully created a folder and using the code below created a text file.

Issue:
I cannot create the code to get the text file to move to the folder.

Thanks for your help


https://chandoo.org/wp/save-range-as-text-using-vba/

>>> use code - tags <<<
Code:
Sub saveText2()  

Dim filename As String, lineText As String   
Dim myrng As Range, i, j      


filename = ThisWorkbook.Path & "\textfile-" & Format(Now, "ddmmyy-hhmmss") & ".txt"      


Open filename For Output As #1       

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 #1, lineText  
    Next I       
Close #1

End Sub
 

Attachments

  • code.txt
    6.4 KB · Views: 1
Last edited by a moderator:
.
Code:
Option Explicit

Sub MakeMyFolder()
    Dim fd As Object
    Application.ScreenUpdating = False
    Set fd = CreateObject("Scripting.FileSystemObject")
    If fd.FolderExists(ThisWorkbook.Path & "\Test Folder") Then
        MsgBox "Folder Exists", vbInformation, "Success"
    Else
        fd.CreateFolder (ThisWorkbook.Path & "\Test Folder")
        MsgBox "Folder created.", vbInformation, "Completedl"
    End If
    Application.ScreenUpdating = True
    
    saveText2
    
End Sub

Sub saveText2()

Dim filename As String, lineText As String
Dim myrng As Range, i, j


filename = ThisWorkbook.Path & "\Test Folder\TextFile-" & Format(Now, "ddmmyy-hhmmss") & ".txt"


Open filename For Output As #1

Set myrng = Range("A1:F15")
    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 #1, lineText
    Next i
Close #1

End Sub
 

Attachments

  • Text.xlsm
    17.9 KB · Views: 9
Back
Top