Most efficient way to delete row with VBA

前端 未结 4 432
粉色の甜心
粉色の甜心 2021-01-14 12:27

I currently have a macro that I use to delete a record if the ID doesn\'t exist in a list of ID\'s I created from an XML document. It does work like I want it to, however I

4条回答
  •  盖世英雄少女心
    2021-01-14 12:51

    Note: I don't have enough "reputation" to add my comments thus posting as answer. Credit to hnk for wonderful answer (Long Answer). I have one edit as suggestion:

    Once you split the long string and in case the last block is more than the set character then it is having "!" at the end which is throwing error for range method. Addition of IF statement and MID is ensuring that there is no such character.

    To handle that, use:

    For i = LBound(DelStr_Cut) + 1 To UBound(DelStr_Cut)
        If Right(DelStr_Cut(i), 1) = "!" Then
            DelStr_Cut(i) = Mid(DelStr_Cut(i), 1, Len(DelStr_Cut(i)) - 1)
            Set DeleteRng = Union(DeleteRng, ActiveSheet.Range(DelStr_Cut(i)))
        Else
            Set DeleteRng = Union(DeleteRng, ActiveSheet.Range(DelStr_Cut(i)))
        End If
    Next i
    

    Thanks, Bakul

提交回复
热议问题