VBA delete multiple row in excel

天涯浪子 提交于 2021-02-05 12:18:38

问题


I need help with this code, how can I make it do delete multiple row of number? Should I use "And" function ?

Thanks

Sub remove_rows()

    Dim rad As Integer
    Dim Sheet1 As Worksheet

    Set Sheet1 = Worksheets("Export Worksheet")

    Application.ScreenUpdating = False

    'Which row do u want to start with?
    rad = 1

    'Loop row that delete all row in Sheet1 that contain number 2265174

    Do Until IsEmpty(Sheet1.Cells(rad, 1)) = True
        If Sheet1.Cells(rad, 1).Value = "2265174" Then
            Rows(rad).Delete
            rad = rad - 1
        End If
        rad = rad + 1

    Loop

    Application.ScreenUpdating = True

End Sub

回答1:


Consider:

Sub remove_rows()

    Dim rad As Long, rrad As Long
    Dim Sheet1 As Worksheet

    Set Sheet1 = Worksheets("Export Worksheet")

    Application.ScreenUpdating = False

    'Which row do u want to start with?
    rrad = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row

    'Loop row that delete all row in Sheet1 that contain number 2265174

    For rad = rrad To 1 Step -1
        If Sheet1.Cells(rad, 1).Text = "2265174" Then
            Rows(rad).Delete
        End If
    Next rad

    Application.ScreenUpdating = True

End Sub

Note:

  • use Long rather than Integer
  • run the loop backwards
  • use .Text to catch both numeric and text values


来源:https://stackoverflow.com/questions/52626828/vba-delete-multiple-row-in-excel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!