excel - 从所有工作表中复制范围并将它们粘贴到新的工作表中

标签 excel vba

请帮助我,我需要复制不同的范围,直到所有工作表中的第一个空白单元格并将它们粘贴到新的单元格中。都在同一个Workbook .
这是我的尝试:

Sub Target()

   Dim lRow As Long
   Dim copyRange As Range
    Dim sh As Worksheet
    Dim shReport As Worksheet
    Set shReport = ThisWorkbook.Worksheets("Target")

    For Each sh In ThisWorkbook.Worksheets
        Select Case sh.Name
            Case Is <> "ALLProjectForReport"
                lRow = shReport.Cells(Rows.Count, "B").End(xlUp).Row
               Set copyRange = sh.Range("A3")

               copyRange.Copy Destination:=shReport.Range("B" & lRow)

        End Select
    Next
    Set shReport = Nothing
    Set sh = Nothing
End Sub

最佳答案

堆叠部分列 ( xlDown )

Option Explicit

Sub createReport()

    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim dst As Worksheet: Set dst = wb.Worksheets("Target")
    Dim cel As Range
    Set cel = dst.Cells(dst.Rows.Count, "B").End(xlUp).Offset(1)

    Dim src As Worksheet
    Dim rng As Range
    For Each src In wb.Worksheets
        Select Case src.Name
            Case "AllProjectForReport", "Target"
            Case Else
                With src.Range("A3")
                    Set rng = .End(xlDown)
                    If rng.Row < src.Rows.Count Then
                        Set rng = .Resize(rng.Row - .Row + 1)
                    Else
                        If Len(.Value) > 0 Then
                            Set rng = .Offset
                        Else
                            Set rng = Nothing
                        End If
                    End If
                    If Not rng Is Nothing Then
                        rng.Copy Destination:=cel
                        Set cel = cel.Offset(rng.Rows.Count)
                    End If
                End With
        End Select
    Next
  
    MsgBox "Report created.", vbInformation, "Success"

End Sub

关于excel - 从所有工作表中复制范围并将它们粘贴到新的工作表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65746444/

相关文章:

excel - 在一个非常大的表中循环一个 if 函数。太慢了

vba - 运行时错误 13 : Type mismatch

vba - 使 Resize() 更具体?

excel - 删除 VBA excel 中的空格

vba - 插入 Excel VBA 字符串

excel - 未找到 getElementById 时捕获错误

excel - 为什么我收到 "object does not support this property method"?

vba - 根据单元格输入将行信息从一张纸复制到另一张纸

excel - 来自 .NET 应用程序或 VSTO 加载项的电子表格比较 (Office 2013) 自动化

javascript - CefSharp ChromiumWebBrowser javascript excel 导出不起作用