Making a text file with identical name & Path

anichols

New Member
Joined
Mar 11, 2021
Messages
39
Office Version
  1. 365
Platform
  1. Windows
Hello all!

I have some VBA that is creating a pipe delimited text file, but I am struggling to get the name correct. I have managed to save it in the same location, but attempting to name it the same as the excel file has failed. Any help would be greatly appreciated.

VBA Code:
Sub PipeDelimited() 
Dim thisWb As Workbook
Set thisWb = ActiveWorkbook

Open thisWb.Path & "\new workbook.txt" For Output As 1  'Change this path



rowno = 1
colcount = Application.CountA(ActiveSheet.Rows(1))
While ActiveSheet.Cells(rowno, 1) <> ""
    dataout = "" 'start of line
    For C = 1 To colcount
        If C <> colcount Then
            dataout = dataout & "" & Trim(ActiveSheet.Cells(rowno, C)) & "|" 
        Else
            dataout = dataout & "" & Trim(ActiveSheet.Cells(rowno, C)) & ""
        End If
    Next C
    Print #1, dataout
    rowno = rowno + 1
Wend
Close #1
End Sub

Thank you,

Drew
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

anichols

New Member
Joined
Mar 11, 2021
Messages
39
Office Version
  1. 365
Platform
  1. Windows
I have tried this, hoping to get closer, but I get run time error 438.

VBA Code:
Open thisWb.Path & "\" & thisWb & ".txt" For Output As 1  'Change this path
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,745
VBA Code:
    Open Left(thisWb.FullName, InStrRev(thisWb.FullName, ".")) & "txt" For Output As #1
 
Solution

anichols

New Member
Joined
Mar 11, 2021
Messages
39
Office Version
  1. 365
Platform
  1. Windows
Awesome! It works perfectly!
Thank you sir!
 

Watch MrExcel Video

Forum statistics

Threads
1,129,297
Messages
5,635,376
Members
416,856
Latest member
silentir

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
Top