VBA使用范围将Excel导出为CSV

时间:2018-10-04 15:12:12

标签: excel vba excel-vba

我使用了发现的here代码。

经过一些更改,这是我现在拥有的代码:

Option Explicit
Sub ExportAsCSV()

Dim MyFileName As String
Dim Item As String
Dim Path As String
Dim CurrentWB As Workbook, TempWB As Workbook
Path = "F:\Excels\csv export\"

Set CurrentWB = ActiveWorkbook
ActiveWorkbook.Worksheets("Nieuw Artikelnummer").UsedRange.Copy
Item = Range("D2")

Set CurrentWB = ActiveWorkbook
ActiveWorkbook.Worksheets("csv").UsedRange.Copy

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

MyFileName = Path & "\" & Item & ".csv"

Application.DisplayAlerts = False
TempWB.SaveAs filename:=MyFileName, FileFormat:=xlCSV, CreateBackup:=False, Local:=True
TempWB.Close SaveChanges:=False
Application.DisplayAlerts = True
MsgBox ".csv file has been created: " _
  & vbCrLf _
  & MyFileName
End Sub

我遇到的问题是它使用UsedRange,但是我想选择复制到新.csv文件中的Range。

如何选择要复制到新文件而不是UsedRange的范围?

3 个答案:

答案 0 :(得分:1)

这将在商品编号表上打开一个输入框,您可以手动选择或输入范围:

Sub ExportAsCSV()

Dim MyFileName As String
Dim Item As String
Dim Path As String
Dim CurrentWB As Workbook, TempWB As Workbook
Dim myrangeNA As Range
Dim myRangeCSV As Range
Path = "F:\Excels\csv export\"

Set CurrentWB = ActiveWorkbook
ActiveWorkbook.Worksheets("Nieuw Artikelnummer").Activate
Set myrangeNA = Application.InputBox(prompt:="Select a range to copy", Type:=8)
Item = Range("D2")

Set TempWB = Application.Workbooks.Add(1)
myrangeNA.Copy Destination:=TempWB.Worksheets("Sheet1").Range("A1")

MyFileName = Path & "\" & Item & ".csv"

Application.DisplayAlerts = False
TempWB.SaveAs Filename:=MyFileName, FileFormat:=xlCSV, CreateBackup:=False, Local:=True
TempWB.Close SaveChanges:=False
Application.DisplayAlerts = True
MsgBox ".csv file has been created: " _
  & vbCrLf _
  & MyFileName
End Sub

如果您不想选择它,请将myrangeNA更改为想要的任何范围,例如range("A5:C20"),它应该可以工作。

答案 1 :(得分:0)

在这种情况下,我倾向于将操作隔离到可以使用参数调用的独立SubFunction上。这样,我可以根据需要在项目或另一个项目中重复使用它。

因此,我将复制所选数据范围并将其粘贴到临时工作簿,然后将其保存到自己的Function中的CSV文件中分离了。该操作将返回True / False结果作为成功检查的结果。

Option Explicit

Sub test()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    Dim destCSVfile As String
    destCSVfile = "C:\Temp\" & ws.Range("D2")

    If ExportAsCSV(Selection, destCSVfile) Then
        MsgBox ".csv file has been created: " _
             & vbCrLf _
             & destCSVfile
    Else
        MsgBox ".csv file NOT created"
    End If
End Sub

Private Function ExportAsCSV(ByRef dataArea As Range, _
                             ByVal myFileName As String) As Boolean
    '--- make sure we have a range to export...
    ExportAsCSV = False
    If dataArea Is Nothing Then
        Exit Function
    End If

    dataArea.Copy

    '--- create a temporary workbook that will be saved as a CSV format
    Dim tempWB As Workbook
    Set tempWB = Application.Workbooks.Add(1)
    With tempWB.Sheets(1).Range("A1")
        .PasteSpecial xlPasteValues
        .PasteSpecial xlPasteFormats
    End With

    '--- suppress alerts to convert the temp book to CSV
    Application.DisplayAlerts = False
    tempWB.SaveAs filename:=myFileName, FileFormat:=xlCSV, _
                  CreateBackup:=False, Local:=True
    tempWB.Close SaveChanges:=False
    Application.DisplayAlerts = True
    ExportAsCSV = True
End Function

答案 2 :(得分:0)

您在上述注释中的其他两个问题提到粘贴转置值,您可以通过将myrangeNA.Copy Destination:=TempWB.Worksheets("Sheet1").Range("A1")行更改为

myrangeNA.Copy 
TempWB.Worksheets("Sheet1").Range("A1").PasteSpecial _ 
Paste:=xlPasteValues, Transpose:=True

此网站是Office VBA集合中所有各种对象,方法和属性的绝佳参考来源:https://docs.microsoft.com/en-us/office/vba/api/overview/excel/object-model (或者https://docs.microsoft.com/de-de/office/vba/api/overview/excel/object-model,如果您希望将大约五个单词翻译成德语)