vba - 将 Excel 数据从多个工作表复制到一张工作表中

标签 vba excel

我曾尝试在互联网上搜索这个问题的各种答案,但找不到正确的答案。我有一个 Excel 工作簿,其中的工作表代表一个月中的每一天。在这些表格中的每一个中,格式都是相同的(星期六和星期日除外),并且表格包含通话统计信息。它以以下格式呈现:

00:00 00:30 0 4 6 3 4 8 0 1 0 0 0

00:00 00:30 0 0 2 7 4 1 0 0 3 3 0

00:00 00:30 7 0 7 5 2 8 6 1 7 9 0

我需要将此数据复制到一张列出所有数据的单张纸中。基本上它将新数据附加到旧数据的底部。所以这将是一个大名单。

如何才能做到这一点?我所能看到的只是如何通过将所有值加在一起来从多个数据中生成总数。我只需要将数据列为一个大列表。

最佳答案

大规模编辑:

与上次与 Iain 的聊天一样,已设置正确的参数。我删除了最后几个代码片段,因为它们完全不正确。如果有人仍然感兴趣,请查看编辑历史记录。

希望这是最后的编辑。 ;)

因此,所需的正确条件是:

  • 工作表中的月份名称。我们为此使用了一个输入框。
  • 我们检查行数。共有三个条件:总共 157 行,总共 41 行,以及所有其他。

  • 下面的子程序可以解决问题。
    Sub BlackwoodTransfer()
    
        Dim Summ As Worksheet, Ws As Worksheet
        Dim ShName As String
        Dim nRow As Long
    
        Set Summ = ThisWorkbook.Sheets("Summary")
        ShName = InputBox("Enter month for Call Flow in mmmm format (ie. November, etc.):") & " Call Flow"
        'Returns November Call Flow. This means it will target every sheet that has November Call Flow in its name.
    
        Application.ScreenUpdating = False
    
        For Each Ws In ThisWorkbook.Worksheets
            If InStr(1, Ws.Name, ShName) > 0 Then
            'Starting from first character of the sheet's name, if it has November, then...
                nRow = Summ.Cells(Rows.Count, 1).End(xlUp).Row + 1
                '... get the next empty row of the Summary sheet...
                Select Case Ws.Cells(Rows.Count, 1).End(xlUp).Row
                '... check how many rows this qualified sheet has...
                    Case 157
                    '... if there are 157 rows total...
                        Ws.Range(Cells(57,1),Cells(104,13)).Copy
                        '... copy Rows 57 to 104, 13 columns wide...
                        Summ.Range("A" & nRow).PasteSpecial xlPasteAll
                        '... and paste to next empty row in Summary sheet.
                    Case 41
                        Ws.Range(Cells(23,1),Cells(126,13)).Copy
                        Summ.Range("A" & nRow).PasteSpecial xlPasteAll               
                    Case Else
                        Ws.Range(Cells(23,1),Cells(30,13)).Copy
                        Summ.Range("A" & nRow).PasteSpecial xlPasteAll
                End Select
            End If
        Next Ws
    
        Application.ScreenUpdating = True
    
    End Sub
    

    @Iain:查看评论并将它们与 MSDN 数据库交叉引用。这应该解释每个函数/方法正在做什么。希望这可以帮助!

    关于vba - 将 Excel 数据从多个工作表复制到一张工作表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20066005/

    相关文章:

    excel - 有没有办法使用 VBA 中的列号选择整列?

    vba - 我可以获得与 DateTime.Now.Ticks 等效的 VBA 吗?

    vba - 在 Excel 中自动复制和粘贴特定范围的最佳方法是什么?

    excel - 在 VBA 中查找数组的模式

    excel - 基于重复的总和值 - VBA

    arrays - 从数组中删除重复项 - vba

    excel - Date 和 If 一起使用

    excel - 通过 UserID 在 Outlook 中搜索联系人

    string - 向下复制找到的字符串 1 单元格

    arrays - 几个不连续的列到数组中