另存为.CSV和.txt

时间:2017-07-05 11:53:33

标签: excel vba excel-vba

我确信这很简单,但让我感到困惑不已。我写了下面的代码。你可以看到我正在保存我的支付最终和非mypay Final作为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

1 个答案:

答案 0 :(得分:3)

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

但这不一定会改变文件的类型。在Activeworkbook.SaveAs命令后,添加FileFormat:= xlTextWindows。这实际上将它保存为文本文件。

应该看起来像:

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

我建议也查找多种不同类型的&#34; FileFormats&#34;在保存工作簿时,只是为了熟悉它们。只需搜索Excel FileFormats,您就应该能够找到有关所有这些信息的信息。

更新

正如评论中所述,您保存的CSV文件实际上可能无法以逗号分隔。为此,与上述内容类似,您可以将初始保存的文件格式更改为xlCSV。这可以通过将FileFormat:= xlcsv添加到您的第一个ActiveWorkbook.SaveAs

的末尾来实现

当您真正有兴趣更改文件类型而不仅仅是扩展名时,需要FileFormats。以下是MSDN网站的链接,解释了所有不同的格式。 MSDN XLFileFormats。您也可以使用分配给这些格式的数值,而不是使用文本xlCSVxlTextWindows。代替FileFormat:= xlCSV您可以FileFormat:= 6xlTextWindows代替FileFormat:= 20