VBA to save as tab delimited file

br_meyers

New Member
Joined
Feb 13, 2018
Messages
2
Hi all,

I've seen this topic discussed, but not sufficient to answer my question. Your help is appreciated.

My goal is to export a worksheet to a new workbook, save the new workbook in a specific location with a specific file name. The next part is where I'm struggling. I then want to take the open workbook that has been saved and "save as" a tab delimited file in the same location with the same name as the recently saved excel file. For example, if the Excel file has been saved as "CRD_Upload_02_13_18_09_08", I want the tab delimited .txt file to also save in this location with this same file name. I need the VBA code to be dynamic (ie, I don't want to put the exact location and filename within the code). I want it to take the name and location of the current file and just save it as a tab delimited file.

The code I'm using is below and ignores the tab delimited .txt step.

Note below: "Y1" is the filepath: \\john\root\shared\modelo\Diversified\Calabrese\Central Port\

Sub Save_As()
Sheets("Trade Sheet Source").Select
Dim FP As String, dt As String, wbNam As String
FP = Range("Y1").Value
wbNam = "CRD_Upload_"
dt = Format(CStr(Now), "mm_dd_yy_hh_mm")
ActiveWorkbook.SaveAs filename:=FP & wbNam & dt


Rows("1:1").Select
Selection.Delete Shift:=xlUp
ActiveWorkbook.Save

End Sub

Thank you.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi

I think this does what you are after, and closes the TAB delimited file once saved, and I've commented out some of the latter statements which you may need for other purposes (so you may need to adapt further)?

Code:
Sub Save_As()
'Get file path
Path = ThisWorkbook.Path
'FullName = ThisWorkbook.FullName
'Get file Name
Fname = ThisWorkbook.Name
'remove extension based on location of ".", so could go wrong if file name contains a dot!
Just_name = Left(Fname, InStr(Fname, ".") - 1)


'turn off any prompts from file save
Application.DisplayAlerts = False


'Save as text / tab delimited
ActiveWorkbook.SaveAs Filename:=Path + "\" + Just_name + ".txt", FileFormat:=xlText


'turn prompts back on
Application.DisplayAlerts = True


'close our text file (as we have saved it!)
ActiveWorkbook.Close savechanges:=False


'Rows("1:1").Select
'Selection.Delete Shift:=xlUp
'ActiveWorkbook.Save


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

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