另存为CSV保存公式为#NAME的公式?

时间:2016-09-01 07:15:01

标签: vba excel-vba excel

我在此报告中有一个模块功能,用于连接某些单元格:

Function AnalysisResults(Specs As Range, Results As Range)
    AnalysisResults = Join(Specs) & ";" & Replace(Join(Results), ",", ".")
End Function

Private Function Join(Range As Range, Optional ByRef Delimeter As String = " ")
    Dim Str As String
    For Each cell In Range
        If Str <> "" Then Str = Str & Delimeter
        Str = Str & cell.Value
    Next

    Join = Str
End Function

然后我有一个命令按钮,将我的文件保存为CSV。它有效,但功能单元格保存的值是 #NAME ?。 如果我手动另存为CSV逗号分隔,则会正确保存并显示公式值。

以下是CommandButton的代码:

   Dim myValue As Variant
myValue = InputBox("Specifica numele WBT-ului de descarcare:", "Save WBT with the following name", 1)
Range("L2").Value = myValue
Dim CopyToCSV()
Dim MyPath As String
Dim MyFileName As String
Sheets("Manual Discharge WBT").EnableCalculation = True
MyPath = "\\FILES\Transfer\~~TTS Import (do not delete)~~\Import Files\"
MyFileName = Range("L2") & " Discharge " & Format(CStr(Now), " dd_mm_yyyy_hh_mm")
If Not Right(MyPath, 1) = "\" Then MyPath = MyPath & "\"
If Not Right(MyFileName, 4) = ".csv" Then MyFileName = MyFileName & ".csv"
Sheets("Manual Discharge WBT").Copy
With ActiveWorkbook
    .SaveAs Filename:= _
        MyPath & MyFileName, _
        FileFormat:=xlCSVWindows, _
        CreateBackup:=False
    .Close False
     MsgBox "Fisierul tau s-a salvat ca: " & MyFileName
End With

1 个答案:

答案 0 :(得分:2)

解决方法是,您必须将AnalysisResultsJoin个函数另存为加载项文件.xlam,然后在Developer->Add-ins添加到Excel。这样做,上面的代码在没有#NAME?

的情况下保存到CSV时对我有用

这里发生的事情是,当excel尝试将文件保存为CSV时,它不知道AnalysisResults函数是什么。