【问题标题】:Creating a summary sheet from multiple sheets从多个工作表创建摘要工作表
【发布时间】:2018-03-10 21:11:24
【问题描述】:

问题

我想遍历所有工作表并创建一个摘要工作表。我有一个动态列,而一些标题在COLUMN X 上,而一些在COLUMN AG 上。

因此,为了解决这个问题,我决定获取特定的标题名称,而不是选择特定的列。

但是,我似乎无法在代码上取得进展,因为我不知道如何循环所有工作表,获取列然后将其添加到摘要中。

代码

Public Sub forSummary()
Dim ws As Worksheet
Dim lRow As Long
Dim aCell As Range, rng1 As Range

'~~> Set this to the relevant worksheet
Set ws = ThisWorkbook.Sheets("JAN 6")

With ws
    '~~> Find the cell which has the name
    Set aCell = .Range("A1:BA1").Find("TOTAL OUT")

    '~~> If the cell is found
    If Not aCell Is Nothing Then
        '~~> Get the last row in that column and check if the last row is > 1
        lRow = .Range(Split(.Cells(, aCell.Column).Address, "$")(1) & .Rows.Count).End(xlUp).Row

        If lRow > 1 Then
            '~~> Set your Range
            Set rng1 = .Range(aCell.Offset(1), .Cells(lRow, aCell.Column))
            '~~> This will give you the address
            rng1.Select
        End If
    End If
End With
End Sub

有什么想法吗?

【问题讨论】:

  • 您需要 VBA 吗?至少在 Libreoffice 中,使用 Sheet1、Sheet2 到 Sheet 5,您可以写 =SUM(Sheet2.A1:B1:Sheet5.A1:B1) 来总结第 2 到第 5 之间所有工作表中的 A1:B1。另外:刚刚在 Excel 中找到对应的:superuser.com/questions/331252/…

标签: excel vba


【解决方案1】:

您的代码中的问题是您将ws 设置为特定工作表。相反,循环遍历工作表集合并以这种方式完成您的工作。

Dim ws As Worksheet, tempRng As Range

For Each ws In ThisWorkbook.Worksheets

    Set tempRng = ws.Range()    'Set your range you need for your summary page

    ' Code to perform actions with your range

    Set tempRng = Nothing

Next ws

【讨论】:

  • 是的,我只是想到了那个大声笑。那么如何将每张工作表附加到摘要中?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-13
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 2021-06-10
  • 1970-01-01
相关资源
最近更新 更多