• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

to delete first 1000 rows or more rows of excel

anuwers

Member
Dear Friend,

I need the VBA code for following.

I have near to 50000+ rows with data.
I need the first 1000 (or) 2000 rows to be deleted while running macro.
While running macro.. it should ask user as how many rows to be deleted.
User might write the requiremnt either as 1000 or 1500 or 1800 or 2000 or more..
Note: it should not delete the first row, as always it has headers.
Based on the input and click OK, requested rows to be deleted and then below rows to come up after deleting.

Kindly help in this please
 
Hi,

Please, try this

Regards

Code:
Public Sub deleteRows()

    Dim answer As Variant
    answer = InputBox("How many rows to be deleted?")
    
    If Not IsNumeric(answer) Then
       MsgBox "For this operation a valid number is required"
       Exit Sub
    End If
    
    Dim numrows As Variant
    numrows = CLng(answer)
    
    Dim s As Range
    Set s = Selection.Cells(1, 1)
    
    s.Resize(numrows).EntireRow.Delete
    
End Sub
 
Hey Frank !​
  • For a valid number just use Application.InputBox - a VBA help must read ! - rather than InputBox

  • numrows does very not need to be Variant when using CLng ! As used only once so this variable is useless …

  • Weird idea to use Selection ! Better work with UsedRange …

  • No need to create an useless Range variable to directly delete a Range !
    And do not forget to free it (set to Nothing) before the procedure ends …
    Such variable may be usefull only if used several times within the VBA procedure.
So you can do it just with no more than 3 codelines …​
 
Sorry for the delayed response.

Can you guide additionally with date selection and delete the row.

Either user will write in the message box to delete the number of rows (or) by writing from and to dates based on the date available in column C
 
Code:
Sub deleteRows()
On Error Resume Next
Cells(2, 1).Resize(InputBox("How many rows to be deleted?")).EntireRow.Delete
End Sub
 
anuwers, for filtering data by dates : according to the missing informations & attachment you can start yourself with the Macro Recorder …​
 
Back
Top