简体   繁体   English

将数据从B列剪切/粘贴到A列中的第一个空单元格

[英]Cutting/Pasting Data from column B to first empty cell in Column A

I have just one worksheet that has 6 columns and 10 rows. 我只有一个工作表,其中有6列和10行。 So the Range of my table is A1:F10 which has 60 cells. 所以我表格的范围是A1:F10,它有60个单元格。

I simply need to cut the data from column B and paste it into the first empty cell in column A. Then, I need it to do the same with columns C - F. Eventually I want to have only one column (Column A) that is 60 rows deep. 我只需要从B列中剪切数据并将其粘贴到A列中的第一个空单元格中即可。然后,我需要对C-F列执行相同的操作。最终,我只希望有一个列(A列)是60行深。

Sub Move_Columns()
    Range("B1:B10").Copy Destination:=Range("A11")
    Range("C1:C10").Copy Desitnation:=Range("21")
    ' this would continue until columns B-F were copied in column A
End Sub

The problem is that this code only copies the data over. 问题在于此代码仅复制数据。 I need it removed once it has been copied. 复制后,我需要将其删除。 I'm also sure there is a much more efficient way to write the code so that I don't have to keep repeating the ranges. 我也确信有一种更有效的方式编写代码,这样我就不必继续重复这些范围。

I wish I knew how to write the code so that Excel will automatically cut and paste the data from each column into the first empty row in column A. 我希望我知道如何编写代码,以便Excel将自动将每列中的数据剪切并粘贴到A列中的第一个空行中。

Would the For Each Statement be a good idea to add in there? 在其中添加For Each语句是一个好主意吗?

I make this code, and put some comments to help you to understand. 我编写此代码,并添加一些注释以帮助您理解。

Sub MoveAllInFirstColumn()
    Dim i As Integer
    Dim lastCol As Integer
    lastCol = Cells(1, Columns.Count).End(xlToLeft).Column 'finds the last column
    For i = 2 To lastCol ' foreach columns except first
        Dim lastRow As Integer
        lastRow = Cells(Rows.Count, "A").End(xlUp).Row 'get the lastrow of current column
        Range(Cells(1, i), Cells(Cells(Rows.Count, i).End(xlUp).Row, i)).Cut Cells(lastRow + 1, 1) 'cut and paste the current column to the first column
    Next i
End Sub

To continue with what you have: 继续您所拥有的:

Sub Move_Columns()
  Range("B1:B10").Copy Destination:=Range("A11")
  Range("B1:B10").ClearContents
  Range("C1:C10").Copy Desitnation:=Range("A21")
  Range("C1:C10").ClearContents
  ' this would continue until columns B-F were copied in column A
End Sub

An alternative with some looping 有一些循环的替代方法

Sub Move_Columns()
Dim StartCol as Integer
Dim EndCol as Integer
Dim StartRow as Integer
Dim EndRow as Integer
Dim CurRow as Integer
Dim i as Integer
Dim DestCol as integer

DestCol = 1
StartCol = 2
EndCol = 6
StartRow = 1
EndRow = 10

CurRow = StartRow
for I = StartCol to EndCol
  'Range(cells(i, StartRow),cells(i, EndRow).Copy Destination:=Range(DestCol,CurRow)
  'Range(cells(i, StartRow),cells(i, EndRow).ClearContents
  Range(cells(StartRow, i), cells(EndRow, 1)).Copy Destination:=Range(DestCol, CurRow)
  Range(cells(StartRow, i),cells(EndRow,i)).ClearContents
  CurRow = CurRow + EndRow
Next
end Sub

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

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