How To Delete A Row In Excel Using Vba

How to Delete a Row in Excel Using VBA

In Excel, you can use VBA (Visual Basic for Applications) to automate tasks and manipulate data. Deleting a row in Excel using VBA involves using the Delete method of the Range object.

Step 1: Open the VBA Editor

Open Excel and press Alt + F11 to open the VBA Editor. This is where you’ll write your VBA code.

Step 2: Insert a New Module

In the VBA Editor, go to Insert > Module to insert a new module. This is where you’ll write your VBA code.

Step 3: Write the VBA Code

Now, let’s write the VBA code to delete a row in Excel. Below is an example:


  Sub DeleteRow()
    Dim rng As Range
    
    ' Set the range to the row you want to delete
    Set rng = Range("A2")
    
    ' Delete the entire row
    rng.EntireRow.Delete
  End Sub
  

In this example, we’re deleting the row containing the cell A2. You can modify the range as per your requirement.

Step 4: Run the VBA Code

To run the VBA code, close the VBA Editor and go back to the Excel worksheet. Press Alt + F8 to open the “Macro” dialog box.

Select the DeleteRow macro and click on the “Run” button. This will execute the VBA code and delete the row specified in the code.

Additional Notes:

  • If you want to delete multiple rows, you can change the range accordingly. For example, Range("A2:A5") will delete rows 2 to 5.
  • You can also use variables to dynamically specify the range you want to delete.

Leave a comment