将值从列B剪切并粘贴到下一个空行列A

时间:2018-07-15 06:26:16

标签: excel

enter image description here

我想从B列(444)剪切一个值到A列中的下一个空行(在rr下)。然后,脚本将遍历B列,因此将16粘贴到ee等下

我尝试使用以下代码在A列中查找下一个空白行,但需要更多帮助来构造脚本;

lastrow = Sheets(1).Range(“ A1”)。End(xlDown).Row + 1

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

您需要的是这样的

Sub CutValuesColumnBtoA()

Dim copyCell As Range
Dim colB As Range
Dim lastRowColB As Long
Dim firstBlankRowColA As Long

'get the rownumber of the last non-empty cell in column B
lastRowColB = Cells(Rows.Count, 2).End(xlUp).Row

'get the range thats holds the values to be cut&pasted from column B
Set colB = Range(Cells(1, 2), Cells(lastRowColB, 2))

'loop through the values in colB.
'If not empty then paste to first empty cell in column A
For Each copyCell In colB

    If copyCell <> "" Then

        'get the rownumber of the first blank cell in column A
        'Cells(1, 1).End(xlDown).Row will not work if one or both of the 
        'first two rows are empty, so they need to be tested separatly
        If Cells(1, 1) = "" Then
            firstBlankRowColA = 1
        ElseIf Cells(2, 1) = "" Then
            firstBlankRowColA = 2
        Else
            firstBlankRowColA = Cells(1, 1).End(xlDown).Row + 1
        End If

    Cells(firstBlankRowColA, 1).Value = copyCell

    End If

Next copyCell

'clear colB Range, because this is a cut action (and not copy)
colB.ClearContents

End Sub