Excel 如果列中的单元格为空或具有值“,则删除单元格;空”;

Excel 如果列中的单元格为空或具有值“,则删除单元格;空”;,excel,vba,Excel,Vba,我有这段代码,想重复50次,或者直到右边的单元格中没有值为止 Sub DeleteCellShiftLeft() For i = 1000 To 1 Step -1 If (Cells(i, 2).Value = "") Then Cells(i, 2).Delete shift:=xlToLeft End If Next i End Sub 要检查右侧是否还有更多数据,可以使

我有这段代码,想重复50次,或者直到右边的单元格中没有值为止

Sub DeleteCellShiftLeft()  

    For i = 1000 To 1 Step -1        

        If (Cells(i, 2).Value = "") Then 

            Cells(i, 2).Delete shift:=xlToLeft

        End If     

    Next i 

End Sub

要检查右侧是否还有更多数据,可以使用Find函数查找包含数据的最右侧单元格

试试这个:

    ' 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次
对于x=0到50
'使用“查找”功能查找最右下角包含任何内容的单元格。
设置BottomRightCell=ActiveSheet.Cells.Find(what:=“*”,After:=ActiveSheet.Range(“A1”),LookIn:=xlFormulas,Lookat:=_
xlPart,SearchOrder:=xlByColumns,SearchDirection:=xlPrevious,MatchCase:=False)
'如果最右边的数据位于第2列或更少的列(从中删除),则退出循环。

如果是BottomRightCell.Column,我也重新整理了一些数据,它与您的代码配合得很好,非常感谢@wannaknow2017很高兴听到它很有用!