简体   繁体   English

VBA在Excel中删除多行

[英]VBA delete multiple row in excel

I need help with this code, how can I make it do delete multiple row of number? 我需要有关此代码的帮助,如何使它删除多行数字? Should I use "And" function ? 我应该使用“ And”功能吗?

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

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 使用Long而不是Integer
  • run the loop backwards 向后运行循环
  • use .Text to catch both numeric and text values 使用.Text捕获数字和文本值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM