转置列中的特定行数

时间:2018-01-16 03:30:24

标签: excel excel-vba vba

我想知道可以在一列中转置特定数量的列并将其显示在一行中。例如,如果有一列从A1扩展到A1000000,是否可以选择前272个数据点,然后将其转换为从A1开始的单行,然后选择接下来的272行并将其显示在B1等上直到它到达最后一行。

谢谢,

3 个答案:

答案 0 :(得分:1)

选择A1:A272。按复制(或Ctl + C)。 选择B1。按功能区Paste选项卡左上角的Home。 在打开的对话框中选择Paste SpecialTranspose

答案 1 :(得分:-1)

Sub Transp_mod2()

Dim P1 As Range, T2()

Set P1 = Sheets(3).UsedRange 'Adapt to your source column range
T1 = P1

Rws = P1.Count
Rmd = Rws
Spl = 272 'Adapt to your required steps
Cnt = 1

If Rws Mod Spl = 0 Then Rnds = Rws / Spl Else Rnds = Int(Rws / Spl) + 1

For i = 1 To Rnds
    ReDim Preserve T2(1 To Spl, 1 To i)
    If Rmd = Rws Mod Spl Then t = Rmd Else t = Spl
    For j = 1 To t
        T2(j, i) = T1(Cnt, 1)
        Cnt = Cnt + 1
        Rmd = Rmd - 1
    Next j
Next i

Sheets(4).Range("A1").Resize(UBound(T2, 2), UBound(T2, 1)) = Application.Transpose(T2) 'Adapt "Sheets(4).range("A1")" to your destination range

End Sub

答案 2 :(得分:-1)

Sub CopyToRange()
    Dim vDB, vR()
    Dim rngDB As Range
    Dim Cnt As Long, i As Long, j As Integer
    Dim n As Long

    Set rngDB = Range("a1", Range("a" & Rows.Count).End(xlUp))
    vDB = rngDB

    Cnt = 272

    For i = 1 To UBound(vDB, 1) Step Cnt
        n = n + 1
        ReDim Preserve vR(1 To Cnt, 1 To n)
        For j = 1 To Cnt
            If i + j - 1 > UBound(vDB, 1) Then GoTo p
            vR(j, n) = vDB(i + j - 1, 1)
        Next j
    Next i
p:
    Sheets.Add
    Range("a1").Resize(n, Cnt) = WorksheetFunction.Transpose(vR)


End Sub