将多张纸张转置为单张纸张

时间:2014-10-31 08:05:35

标签: excel excel-vba vba

Sub sbCopyRangeToAnotherSheet()
   Dim col As Integer
   col = 1
   For Each wks In Worksheets
      If (wks.Name <> "Sheet23") Then
          wks.Range("A1:z29").Copy Destination:=Sheets("Sheet23").Cells(1, col)
          col = col + 25
      End If
   Next wks
End Sub

当我使用上面的代码时,它只会将所有内容添加到彼此之后。但我需要对其进行转置,并将每张纸张显示在另一张纸张下方,而不是彼此相邻。我需要做些什么才能做到这一点?

1 个答案:

答案 0 :(得分:0)

试试这个,让我知道:

Option Explicit

Sub sbCopyRangeToAnotherSheet()

    Dim rngRange        As Range
    Dim rnglasRow       As Long
    Dim wksDestination  As Worksheet
    Dim wks             As Worksheet

    Set wksDestination = Sheets("Sheet23")

    For Each wks In Worksheets
        If (wks.Name <> "Sheet23") Then
            Set rngRange = wksDestination.Cells.Find("*", wksDestination.Cells(1, 1), xlFormulas, xlWhole, xlByRows, xlPrevious)
            If Not rngRange Is Nothing Then
                rnglasRow = rngRange.Row
            Else
                rnglasRow = 1
            End If
            wks.Range("A1:z29").Copy Destination:=Sheets("Sheet23").Cells(rnglasRow + 2, 1)
        End If
    Next wks

    'Release Memory
    Set rngRange = Nothing
    Set wksDestination = Nothing
    Set wks = Nothing

End Sub