Excel VBA:如何复制包括隐藏列在内的整个范围

Excel VBA: How to copy entire range including hidden columns

我正在寻找一个 VBA 宏来将数据导出到 csv。我发现 this code 经过一些调整后效果很好。但是,从范围复制时,Excel 似乎忽略了隐藏的列,而我希望 CSV 包含所有列。有没有人发现简洁的编码方式?

这是我目前的代码:

Sub ExportListOrTable(Optional newBook As Boolean, Optional willNameSheet As Boolean, Optional asCSV As Boolean, Optional visibleOnly As Boolean)
'Sub CopyListOrTable2NewWorksheet()
'Works in Excel 2003 and Excel 2007. Only copies visible data.
'code source: https://msdn.microsoft.com/en-us/library/dd637097%28v=office.11%29.aspx
'improved by: Tzvi
'   - replaced new worksheet with new workbook

'params:
'   newBook: To create a new new sheet in the current workbook or (default) in a new workbook
'   willNameSheet: To offer the user to name the sheet or (default) leave the default names
'   asCSV: not implemented - will always save as CSV
'   visibleOnly: to filter out any hidden columns - default false

'TODO
'   -add parameter list for following options:
'   - if table was not selected, copy activesheet.usedRange
'   - optional saveFileType
'   -


Dim New_Ws As Worksheet
Dim ACell, Data As Range
Dim CCount As Long
Dim ActiveCellInTable As Boolean
Dim CopyFormats, retrySave As Variant
Dim sheetName, user, defaultFileName, fileSaveName As String
Dim userChoice As Boolean

'Check to see if the worksheet or workbook is protected. TODO this may not be necessary anymore
If ActiveWorkbook.ProtectStructure = True Or ActiveSheet.ProtectContents = True Then
    MsgBox "This macro will not work when the workbook or worksheet is write-protected."
        Exit Sub
    End If

    'Set a reference to the ActiveCell. You can always use ACell to
    'point to this cell, no matter where you are in the workbook.
    Set ACell = activeCell

    'Test to see if ACell is in a table or list. Note that by using ACell.ListObject, you
    'do not need to know the name of the table to work with it.
    On Error Resume Next
    ActiveCellInTable = (ACell.ListObject.Name <> "")
    On Error GoTo 0

    'TODO here we will select the fields to export
    'If the cell is in a list or table run the code.
    If ActiveCellInTable = True Then
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        If visibleOnly = True Then
            'Test if there are more than 8192 separate areas. Excel only supports
            'a maximum of 8,192 non-contiguous areas through VBA macros and manual.
            On Error Resume Next
            With ACell.ListObject.ListColumns(1).Range 'TODO remove this "with"
                CCount = .SpecialCells(xlCellTypeVisible).Areas(1).Cells.Count
            End With
            On Error GoTo 0

            If CCount = 0 Then
                MsgBox "There are more than 8192 individual areas, so it is not possible to " & _
                       "copy the visible data to a new worksheet. Tip: Sort your " & _
                       "data before you apply the filter and try this macro again.", _
                       vbOKOnly, "Copy to new worksheet"
                Exit Sub
            Else
                'Copy the visible cells.
                ACell.ListObject.Range.Copy
            End If
        Else
            'The user indicated he wants to copy hidden columns too.
            '**********************************************************
            'HOW DO I PROPERLY IMPLEMENT THIS PART?
            '**********************************************************

            MsgBox ("You wanted to copy hidden columns too?")
            ActiveSheet.UsedRange.Copy
        End If
    Else
'        MsgBox "Select a cell in your list or table before you run the macro.", _
'           vbOKOnly, "Copy to new worksheet"
        userChoice = MsgBox("A Table/Table protion is not selected. Do you want to export the entire page?", vbYesNo)
        If userChoice = False Then Exit Sub
        ActiveSheet.UsedRange.Copy
        'Exit Sub
    End If
            'Add a new Worksheet/WorkBook.
            If newBook = False Then
                Set New_Ws = Worksheets.Add(after:=Sheets(ActiveSheet.Index))
            Else
                Set New_Ws = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
            End If

            'Prompt the user for the worksheet name.
            If willNameSheet = True Then
                sheetName = InputBox("What is the name of the new worksheet?", _
                                     "Name the New Sheet")

                On Error Resume Next
                New_Ws.Name = sheetName
                If Err.Number > 0 Then
                    MsgBox "Change the name of sheet : " & New_Ws.Name & _
                         " manually after the macro is ready. The sheet name" & _
                         " you typed in already exists or you use characters" & _
                         " that are not allowed in a sheet name."
                    Err.Clear
                End If
                On Error GoTo 0
            End If

            'Paste the data into the new worksheet.
            With New_Ws.Range("A1")
                .PasteSpecial xlPasteColumnWidths
                .PasteSpecial xlPasteValuesAndNumberFormats
                .Select
                Application.CutCopyMode = False
            End With

            Application.ScreenUpdating = False

            'If you did not create a table, you have the option to copy the formats.
            If ActiveCellInTable = False Then
                Application.Goto ACell
                CopyFormats = MsgBox("Do you also want to copy the Formatting?", _
                                     vbOKCancel + vbExclamation, "Copy to new worksheet")
                If CopyFormats = vbOK Then
                    ACell.ListObject.Range.Copy
                    With New_Ws.Range("A1")
                        .PasteSpecial xlPasteFormats
                        Application.CutCopyMode = False
                    End With
                End If
            End If

        'Select the new worksheet if it is not active.
        Application.Goto New_Ws.Range("A1")

        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With

        'Now we're ready to save our new file as excel format
        defaultFileName = ActiveWorkbook.Name
        user = Environ("userprofile")

'marker getfilename: to return to if we need to look for a new filename
getfilename:
        ChDir user & "\Desktop"
        fileSaveName = Application.GetSaveAsFilename(defaultFileName & ".csv", "Comma Delimited Format (*.csv), *.csv")

        If fileSaveName <> "False" Then
            'error handling for 'file already exists and the user clicks 'no'
            On Error Resume Next
            ActiveWorkbook.SaveAs fileName:=fileSaveName, FileFormat:=xlCSV, ReadOnlyRecommended:=True, CreateBackup:=False, ConflictResolution:=xlUserResolution
            If Err.Number = 1004 Then
                'Offer user two options: To try a different filename or cancel the entire export
                retrySave = MsgBox(Err.Description, vbRetryCancel, "Error creating file")
                If retrySave = vbRetry Then
                    GoTo getfilename
                Else
                    GoTo cancelprocedure
                End If
            End If
            On Error GoTo 0
        Else
            GoTo cancelprocedure
        End If

Exit Sub
cancelprocedure:
    ActiveWorkbook.Close saveChanges:=False
    Exit Sub
End Sub

更新:

回应shagan的关注。第一行的参数列表旨在由另一个宏设置:

Sub ExportVisibleAsCSV
    Call ExportListOrTable(newBook:=True, willNameSheet:=False, asCSV:=True, visibleOnly:=True)
End Sub

现在更新示例代码可用:

好的,查看您发布的代码,我看到一个名为 visibleOnly 的布尔值,但我看不到它的设置位置。您使逻辑达到 UsedRange.Copy 的能力完全取决于将其设置为 false。 ACell.ListObject.Range.Copy 上方的注释表明,如果您到达该语句,您只是在复制可见单元格。为了复制隐藏的单元格,需要将 visibleOnly 设置为 false(绕过 CCount 的其余部分)。因此,我很想知道该 bool 是如何设置的,并在您 运行 您的代码时检查它的值设置为什么。

更新 2:

您需要以某种方式设置 visibleOnly 布尔值。

这是我编辑的一些代码,它创建了一个消息框,允许用户说 "yes" 或 "no" 到 "do you want to copy hidden data too?",答案将决定 visibleOnly 的值,而 visibleOnly 又决定他们进入哪个流程。

除此之外,您关于 ACell.ListObject.Range.Copy 只会复制可见单元格的假设似乎是不正确的。相反,它被可见单元格的特殊单元格类型所取代。

最后,vbYesNo 实际上 return 不是一个布尔值。相反,它 returns vbYes 或 vbNo 是 vb 类型的枚举数(分别为值 6 和 7)。因此,将 bool 设置为 vbYesNo 的值将始终 return True(因为存在一个值,并且本质上它只是评估 iferror)。

所以我也更改了那个位,因此它现在可以正确检查用户选择的 Yes/No 条件(不再是布尔值)。

代码如下:

Dim ACell, Data As Range
Dim CCount As Long
Dim ActiveCellInTable As Boolean
Dim CopyFormats, retrySave As Variant
Dim sheetName, user, defaultFileName, fileSaveName As String


'Check to see if the worksheet or workbook is protected. TODO this may not be necessary anymore
If ActiveWorkbook.ProtectStructure = True Or ActiveSheet.ProtectContents = True Then
    MsgBox "This macro will not work when the workbook or worksheet is write-protected."
        Exit Sub
    End If

    'Set a reference to the ActiveCell. You can always use ACell to
    'point to this cell, no matter where you are in the workbook.
    Set ACell = ActiveCell

    'Test to see if ACell is in a table or list. Note that by using ACell.ListObject, you
    'do not need to know the name of the table to work with it.
    On Error Resume Next
    ActiveCellInTable = (ACell.ListObject.Name <> "")
    On Error GoTo 0

    'TODO here we will select the fields to export
    'If the cell is in a list or table run the code.
    If ActiveCellInTable = True Then
        CopyHidden = MsgBox("Would you like to copy hidden data also?", vbYesNo, "Copy Hidden Data?")
        If CopyHidden = vbYes Then
            visibleOnly = False
        ElseIf CopyHidden = vbNo Then
            visibleOnly = True
        End If

        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        If visibleOnly = True Then
            'Test if there are more than 8192 separate areas. Excel only supports
            'a maximum of 8,192 non-contiguous areas through VBA macros and manual.
            On Error Resume Next
            With ACell.ListObject.ListColumns(1).Range 'TODO remove this "with"
                CCount = .SpecialCells(xlCellTypeVisible).Areas(1).Cells.Count
            End With
            On Error GoTo 0

            If CCount = 0 Then
                MsgBox "There are more than 8192 individual areas, so it is not possible to " & _
                       "copy the visible data to a new worksheet. Tip: Sort your " & _
                       "data before you apply the filter and try this macro again.", _
                       vbOKOnly, "Copy to new worksheet"
                Exit Sub
            Else
                'Copy the visible cells.
                ACell.ListObject.Range.SpecialCells(xlCellTypeVisible).Copy
                ' Only visible cells within the table are now in clipboard
            End If
        Else
            'The user indicated he wants to copy hidden columns too.
            MsgBox ("You wanted to copy hidden columns too?")
            ACell.ListObject.Range.Copy
            ' All table data cells including hidden are now in clipboard
        End If
    Else
'        MsgBox "Select a cell in your list or table before you run the macro.", _
'           vbOKOnly, "Copy to new worksheet"
        userChoice = MsgBox("A Table/Table protion is not selected. Do you want to export the entire page?", vbYesNo)
        If userChoice = vbNo Then Exit Sub
        ActiveSheet.UsedRange.Copy
        'Entire sheet range is now in clipboard (this is not always accurate)
        'Exit Sub
    End If

将范围的值分配给目标范围,而不是使用 .Copy 方法:

Sub ExportCSV(source As Range, filename As String)

    Dim temp As Workbook
    Set temp = Application.Workbooks.Add

    Dim sheet As Worksheet
    Set sheet = temp.Worksheets(1)

    Dim target As Range
    'Size the target range to the same dimension as the source range.
    Set target = sheet.Range(sheet.Cells(1, 1), _
                 sheet.Cells(source.Rows.Count, source.Columns.Count))
    target.Value = source.Value

    temp.SaveAs filename, xlCSV
    temp.Close False

End Sub

这还有一个好处,即不会破坏用户在剪贴板上可能拥有的任何内容。