- Edited by RussellDVW 14 hours 29 minutes ago
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
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
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
- Edited by James Cone 8 hours 58 minutes ago
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