【问题标题】:Export Excel to csv and deleting the last empty cells of a row将 Excel 导出到 csv 并删除一行的最后一个空单元格
【发布时间】:2018-06-24 09:02:38
【问题描述】:

我正在尝试编写一个宏来将 Excel 导出为 CSV,但我对 vba 很陌生。 我现在拥有的是:

Option Explicit

Sub ExportAsCSV()

    Dim MyFileName As String
    Dim CurrentWB As Workbook, TempWB As Workbook

    Set CurrentWB = ActiveWorkbook
    ActiveWorkbook.ActiveSheet.UsedRange.Copy

    Set TempWB = Application.Workbooks.Add(1)
    With TempWB.Sheets(1).Range("A1")
      .PasteSpecial xlPasteValues
      .PasteSpecial xlPasteFormats
    End With        

    Dim Change below to "- 4"  to become compatible with .xls files
    MyFileName = CurrentWB.Path & "\" & Left(CurrentWB.Name, Len(CurrentWB.Name) - 5) & ".csv"

    Application.DisplayAlerts = False
    TempWB.SaveAs Filename:=MyFileName, FileFormat:=xlCSV, CreateBackup:=False, Local:=True
    TempWB.Close SaveChanges:=False
    Application.DisplayAlerts = True
End Sub

来自Excel: macro to export worksheet as CSV file without leaving my current Excel sheet

哪个是正确的 csv,但我正试图摆脱多余的“;”在行的末尾。请问有什么办法吗?

例子:

1|2|3|4|5
2|5|3
2| |5
3

出口会给:

1;2;3;4;5
2;5;3;;
2;;5;;
3;;;;

我想要的是:

1;2;3;4;5
2;5;3
2;;5
3

这可能吗?

非常感谢您的帮助

编辑: 这是给出答案后编辑的代码:

Option Explicit
Public Function RemoveTrailing(s As String) As String
  Dim nIndex As Integer

  For nIndex = Len(s) To 1 Step -1
    If Right$(s, 1) = ";" Then
      s = Left$(s, Len(s) - 1)
    End If
  Next
  RemoveTrailing = s

End Function

Sub ExportAsCSV()

    Dim MyFileName As String
    Dim CurrentWB As Workbook, TempWB As Workbook

    Set CurrentWB = ActiveWorkbook
    ActiveWorkbook.ActiveSheet.UsedRange.Copy

    Set TempWB = Application.Workbooks.Add(1)
    With TempWB.Sheets(1).Range("A1")
        .PasteSpecial xlPasteValues
        .PasteSpecial xlPasteFormats
    End With

    'Dim Change below to "- 4"  to become compatible with .xls files
    MyFileName = CurrentWB.Path & "\" & Left(CurrentWB.Name, Len(CurrentWB.Name) - 5) & ".csv"

    Application.DisplayAlerts = False
    TempWB.SaveAs Filename:=MyFileName, FileFormat:=xlCSV, CreateBackup:=False, Local:=True
    TempWB.Close SaveChanges:=False
    Application.DisplayAlerts = True
    Dim sFile2 As String
      Dim sLine As String

      sFile2 = Replace(MyFileName, ".csv", "2.csv")
      Open MyFileName For Input As #1
      Open sFile2 For Output As #2

      Do Until EOF(1)
        Line Input #1, sLine
        Print #2, RemoveTrailing(sLine)
      Loop

      Close #1
      Close #2
End Sub

但我收到错误“类型不兼容”

【问题讨论】:

    标签: vba excel csv


    【解决方案1】:

    这是一个删除尾随分号的函数:

    Public Function RemoveTrailing(s As String) As String
      Dim nIndex As Integer
    
      For nIndex = Len(s) To 1 Step -1
        If Right$(s, 1) = ";" Then
          s = Left$(s, Len(s) - 1)
        End If
      Next
      RemoveTrailing = s
    
    End Function
    

    这里有一个更短的代码 sn-p 做同样的事情:

    Public Function RemoveTrailing2(s As String) As String
      Do Until Right$(s, 1) <> ";"
        s = Left$(s, Len(s) - 1)
      Loop
      RemoveTrailing2 = s
    End Function
    

    这是您如何使用上述功能。基本上,它的作用是逐行读取 CSV 文件,并将每一行输出到一个新文件,该文件去除了 ; 字符。将此添加到您日常工作的最后:

      Dim sFile2 As String
      Dim sLine As String
    
      sFile2 = Replace(MyFilename, ".csv", "2.csv")
      Open MyFilename For Input As #1
      Open sFile2 For Output As #2
    
      Do Until EOF(1)
        Line Input #1, sLine
        Print #2, RemoveTrailing(sLine)
      Loop
    
      Close #1
      Close #2
    

    新文件将具有相同的文件名,文件名末尾带有2

    【讨论】:

    • 谢谢你的回答,你能告诉我在代码中哪里可以使用它吗? ActiveWorkbook.ActiveSheet.UsedRange.Copy ?
    • 我添加了一个简短的例程,它使用该函数去除;
    • 我将函数放在 End sub 之前,但它不起作用
    • 说“它不起作用”并不能提供有关您遇到的问题的任何信息。错误是什么,错误发生在哪一行?
    • 当我不放函数时,生成的csv带有额外的“;” ,但是当我添加另一个时,根据我放置代码的位置出现“工作表已打开”或“类型不兼容”的错误
    猜你喜欢
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多