How to Delete Rows in Excel Worksheet based on a Criteria

前端 未结 4 1623
抹茶落季
抹茶落季 2021-01-25 11:19

I have an excel workbook, in worksheet1 in Column A, IF the value of that column = ERR I want it to be deleted (the entire row), how is that possible?

PS: keep in mind t

4条回答
  •  悲&欢浪女
    2021-01-25 11:52

      sub delete_err_rows()
          Dim Wbk as Excel.workbook  'create excel workbook object
          Dim Wsh as worksheet       ' create excel worksheet object 
          Dim Last_row as long
          Dim i as long
          Set Wbk = Thisworkbook ' im using thisworkbook, assuming current workbook
                                 ' if you want any other workbook just give the name 
                                 ' in invited comma as "workbook_name"
          Set Wsh ="sheetname"   ' give the sheet name here 
          Wbk.Wsh.activate
         ' it means Thisworkbook.sheets("sheetname").activate
         ' here the sheetname of thisworkbook is activated
         ' or if you want looping between sheets use thisworkbook.sheets(i).activate
         ' put it in loop , to loop through the worksheets
         ' use thisworkbook.worksheets.count to find number of sheets in workbook
         Last_row = ActiveSheet.Cells(Rows.count, 1).End(xlUp).Row 'to find the lastrow of the activated sheet
              For i = lastrow To 1 step -1
                 if activesheet.cells(i,"A").value = "yourDesiredvalue"
                      activesheet.cells(i,"A").select  ' select the row
                      selection.entirerow.delete       ' now delete the entire row
                 end if
              Next i
       end sub
    

    Note any operations that you do using activesheet , will be affected on the currently activated sheet

    As your saying your a begginner, why dont you record a macro and check out, Thats the greatest way to automate your process by seeing the background code

    Just find the macros tab on the sheet and click record new macro , then select any one of the row and do what you wanted to do , say deleting the entire row, just delete the entire row and now go back to macros tab and click stop recording .

    Now click alt+F11 , this would take you to the VBA editor there you find some worksheets and modules in the vba project explorer field , if you dont find it search it using the view tab of the VBA editor, Now click on module1 and see the recorded macro , you will find something like these

            selection.entirerow.delete
    

    I hope i helped you a bit , and if you need any more help please let me know, Thanks

提交回复
热议问题