所以在我的数据中有很多空单元格,我想将相邻列的值移动到下面的数据结构上方,直到BK列为止。
A B C
row1
4
5
row2
2
3
row3
1
2
expected outcome
A B C
row1 4 5
row2 2 3
row3 1 2
我尝试执行shiftlup函数,但是它一直沿整个方向使用该值。
答案 0 :(得分:2)
全选>按F5>特殊>空白>删除
答案 1 :(得分:0)
如果您想将其实现为VBA代码,因为您已将问题标记为VBA,所以我认为您想这样做。我认为我的代码可以帮助您实现所需的结果。请参见下面的代码:
Sub fix_position()
Dim cel As Range
Dim rTable As Range
Dim lastRow As Long
Application.ScreenUpdating = False
Application.Calculation = xlManual
lastRow = ActiveWorkbook.ActiveSheet.Cells(Rows.Count, "BK").End(xlUp).Row
Set rTable = ActiveWorkbook.ActiveSheet.Range("A1:BK" & lastRow)
'this loop keeps on going until the cell first cell in column BK (last column) has data
Do Until IsEmpty(Range("BK1")) = False
'this loop goes cell by cell and if it is empty, it deletes it and shifts the rest of the cels up
For Each cel In rTable
If IsEmpty(cel) = True Then
With cel
.Delete shift:=xlUp
End With
End If
Next cel
Loop
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic
End Sub
此宏基本上从左到右移到每行最后定义的列“ BK”,并不断将数据从底部移到顶部。
它不是最快的,但是我已经在您描述的确切数据样本上对其进行了测试,并且可以正常工作。当然,您可以自己添加空白的第一行(在您的示例中看到)。