PDA

View Full Version : Delete rows when the code gets deleted.



adamsm
01-30-2011, 11:53 PM
Hi,

I'm trying to figure out a Macro code that would delete the appropriate row form Sheet 2 when the code number in column F of the Sheet 1 is deleted.

Lets say for an example; the user writes a serial number such as 0133 in cell "O6" of the Sheet 1 and the code numbers that comes with it in the column "F" of the same sheet. When the user clears one of the code number such as 0001 and hit the "save data" button, how could it be made so that only the row containing 0001 with the serial number 0133 gets deleted.

I hope I've made my question clear.

Any help on this would be kindly appreciated.

mdmackillop
01-31-2011, 03:53 PM
Not totally clear on what you want, but this should delete the combination of Selected Cell and the Serial Number
Option Explicit
Sub DelRow()
Dim ws As Worksheet
Dim c As Range
Dim FirstAddress As String
Dim V1, V2
Set ws = Sheets("Sheet 2")
V1 = Selection
V2 = Cells(6, 15)
With ws.Columns(2).Cells
Set c = .Find(What:=Cells(6, 15), LookIn:=xlValues)
If Not c Is Nothing Then
FirstAddress = c.Address
Do
If c.Offset(, 1) = V1 Then
c.EntireRow.Delete
MsgBox "Row deleted"
Exit Sub
End If
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> FirstAddress
End If
End With
End Sub

adamsm
02-01-2011, 12:36 PM
Thanks for the help mdmackillop. If I may add; Suppose if the user writes a number such as 0015 in the row below the row where the code 0001 exists in the sheet 1 and hit the macro button; How could I add up a line to the code so that it would add a row with the number 0015 to the sheet 2 just below the row where 0001 is written ( in sheet 2)