In Excel I open a Form created in VBA which has a CommandButton that erases the row you have selected in a ListBox (which shows 6 columns from Sheet1) displayed in the same form . I want to open the form from other worksheets (in this case Sheet5) but the code I have only works on the current worksheet (erasing the corresponding row on Sheet5). How can I write the code for the CommandButton to erase the row selected but on Sheet1? This is the code I currently have:
Private Sub BotonBorrarP_Click()
Dim i As Integer
For i = 1 To Range("C1048576").End(xlUp).Row - 1
If ListaIngresos.Selected(i) Then
Rows(i 1).Select
Selection.Delete
End If
Next i
End Sub
CodePudding user response:
Reference the sheet directly:
Private Sub BotonBorrarP_Click()
Dim i As Integer, ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
'step backwards when deleting rows!
For i = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row - 1 to 1 step -1
If ListaIngresos.Selected(i) Then
ws.Rows(i 1).Delete
End If
Next i
End Sub