简体   繁体   English

VBA:剪切一系列列并将其粘贴为一列

[英]VBA: Cut a range of columns and paste it as one column

I'm pretty new and would greatly appreciate your help.我是新手,非常感谢您的帮助。 I have data in 14000 columns that are about 30 lines each.我有 14000 列数据,每列大约 30 行。

I need to paste each column underneath one another such that I will only have 1 column remaining.我需要将每一列粘贴在另一列下面,这样我就只剩下 1 列了。 I have provided an example screenshot:我提供了一个示例截图:

数据示例

My attempt at the VBA code is as follows (which doesn't work):我对 VBA 代码的尝试如下(不起作用):

Sub Cut()

Dim i As Integer
For i = 1 To 14000
  Col = Columns(i).Select

  Range("N2:N31").Offset(, i).Select
  Selection.Cut

  Range("H2").End(xlDown).Offset(1).Row.Select
  ActiveSheet.Paste

Next i

End Sub

Please Help.请帮忙。 Thanks谢谢

Try this, which assumes your data start in row 1.试试这个,它假设您的数据从第 1 行开始。

Sub Cut()

Dim c As Long

Application.screenupdating=false

For c = 14 To Cells(1, Columns.Count).End(xlToLeft).Column 'change 1 if data starts in a different row
    Range(Cells(1, c), Cells(Rows.Count, c).End(xlUp)).Copy Range("A" & Rows.Count).End(xlUp)(2)
Next c

Application.screenupdating=true

End Sub

It will look something like this:它看起来像这样:

Option Explicit
Sub MoveData()
    Dim i As Long, lRowData As Long, lRowInput As Long
    ' "H" is 8th column, so you might want
    ' to change starting value of i to 9 or something like that
    For i = 14 To 14000
        ' Last row in "H" column (where you paste data)
        lRowInput = Range("H" & ActiveSheet.Rows.Count).End(xlUp).Row
        ' Last row in i column (from where you draw data)
        lRowData = Cells(ActiveSheet.Rows.Count, i).End(xlUp).Row
        ' Copy values from i column to "H" column
        Range("H" & lRowInput + 1, "H" & lRowInput + lRowData - 1) = _
            Range(Cells(2, i), Cells(lRowData, i)).Value
    Next i
End Sub

Especially for large amounts of data, you will often be better off (faster execution time), by reading the original data into a VBA Array;特别是对于大量数据,通过将原始数据读入 VBA 数组,您通常会更好(更快的执行时间); manipulating it;操纵它; then writing it back to the worksheet, as compared with multiple read/writes to the worksheet.然后将其写回工作表,与多次读/写工作表相比。

Here is an example of using that somewhat different technique on your data, if I understand your data setup correctly (a contiguous range of data including N2 , with no blanks, and all columns the same length).如果我正确理解了您的数据设置(包括N2在内的连续数据范围,没有空格,并且所有列的长度相同),那么这是对您的数据使用这种稍微不同的技术的示例。 If my understanding of your data setup is incorrect, probably minor changes to the code will be necessary如果我对您的数据设置的理解不正确,可能需要对代码进行细微的更改

Also, this might not work in 32 bit Excel, but it is worth a try.此外,这可能不适用于 32 位 Excel,但值得一试。

Option Explicit
Sub ManyIntoOneColumn()
    Dim WS As Worksheet
    Dim rWhatIHave As Range
    Dim rWhatIWant As Range
    Dim V As Variant, W As Variant
    Dim I As Long, J As Long, K As Long

'set worksheet
Set WS = Worksheets("sheet1") 'change worksheet name

'set range to copy.  If not contiguous, might have to do it differently
Set rWhatIHave = WS.Range("N2").CurrentRegion

'set first cell of WhatIWant
Set rWhatIWant = WS.Range("H2")

'read range into variant array
V = rWhatIHave

'dimension results array
ReDim W(1 To UBound(V, 1) * UBound(V, 2), 1 To 1)

'populate W
K = 0
For I = 1 To UBound(V, 2)
    For J = 1 To UBound(V, 1)
        K = K + 1
        W(K, 1) = V(J, I)
    Next J
Next I

'set rWhatIWant
Set rWhatIWant = rWhatIWant.Resize(rowsize:=UBound(W, 1), columnsize:=1)

'clear results range and write the values
With rWhatIWant
    .EntireColumn.Clear
    .Value = W
    .EntireColumn.AutoFit
End With

End Sub

Instead of copying range you can just assign the values to range as您可以将值分配给范围,而不是复制范围

Sub ConvertRangeToColumn()
    Dim ws As Worksheet
    Dim lastColumn As Long, lastRow As Long
    Dim rng As Range

    Application.ScreenUpdating = False
    Set ws = ThisWorkbook.Sheets("Sheet1")  'change Sheet1 to yout data sheet
    With ws
        lastColumn = Cells(1, Columns.Count).End(xlToLeft).Column   'get last column using Row 1
        For i = 5 To lastColumn             'loop though each column starting from 5
            Set rng = .Range(.Cells(2, i), .Cells(.Cells(.Rows.Count, i).End(xlUp).Row, i)) 'set range to copy
            .Range("A" & .Cells(.Rows.Count, "A").End(xlUp).Row + 1).Resize(rng.Rows.Count).Value = rng.Value
        Next i
    End With
    Application.ScreenUpdating = True
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM