【问题标题】:excel macro save sheets as csv with specific delimiter and enclosureexcel宏将工作表另存为带有特定分隔符和附件的csv
【发布时间】:2012-12-13 11:50:45
【问题描述】:

对于 vb 和 excel,我完全是个傻瓜,我试图将我在这里找到的 2 个宏组合成 1 个,但显然做了一些非常错误的事情,现在我被卡住了.. 首先我只是使用了这个宏(保存为personal.xlsb,以便在任何工作簿中使用)

    Sub CSVFile()

    Dim SrcRg As Range
    Dim CurrRow As Range
    Dim CurrCell As Range
    Dim CurrTextStr As String
    Dim ListSep As String
    Dim FName As Variant
    FName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv")

    ListSep = ";"
      If Selection.Cells.Count > 1 Then
        Set SrcRg = Selection
      Else
        Set SrcRg = ActiveSheet.UsedRange
      End If
    Open FName For Output As #1
    For Each CurrRow In SrcRg.Rows
      CurrTextStr = ìî
    For Each CurrCell In CurrRow.Cells
      CurrTextStr = CurrTextStr & """" & GetUTF8String(CurrCell.Value) & """" & ListSep
    Next
    While Right(CurrTextStr, 1) = ListSep
      CurrTextStr = Left(CurrTextStr, Len(CurrTextStr) - 1)
    Wend
    Print #1, CurrTextStr
    Next
    Close #1
    End Sub

那加上 GetUTF8String 函数代码。现在一切正常。然后我想好了为什么不只是尝试我有限的(这是一个严重的轻描淡写)vb理解,添加以下代码并将CSVFile子更改为一个函数,然后我从下面的子调用它,输出文件名为一个参数(用于代替 FName = Application.GetSaveAsFilename)。我想是的,这段代码会自动保存所有工作表,现在让我们确保在保存每张工作表之前运行编码和分隔符/附件设置功能。这似乎不对,但我想嘿为什么不试试..

Public Sub SaveAllSheetsAsCSV()
On Error GoTo Heaven

' each sheet reference
Dim Sheet As Worksheet
' path to output to
Dim OutputPath As String
' name of each csv
Dim OutputFile As String

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False

' Save the file in current director
OutputPath = ThisWorkbook.Path


If OutputPath <> "" Then
Application.Calculation = xlCalculationManual

' save for each sheet
For Each Sheet In Sheets

    OutputFile = OutputPath & Application.PathSeparator & Sheet.Name & ".csv"

    ' make a copy to create a new book with this sheet
    ' otherwise you will always only get the first sheet

    Sheet.Copy
    ' this copy will now become active
     CSVFile(OutputFile)
     ActiveWorkbook.SaveAs Filename:=OutputFile, FileFormat:=xlCSV,     CreateBackup:=False
    ActiveWorkbook.Close
Next

Application.Calculation = xlCalculationAutomatic

End If

Finally:
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True

Exit Sub

Heaven:
MsgBox "Couldn't save all sheets to CSV." & vbCrLf & _
        "Source: " & Err.Source & " " & vbCrLf & _
        "Number: " & Err.Number & " " & vbCrLf & _
        "Description: " & Err.Description & " " & vbCrLf

GoTo Finally
End Sub

保存了它,我已经成功地实现了一些非常不同的东西。在打开任何工作簿时,该宏会运行并将该特定工作簿中的工作表打开为 csv 文件(不保存它们)。现在我就像爱丽丝梦游仙境。它为什么在文件打开时运行?这是不可取的,所以我回到宏代码并将其更改回只是 csvfile 子。好吧,这没有帮助,不知道我在那里做了什么,肯定是在编辑同一个宏......所以我删除了宏,模块,我无法想象现在的东西在哪里,但它仍在运行 + 我收到这个警告宏被停用。无法摆脱它!现在小伙子们,我很抱歉我完全缺乏专业性,这只是对客户的一个小忙,没有浪费大量时间学习vb,因为我的老板不喜欢那样......我我当然对如何在其中设置分界符和附件后实现自动保存工作表的目标感兴趣。而此刻我对如何摆脱那个宏以及它隐藏在哪里非常感兴趣。我做了什么?!感谢您的耐心等待!

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    我认为问题出在这条线上

    OutputPath = ThisWorkbook.Path
    

    因为您是从存储在 XLSTART 文件夹中的 personal.xlsb 运行它,所以它在同一位置创建了 CSV 文件。当 Excel 启动时,它会尝试加载在该位置找到的所有文件。

    只需找到您的 XLSTART 文件夹并删除您在其中找到的所有 CSV 文件。

    尝试使用

    OutputPath = ActiveWorkbook.Path
    

    XLSTART 文件夹位置,取决于您的系统,可能类似于:

    C:\Users\YOURNAME\AppData\Roaming\Microsoft\Excel\XLSTART
    

    【讨论】:

      猜你喜欢
      • 2012-03-16
      • 2020-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-06
      • 2012-06-03
      • 2011-11-11
      相关资源
      最近更新 更多