将csv保存为xlsx

时间:2017-03-14 19:06:21

标签: excel-vba csv vba excel

有人可以帮忙吗?

是否有可能将多个.csv文件转换为xlsx,但保留所有数据?我的意思是当我使用下面的代码时输出xlsx文件搞砸了。没有列所有数据都是文本。

我目前正在使用此

Sub CopyDataFromCSTR()
  Dim wb As Workbook
  Dim myPath As String
  Dim myFile As String
  Dim myExtension As String

  'Optimize Macro Speed
  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual

  'Set folder's path to search within
  myPath = ActiveWorkbook.Path & "\"

  'Target File Extension (must include wildcard "*")
  myExtension = "*.csv*"

  'Target Path with Ending Extention
  myFile = Dir(myPath & myExtension)

  'Loop through each Excel file in folder
  Do While myFile <> ""
    'Set variable equal to opened workbook
    Set wb = Workbooks.Open(Filename:=myPath & myFile)

    'Ensure Workbook has opened before moving on to next line of code
    DoEvents

    wb.SaveAs myPath & Replace(myFile, ".csv", ""), xlOpenXMLWorkbook
    wb.Close False

    'Ensure Workbook has closed before moving on to next line of code
    DoEvents

    'Get next file name
    myFile = Dir
  Loop

  MsgBox ("Done!")

ResetSettings:
  'Reset Macro Optimization Settings
  Application.EnableEvents = True
  Application.Calculation = xlCalculationAutomatic
  Application.ScreenUpdating = True

  'Release garbage
  Set wb = Nothing
End Sub

0 个答案:

没有答案