【问题标题】:Transpose data from a specific row in multiple worksheets into columns on another sheet将多个工作表中特定行的数据转换为另一个工作表上的列
【发布时间】:2020-05-24 06:04:05
【问题描述】:

我有一个大约 80 张的工作簿。每张纸的格式相同(这里和那里都有一些空白)。

有没有办法转置同一行,例如A2 - K2 从所有 80 个工作表到汇总表中的列?

这篇文章 (Transpose data from a specific column from multiple sheets to rows on another 'summary' sheet) 将列转换为行。

【问题讨论】:

  • 我们很乐意帮助您编写任何代码。我们不会为您编写代码。这不是本网站提供的。
  • 尝试通过在复制和转置几行时录制宏来开始。然后尝试修改代码以添加循环。如果遇到问题,请回复。
  • @Tim Williams:此评论是否不当:乍一看,在提到的代码中,将整个For...Next 块替换为ws.Range(ws.Cells(1, currentRow), ws.Cells(11, currentRow)) = Application.Transpose(sh.Range("A2:K2"))(全部在一行中)并适当地重命名@987654324 的出现@ 到例如curCol 并尝试找出其余部分。如果你不能或结果不是你所期望的,那么edit你的问题adding the code并解释它有什么问题,你肯定会得到帮助。
  • 感谢大家的回复!我会试一试,看看我会怎么走。抱歉,如果我的问题不恰当,我对宏不太熟悉,因此需要朝正确的方向轻推。

标签: excel vba transpose


【解决方案1】:

试试这个。它假设您所有的数据表都被命名为数字。 它将添加一个新工作表“摘要”。如果“摘要”工作表存在,则会将其删除。

Sub MakeSummary()
    Dim v As Variant
    Dim wsSummary As Worksheet
    'Assume that there are worksheets with data in range A2:A12
    'All this sheets are named by number (1, 2, ... n)
    'There will be a new Summary sheet created with the transposed range

    'Add the new Worksheet
    Set wsSummary = ThisWorkbook.Worksheets.Add
    On Error Resume Next
        wsSummary.Name = "Summary" 'Try to name it  "Summary"
        If wsSummary.Name <> "Summary" Then
            'If the name is not  "Summary" than failed, probably there is already a sheet named as  "Summary"
            Worksheets("Summary").Delete 'Delete the sheet  "Summary"
        End If
        wsSummary.Name = "Summary" 'Try again name the sheet to  "Summary"
    On Error GoTo 0

    Dim rngTarget As Range
    Dim rngSource As Range
    Set rngTarget = wsSummary.Range("A2:A12") 'size should match

    Dim i As Long
    For Each v In ThisWorkbook.Worksheets 'Looping through all the worksheets
        If IsNumeric((v.Name)) Then 'If the name is number, then it is a data sheet
            i = i + 1
            Set rngSource = v.Range("A2:K2") ' Set the source range to the actual sheet and range
            rngTarget.Offset(, i).Value = Application.WorksheetFunction.Transpose(rngSource.Value)
        End If
    Next v
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 2018-11-18
    • 1970-01-01
    相关资源
    最近更新 更多