通过VBA将Excel工作表导出为CSV,并显示公式结果

时间:2019-03-04 12:27:37

标签: excel vba

我有一本具有多个工作表的excel工作簿,我正在尝试将特定工作表导出为CSV文件并将其保存到用户桌面,而不会影响原始工作表。一切似乎正常,但是文件以PDF格式发布,有什么想法吗?代码如下:

Sub CSVSagePricelist_Click()

Dim wsA As Worksheet
Dim wbA As Workbook
Dim TempWB As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler

Set wbA = ActiveWorkbook
Set wsA = Sheet8
strTime = Format(Now(), "yyyymmdd\_hhmm")

'get active workbook folder, if saved
strPath = CreateObject("WScript.Shell").specialfolders("Desktop")
If strPath = "" Then
strPath = CreateObject("WScript.Shell").specialfolders("Desktop")
End If
strPath = strPath & "\"

'replace spaces and periods in sheet name
strName = Replace(wsA.Name, " ", "")
strName = Replace(strName, ".", "_")

'create default name for saving file
strFile = "INT-ONLY-SAGE-PRICELIST" & "_" & Sheet16.Range("C17").Text & "_"     & Sheet16.Range("B2").Text & ".csv"
strPathFile = strPath & strFile

'use can enter name and
'select folder for file
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
    FileFilter:="CSV (Comma delimited) (*.csv), *.csv", _
    Title:="Select Folder and FileName to save")

'export to PDF if a folder was selected
If myFile <> "False" Then
    wsA.ExportAsFixedFormat _
        Type:=xlCSV, _
        Filename:=myFile
    'confirmation message with file info
    MsgBox "Sage pricebook CSV created: " _
      & vbCrLf _
      & myFile
End If

exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create CSV file"
    Resume exitHandler
End Sub

谢谢, 尼克

1 个答案:

答案 0 :(得分:0)

以下代码行/代码块需要更正。另外,您还应该在代码开头包含Option Explicit,以便容易识别未声明的变量。

A。)

Set wsA = Sheet8

将给出编译器错误。变量未定义。它应该更改为

Set wsA = Sheets("Sheet8")

类似地

      strFile = "INT-ONLY-SAGE-PRICELIST" & "_" & Sheet16.Range("C17").Text & "_"     & Sheet16.Range("B2").Text & ".csv"
strPathFile = strPath & strFile

应更改为

      strFile = "INT-ONLY-SAGE-PRICELIST" & "_" & Sheets("Sheet16").Range("C17").Text & "_" & Sheets("Sheet16").Range("B2").Text & ".csv"
strPathFile = strPath & strFile

B。)用于另存为pdf文件的代码块语法不正确。

  If myFile <> "False" Then
        wsA.ExportAsFixedFormat _
            Type:=xlCSV, _
            Filename:=myFile
        'confirmation message with file info
         MsgBox "Sage pricebook CSV created: " _
          & vbCrLf _
          & myFile
    End If

需要更改为

        'export to PDF if a folder was selected
If myFile <> "False" Then
    wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
      & vbCrLf _
      & myFile
End If

通过这些更改,您的最终代码应该可以正常工作。

Option Explicit
Sub CSVSagePricelist_Click()

Dim wsA As Worksheet
Dim wbA As Workbook
Dim TempWB As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler

Set wbA = ActiveWorkbook
Set wsA = Sheets("Sheet8")
strTime = Format(Now(), "yyyymmdd\_hhmm")

'get active workbook folder, if saved
strPath = CreateObject("WScript.Shell").specialfolders("Desktop")
If strPath = "" Then
strPath = CreateObject("WScript.Shell").specialfolders("Desktop")
End If
strPath = strPath & "\"

'replace spaces and periods in sheet name
strName = Replace(wsA.Name, " ", "")
strName = Replace(strName, ".", "_")

'create default name for saving file
strFile = "INT-ONLY-SAGE-PRICELIST" & "_" & Sheets("Sheet16").Range("C17").Text & "_" & Sheets("Sheet16").Range("B2").Text & ".csv"
strPathFile = strPath & strFile

'use can enter name and
'select folder for file
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
    FileFilter:="CSV (Comma delimited) (*.csv), *.csv", _
    Title:="Select Folder and FileName to save")

'export to PDF if a folder was selected
If myFile <> "False" Then
    wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
      & vbCrLf _
      & myFile
End If

exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create CSV file"
    Resume exitHandler
End Sub