生成给定范围(VBA)的CSV

时间:2019-03-21 16:34:14

标签: excel vba

我想在范围A6:X20000上创建一个.csv,并跳过所有空行。 我尝试搜索并找到了solution,但我尝试对其进行调整但失败了。以下代码始终生成A1:X20000,而不会跳过空行。

Sub Button_click()
    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("A6:X20000")
        .PasteSpecial xlPasteValues
        .PasteSpecial xlPasteFormats
        .AutoFilter 1, "<>" 'Filter to get only non-blank cells

    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

0 个答案:

没有答案