Deleting a row in a spreadsheet using row number
I need the code for a userform where I have a text box I put a row number in and a delete button to delete the row in the text box.  I don't need it to copy the data and move it all I want it to do is to delete it.  I can't figure it out, if someone has a solution I would appreciate it.

April 9th, 2015 12:51pm

Re: delete row from a userform

In the UserForm module...
'---
Sub RowRemove()
  Dim Rw As Long 
  Rw = VBA.Val(Me.TextBox1.Text)
  If Rw > 0 And Rw <= ActiveSheet.Rows.Count Then
    ActiveSheet.Rows(Rw).Delete
  Else
    MsgBox "bad row number"
  End If
End Sub

Private Sub CommandButton1_Click()
  Call RowRemove
End Sub
'---

Also, setting the MaxLength of the textbox to 7 will help prevent some errors.

'---
Jim Cone
Portland, Oregon USA
free & commercial excel programs (n/a xl2013)
https://jumpshare.com/b/O5FC6LaBQ6U3UPXjOmX2

Free Windows Admin Tool Kit Click here and download it now
April 9th, 2015 1:42pm

Private Sub cmdclose_Click()
Unload Me
End Sub

My delete button is called cmddelete and I get some kind of error when I attempt to use it.  Thanks for the code, I'm new to this so I don't know what I'm doing wrong, so any help would be greatly appreciated.  Thanks in advance !

Private Sub cmddelete_Click()
Sub RowRemove()
  Dim Rw As Long
  Rw = VBA.Val(Me.TextBox1.Text)
  If Rw > 0 And Rw <= ActiveSheet.Rows.Count Then
    ActiveSheet.Rows(Rw).Delete
  Else
    MsgBox "bad row number"
  End If
End Sub

Private Sub CommandButton1_Click()
  Call RowRemove
End Sub
End Sub

April 9th, 2015 3:19pm

I got it to work, whew !!! Now that that works is there any way to put more than one row to be deleted in the text box ?  Example would be 1,3,9  showing I wanted to delete rows 1 3 and 9.  Also can a warning pop up to tell me we are trying to delete a blank row.  Thanks in advance, the code was awesome.
Free Windows Admin Tool Kit Click here and download it now
April 9th, 2015 3:24pm

Re:  "...now is there any way to..."

I see "mission creep" ocurring here.
Your request can get complicated...
  What if the user enters "1,3;xyz,9 44" ?
  You can prevent entries into a textbox, that are not numbers or commas, using the Textbox_KeyPress event.
  You can also clean up the text after entry by removing all non-numbers and dupe commas.
  Then the Split function can be used to separate the entry into row numbers.
  Verification by the user (with a message box) at this point would be wise.
  Each row could then be verified to contain data using Application.WorksheetFunction.CountA.

Suggest you review the VBA help file for: KeyPress event, Like operator, Split function, Msgbox function and the Excel CountA function; then post a new question.

'---
Jim Cone
April 9th, 2015 6:31pm

Yeah I agree, I got the form to work as needed, is there a way to "Clear" a row vs deleting it?  If there is this will be my last question on this project.  Jim I want to thank you for your help.
Free Windows Admin Tool Kit Click here and download it now
April 9th, 2015 7:33pm

Re:  clear instead of delete

Each of the following works a little differently...
    ActiveSheet.Rows(Rw).Clear              'just like new
    ActiveSheet.Rows(Rw).ClearContents  'leaves the formatting in place
    ActiveSheet.Rows(Rw).ClearFormats    'leaves the contents in place
'---
Jim Cone
April 9th, 2015 8:48pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics