PC Review


Reply
Thread Tools Rate Thread

Auto Copy from Sheet1 to Sheet2

 
 
ryguy7272
Guest
Posts: n/a
 
      10th Jan 2008
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--


--
RyGuy
 
Reply With Quote
 
 
 
 
JLGWhiz
Guest
Posts: n/a
 
      10th Jan 2008
I don't see where you have defined what the variable rng represents.
There should be a Set rng = 'Some range statement and I don't see it.

"ryguy7272" wrote:

> 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--
>
>
> --
> RyGuy

 
Reply With Quote
 
Jim Cone
Guest
Posts: n/a
 
      10th Jan 2008

rng is not declared... Dim rng as Range
rng is not pointing to anything... Set rng = Range("A1", Cells(lRow,1))
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)




"ryguy7272"
wrote in message
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--
--
RyGuy
 
Reply With Quote
 
ryguy7272
Guest
Posts: n/a
 
      10th Jan 2008
Thanks JLGWhiz!

The error is gone, but Excel doesn't copy/paste anything when it runs. This
is what I have now:
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)
Set rng = Range("A1:C10")
For Each i In rng
If custLookup Is Nothing Then
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



--
RyGuy


"JLGWhiz" wrote:

> I don't see where you have defined what the variable rng represents.
> There should be a Set rng = 'Some range statement and I don't see it.
>
> "ryguy7272" wrote:
>
> > 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--
> >
> >
> > --
> > RyGuy

 
Reply With Quote
 
Incidental
Guest
Posts: n/a
 
      10th Jan 2008
Hi RyGuy

Your variable "rng" is not declared in your code i.e. Dim rng as
range, also you are trying to loop through "rng" which is not set to
anything i.e. set rng=range("A1:A100")

You then go on to "if i.custlookup" this wont work as you are
effectively saying range.range???

if you are looking for a cell ("i") in the range ("rng") that contains
the same value as the range ("CustLookUp") you could write it as

For Each i In rng
If i.value=custlookup.value then

or if you want the cells that do not hold the same value as custlookup
you could write it as

For Each i In rng '< -- error occurs here
If i.value <> custlookup.value then


I hope this helps you out

Steve


 
Reply With Quote
 
ryguy7272
Guest
Posts: n/a
 
      10th Jan 2008
WOW!!! That did it Jim! Thanks all! Final version below:

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)
Set rng = Range("A1", Cells(lRow, 1))
For Each i In rng
If custLookup Is Nothing Then
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



Regards,
Ryan--


--
RyGuy


"Incidental" wrote:

> Hi RyGuy
>
> Your variable "rng" is not declared in your code i.e. Dim rng as
> range, also you are trying to loop through "rng" which is not set to
> anything i.e. set rng=range("A1:A100")
>
> You then go on to "if i.custlookup" this wont work as you are
> effectively saying range.range???
>
> if you are looking for a cell ("i") in the range ("rng") that contains
> the same value as the range ("CustLookUp") you could write it as
>
> For Each i In rng
> If i.value=custlookup.value then
>
> or if you want the cells that do not hold the same value as custlookup
> you could write it as
>
> For Each i In rng '< -- error occurs here
> If i.value <> custlookup.value then
>
>
> I hope this helps you out
>
> Steve
>
>
>

 
Reply With Quote
 
JLGWhiz
Guest
Posts: n/a
 
      10th Jan 2008
I am not really sure what you are trying to do, but I think you want to
change this line from:

If custLookup Is Nothing Then

To:

If i <> custLookup Then

When I do complex find and compare procedures, I find it useful to take a
pencil and paper and draw out my logic sequence of events to make sure I am
not looking in air holes.

"ryguy7272" wrote:

> Thanks JLGWhiz!
>
> The error is gone, but Excel doesn't copy/paste anything when it runs. This
> is what I have now:
> 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)
> Set rng = Range("A1:C10")
> For Each i In rng
> If custLookup Is Nothing Then
> 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
>
>
>
> --
> RyGuy
>
>
> "JLGWhiz" wrote:
>
> > I don't see where you have defined what the variable rng represents.
> > There should be a Set rng = 'Some range statement and I don't see it.
> >
> > "ryguy7272" wrote:
> >
> > > 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--
> > >
> > >
> > > --
> > > RyGuy

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
copy data from sheet2 to sheet1 when sheet2 has variable # of rows Anne Microsoft Excel Misc 6 27th Feb 2009 09:48 PM
A1 Sheet2 is linked to A1 sheet1 so that user enters value(abc123) a1 sheet1 and A1 sheet2 is updated pano Microsoft Excel Programming 2 28th Oct 2007 02:32 PM
Copy result from sheet1 to sheet2 =?Utf-8?B?V2lubmll?= Microsoft Excel Misc 3 26th Jun 2006 09:22 AM
Copy some information from sheet1 to sheet2 john_liu Microsoft Excel Programming 1 26th Mar 2005 04:37 PM
Copy values from Sheet1 to Sheet2 Eintsein_mc2 Microsoft Excel Misc 1 6th Jan 2005 05:02 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:08 PM.