R
ryguy7272
I am getting an error with this code, which was pieced together from various
older discussions from right here...
Private Sub Worksheet_Change(ByVal Target As Range)
Dim custLookup As Range, lRow As Long
Dim i As Range
If Target.Cells.Count > 1 Or _
Target.Value = "" Then Exit Sub
With Sheets(2)
lRow = .Cells(.Rows.Count, 1).End(xlUp).Row
End With
Select Case Target.Column
Case Is = 1
Set custLookup = Sheets(2).Columns("A").Find( _
What:=Target.Value, _
LookIn:=xlValues, _
MatchCase:=False)
For Each i In rng '< -- error occurs here
If i.custLookup Is Nothing Then '< -- error occurs here too
i.EntireRow.Copy Sheets(2).Cells(lRow + 1, 1)
Else
If MsgBox("This customer already exists. " & _
"Would you like to add them again?", _
vbYesNo) = vbYes Then
i.EntireRow.Copy Sheets(2).Cells(lRow + 1, 1)
End If
End If
Next i
Case Is = 2
' do stuff
End Select
End Sub
The code errors on this line:
For Each i In rng
Excel says i = nothing.
I don't understand why. Please help.
Thanks,
Ryan--
older discussions from right here...
Private Sub Worksheet_Change(ByVal Target As Range)
Dim custLookup As Range, lRow As Long
Dim i As Range
If Target.Cells.Count > 1 Or _
Target.Value = "" Then Exit Sub
With Sheets(2)
lRow = .Cells(.Rows.Count, 1).End(xlUp).Row
End With
Select Case Target.Column
Case Is = 1
Set custLookup = Sheets(2).Columns("A").Find( _
What:=Target.Value, _
LookIn:=xlValues, _
MatchCase:=False)
For Each i In rng '< -- error occurs here
If i.custLookup Is Nothing Then '< -- error occurs here too
i.EntireRow.Copy Sheets(2).Cells(lRow + 1, 1)
Else
If MsgBox("This customer already exists. " & _
"Would you like to add them again?", _
vbYesNo) = vbYes Then
i.EntireRow.Copy Sheets(2).Cells(lRow + 1, 1)
End If
End If
Next i
Case Is = 2
' do stuff
End Select
End Sub
The code errors on this line:
For Each i In rng
Excel says i = nothing.
I don't understand why. Please help.
Thanks,
Ryan--