save range to notepad txt file. VBA

davewatson86

New Member
Joined
Jul 8, 2019
Messages
30
G'Day all

i am trying to loop through some worksheets and export the data into a txt file.

the below code works but i need it to save the file in the specified location rather than open it.

anyone able to help me out?

many thanks
Dave

Code:
Sub CrText()
Dim c00            As Variant
Dim textFilePath, File_Name As String
Dim lngCounter     As Long
Dim FF             As Integer
 
 i = 1
 Do Until i = 13
    
            
            If i = 1 Then Set WS = WS_1_Con
            If i = 1 Then File_Name = WS_Admin.Range("C2").Value & "1C"
            If i = 2 Then Set WS = WS_1_FCon
            If i = 2 Then File_Name = WS_Admin.Range("C2").Value & "1F"
            If i = 3 Then Set WS = WS_2_Con
            If i = 3 Then File_Name = WS_Admin.Range("C2").Value & "2C"
            If i = 4 Then Set WS = WS_2_FCon
            If i = 4 Then File_Name = WS_Admin.Range("C2").Value & "2F"
            If i = 5 Then Set WS = WS_3_Con
            If i = 5 Then File_Name = WS_Admin.Range("C2").Value & "3C"
            If i = 6 Then Set WS = WS_3_FCon
            If i = 6 Then File_Name = WS_Admin.Range("C2").Value & "3F"
            If i = 7 Then Set WS = WS_4_Con
            If i = 7 Then File_Name = WS_Admin.Range("C2").Value & "4C"
            If i = 8 Then Set WS = WS_4_FCon
            If i = 8 Then File_Name = WS_Admin.Range("C2").Value & "4F"
            If i = 9 Then Set WS = WS_5_Con
            If i = 9 Then File_Name = WS_Admin.Range("C2").Value & "5C"
            If i = 10 Then Set WS = WS_5_FCon
            If i = 10 Then File_Name = WS_Admin.Range("C2").Value & "5F"
            If i = 11 Then Set WS = WS_6_Con
            If i = 11 Then File_Name = WS_Admin.Range("C2").Value & "6C"
            If i = 12 Then Set WS = WS_6_FCon
            If i = 12 Then File_Name = WS_Admin.Range("C2").Value & "6F"
            
            textFilePath = CStr(VBA.CurDir) & "\" & File_Name & ".txt"




            WS.Activate
            FF = VBA.FreeFile
            c00 = Range("A3", Range("A" & Rows.Count).End(xlUp).Offset(0, 14))
            
            Open textFilePath For Output As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FF]#FF[/URL] 
                For lngCounter = LBound(c00, 1) To UBound(c00, 1)
                    Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FF]#FF[/URL] , Join(Application.Index(c00, lngCounter, 0), "|")
                Next
            Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FF]#FF[/URL] 
            Shell "C:\WINDOWS\notepad.exe " & textFilePath, vbNormalFocus
            
            i = i + 1
    
        Loop


End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Forum statistics

Threads
1,213,552
Messages
6,114,278
Members
448,559
Latest member
MrPJ_Harper

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