简体   繁体   English

VBA:剪切一系列列并将其粘贴到前三列数据的底部

[英]VBA: Cut a range of columns and paste it to bottom of data in first three columns

I have data in an excel file in which there is data for separated into three columns: Date (column A), number (column B), number (column C).我有一个 excel 文件中的数据,其中的数据分为三列:日期(A 列)、数字(B 列)、数字(C 列)。

在此处输入图片说明

This sequence gets repeated to column UI.此序列将重复到列 UI。 I would like to cut the data in every three columns and paste it the last row + 1 in column a,b,c so I only have three columns of data.我想每三列剪切一次数据并将其粘贴到最后一行 + 1 列 a,b,c 中,所以我只有三列数据。 I am having trouble accounting for three columns of data in my code.我在处理代码中的三列数据时遇到问题。

`Sub movedata()
Application.ScreenUpdating = False

Dim i As Integer

Set ws = ThisWorkbook.Sheets("Cashflow Chart")
    With ws
        lastColumn = Cells(1, Columns.Count).End(xlToLeft).Column 'get last column using Row 1
        For i = 4 To lastcolumn  'loop though each column starting from 4
            Set Rng = .Range(.Cells(1, 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

    End With
    Application.ScreenUpdating = True
End Sub`

Fundimentally, change the For loop to step by 3's从根本上讲,将For循环更改为步进 3

Also, moving the data via a Variant Array will be faster, plus a few other things此外,通过 Variant Array 移动数据会更快,还有其他一些事情

Sub MoveData()
    Application.ScreenUpdating = False

    Dim i As Long
    Dim Data As Variant
    Dim LastColumn As Long
    Dim InsertRow As Long

    With ThisWorkbook.Sheets("Cashflow Chart")
        LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column 'get last column using Row 1
        InsertRow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Row 'get insert point using column A
        For i = 4 To LastColumn Step 3  'loop though each column starting from 4, step by 3
            Data = .Range(.Cells(1, i + 2), .Cells(.Rows.Count, i).End(xlUp)).Value 'copy range to variant array
            .Cells(InsertRow, 1).Resize(UBound(Data, 1), 3).Value = Data 'place data at end of column A data
            InsertRow = InsertRow + UBound(Data, 1) 'increment insert point
        Next
        .Cells(1, 4).Resize(InsertRow, LastColumn - 3).ClearContents 'clear old data
    End With

    Application.ScreenUpdating = True
End Sub

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

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