Deleting rows in a worksheet

seekyourway

New Member
Joined
Nov 1, 2011
Messages
21
Hello,

I just wanted to delete all rows in a worksheet. The macro doesn't give any error, but it's not deleting. It seems to be shifting rows as it loops. This is what I have:

Dim wB As Workbook
Dim wS As Worksheet
Dim lastRow As Integer
Dim j As Integer

'On Error GoTo ErrHandler
Application.ScreenUpdating = True

'Delete existing records in template
Workbooks.Open HosTemplate
Set wB = ActiveWorkbook
Set wS = ActiveWorkbook.Worksheets("data")

With wS
lastRows = .UsedRange.Rows.Count

For j = 2 To lastRows
.Rows(j).Delete
Next j

End With

wB.Save
wB.Close

Thank you for helping
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
.
Please use the menu selection for placing your code inside a code window. "<vba/>"

VBA Code:
Option Explicit

Sub DelAllRws()
    Sheet1.Rows(2 & ":" & Sheet1.Rows.Count).Delete
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,982
Members
449,201
Latest member
Lunzwe73

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top