要检查是否有更多的数据在右侧,您可以使用查找功能找到包含数据的最右边的单元格。
试试这个:
' Run the code 50 times
For x = 0 to 50
' Use the Find function to locate the bottom rightmost cell with any content in it.
Set BottomRightCell = ActiveSheet.Cells.Find(what:="*", After:=ActiveSheet.Range("A1"), LookIn:=xlFormulas, Lookat:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
' If the rightmost data is in column 2 or less (where you are deleting from), then exit the loop.
If BottomRightCell.Column <= 2 Then
Exit For
End If
' Otherwise, call your code.
DeleteCellShiftLeft()
Next x
这应该打电话给你的代码的50倍,或停止调用它如果在细胞向右没有更多的数据。
我希望这可以解决您的问题。
我重新安排了一些我的数据,它的工作很好,你的代码非常感谢! – wannaknow2017
@ wannaknow2017很高兴听到它有用! –