Excel VBA:将多个可变大小的表/范围复制到大型数据表中

时间:2016-04-22 08:11:32

标签: excel vba excel-vba

我是这个论坛的快乐读者,帮助了我很多问题,但现在有一些我找不到的东西-yet-。首先,我正在学习vba,但仍然是初学者!

问题:

我正在寻找一个marco,当运行时:在具有可变行的多个工作表上选择一个变量范围:

所以它应该选择A25:L25,然后直到最后一行,在表格中找到一个填充的单元格。我认为如下:

Range("A25:L25").Select  
Range(Selection, Selection.End(xlDown)).Select.)

然后它应该复制这些值,并将其粘贴到数据表" PIVOTDATA",彼此之下。

是多张,其数量各不相同。但要处理的纸张范围在2张之间:'客户' &安培; ' END'

我试图尽可能具体,希望有人可以提供帮助!

1 个答案:

答案 0 :(得分:0)

这应该有效

 Sub CopyDataToPIVOTDATASheet()

      Dim PivotDataSheet As Worksheet
      Set PivotDataSheet = ThisWorkbook.Sheets("PIVOTDATA")

      Dim CurrentWorkSheetIndex As Long
      Dim CurrentWorkSheet As Worksheet
      Dim CurrentWorkSheetLastRow As Long
      Dim PivotDataWorkSheetLastRow As Long

      For CurrentWorkSheetIndex = ThisWorkbook.Sheets("Clients").Index +1 To ThisWorkbook.Sheets("END").Index - 1

           Set CurrentWorkSheet = ThisWorkbook.Sheets(CurrentWorkSheetIndex)
           CurrentWorkSheet.Activate

           CurrentWorkSheetLastRow = CurrentWorkSheet.Cells(CurrentWorkSheet.Rows.Count, "L").End(xlUp).Row

           PivotDataWorkSheetLastRow = PivotDataSheet.Cells(PivotDataSheet.Rows.Count, "L").End(xlUp).Row + 1

           CurrentWorkSheet.Range(CurrentWorkSheet.Cells(25, "A"), Cells(CurrentWorkSheetLastRow, "L")).Copy _
                          Destination:=PivotDataSheet.Cells(PivotDataWorkSheetLastRow, "A")

      Next CurrentWorkSheetIndex

 End Sub