保存为.CSV和.txt

我相信这很简单,但让我难以置信。 我写了下面的代码。 您可以看到我正在将我的薪资决赛和非支付决赛分数保存为CSV文件的地方。 有没有办法让我的文件保存为txt文件。 理想的情况下,如果可能的话在同一个位置 一如既往,您的帮助非常感谢。

Option Explicit Sub BACSConversion() Dim MyNewBook As String Dim MySaveFile As String Dim fileToOpen As Variant Dim fileName As String Dim sheetName As String Dim rCopy As Range 'Turn off display alerts Application.DisplayAlerts = False 'Turn off screen updates Application.ScreenUpdating = False 'Ensures that the file open directory is always the same ChDir "S:\MERIT OUTPUTS FOLDER\MSI Recruitment Limited\" 'Opens the folder to location to select txt file fileToOpen = Application.GetOpenFilename("Text Files (*.txt), *.txt") If fileToOpen <> False Then Workbooks.OpenText fileName:=fileToOpen, _ DataType:=xlDelimited, Tab:=True End If 'Creates the file name based on txt file name fileName = Mid(fileToOpen, InStrRev(fileToOpen, "\") + 1) 'Creates the sheet name based on the active txt file sheetName = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4) 'Save active file as... ActiveWorkbook.SaveAs ("S:\MERIT OUTPUTS FOLDER\MSI Recruitment Limited\BACS File Original\" & _ fileName & ".CSV") 'Selects all data in column A and copies to clipboard Set rCopy = Range("A1", Range("A1").End(xlDown)) 'Open the original document where the BACS file is located Workbooks.Open "S:\Accounts (New)\Management Information (Analysis)\Phil Hanmore - Analysis\bacs conversation calc.xlsx" 'Selects the worksheet called "Original" Sheets("Original").Range("A:A").ClearContents 'Paste selected values from previous sheet rCopy.Copy Sheets("Original").Range("A1").PasteSpecial Paste:=xlPasteValues 'Selects appropriate worksheet - Non-MyPayFINAL Sheets("Non-MyPay FINAL").Select 'Selects all data in column A and copies to clipboard Range("A1", Range("A1").End(xlDown)).Select Selection.Copy 'Add a new workbook Workbooks.Add 'Paste selected values from previous sheet Selection.PasteSpecial Paste:=xlPasteValues 'Build SaveAs file name MySaveFile = Format(Now(), "DDMMYYYY") & "NonMyPayFINAL" & ".CSV" 'Save template file as... ActiveWorkbook.SaveAs ("S:\MERIT OUTPUTS FOLDER\MSI Recruitment Limited\" & MySaveFile) 'Close the new saved file ActiveWorkbook.Close 'Selects appropriate worksheet - MyPayFINAL Sheets("MyPay FINAL").Select 'Selects all data in column A and copies to clipboard Range("A1", Range("A1").End(xlDown)).Select Selection.Copy 'Add a new workbook Workbooks.Add 'Paste selected values from previous sheet Selection.PasteSpecial Paste:=xlPasteValues 'Build SaveAs file name MySaveFile = Format(Now(), "DDMMYYYY") & "MyPayFINAL" & ".CSV" 'Save template file as... ActiveWorkbook.SaveAs ("S:\MERIT OUTPUTS FOLDER\MSI Recruitment Limited\" & MySaveFile) 'Close the new saved file ActiveWorkbook.Close 'Close original source workbook (template) Workbooks("bacs conversation calc").Close 'Turn on display alerts Application.DisplayAlerts = True 'Turn on screen updates Application.ScreenUpdating = True End Sub 

将其保存为.csv文件后,只需重新执行相同的代码,而不是.csv将其更改为.txt

但是这不一定会改变文件的types。 在您的FileFormat:= xlTextWindows命令之后,添加FileFormat:= xlTextWindows 。 这实际上将其保存为文本文件。

它应该看起来像:

ActiveWorkbook.SaveAs ("S:\MERIT OUTPUTS FOLDER\MSI Recruitment Limited\" & MySaveFile), FileFormat:= xlTextWindows

我build议在保存工作簿时也要查找多种不同types的“FileFormats”,以便熟悉它们。 只要searchExcel FileFormats,你应该能够find所有的信息。

更新:

正如在评论中提到的,您保存的CSV文件实际上可能不会保存为逗号分隔符。 要做到这一点,与我上面所说的类似,您可以将初始保存的文件格式更改为xlCSV 。 这可以通过将FileFormat:= xlcsv添加到第一个ActiveWorkbook.SaveAs的末尾来实现

当你真的想要更改文件types而不仅仅是扩展名时,FileFormats是必需的。 这里是链接到MSDN网站解释所有不同的格式。 MSDN XLFileFormats 。 而不是使用文本xlCSVxlTextWindows您也可以使用分配给这些格式的数字值。 而不是FileFormat:= xlCSV你可以做FileFormat:= 6或者你可以做FileFormat:= 20